When Should You Use a Web Framework Such as CakePHP, Pylons, etc…?

I would like to discuss a few good reasons you or your web developer should use or not use a web framework.

Web Development frameworks have taken off since the release of Ruby on Rails (RoR).

Some other frameworks that have come along are CakePHP, CodeIgniter (PHP) and Pylons (Python).

The basic goal of web frameworks is to increase the quality and speed of development by giving a collection of tools and methodologies that build on an existing language.

Or in short write better code, faster.

But what is the reality and when should a framework be used?

I will focus on PHP and Python frameworks because those are the ones I know best.

Rule One:

If you don’t know the language, learn it first, then learn a framework in that language.

If you are not comfortable with the underlying language than the framework will not make things easier. The added level of abstraction is harder to grok than the “function soup” which is PHP or the Swiss Army knife which is Python.

The added level of abstraction will make debugging harder, because there are more things for you to misunderstand (logic, language, or framework).

Rule Two:

Frameworks rule how you think about your web application, do not fight your ruler, she means well and often knows best.

Frameworks are built on their own ideologies about what beautiful, powerful, maintainable code should look like, you should strive to learn the “right” way to do things and stick with the plan, taking shortcuts and going against the grain will lead to your code being less maintainable than if you worked from the bottom up.

Pylons is especially good at giving you a balance of powerful tools and doing as you like it.

Rule Three

If its a simple project don’t bother with a framework.

If your project doesn’t require datebase table joins, foreign keys, isn’t going to add new features in the future, and is basically just going to get up and shut up, forget the framework, just build and profit.

Right tool for the job.

Rule Four

Frameworks make the opening moves longer and require more thought, but the end game is much, much easier.

Generally speaking you can get something up that does something sorta kinda half-assed quicker without a framework, but then again when you try to finalize, perfect and tweak your Frankenstein of a web application you will find out where all the warts and inconsistencies are.

Fixing bugs on a lot of code is harder than fixing bugs on a little code, so this is why its a good idea to do things right earlier rather than later.

Rule Δ

If you use a PHP framework at some point you will come to the conclusion that you are _still_ using PHP. It will not be a happy moment.

The reality of the situation is that PHP is great because it has no learning curve and every cheap webhost supports it. That being said it is woefully inconsistent, its a function soup and it doesn’t lend itself to writing quality code easily.

If you really need to make sure that deployment is brain dead simple than you might have to use PHP. Learn good PHP. The best example of good PHP is Drupal CMS. Regardless of what you think of the CMS as a whole, the documentation, community and coding standards are good models to follow.

Download the source, learn how they implement hooks, and feel free to copy or use (respecting the OSS license) their library of include files (like multi-byte support, validation, form creation etc).

Even if you don’t use a PHP framework, make sure you have a library of include files to make your life easier, have some sort of “$H” class that writes html (such as form elements) and an easy “e($str)” is for echo functions which make it easy to do things like echo htmlentities($str, ENT_QUOTES, 'UTF-8', True);.

Try to use named parameters when they are needed, I.e foo(Array("width"=>10 ,"height"=>10, "color"=>"blue", time=>"now")) instead of foo(10, 10, "blue", False, False, True, None, Grandma_Moses, "now")

Rule π

If you are going to deploy a Python web application and don’t know what deployment method to use, then use mod_wsgi which points to a virtual environment.

mod_wsgi Apache httpd.conf

WSGIPythonHome /path/to/virtualenv/
<VirtualHost *:80>
# ....
WSGIScriptAlias / /path/to/mypylonsapp/apache/myapp.wsgi
</VirtualHost>

Where myapp.wsgi is like the following:

myapp.wsgi

 
APP_CONFIG = "/path/to/mypylonsapp/development.ini"
 
import sys,os
sys.path.append("/path/to/mypylonsapp/")
 
import site
site.addsitedir('/path/to/virtualenv/lib/python2.5/site-packages')
 
from paste.deploy import loadapp
application = loadapp("config:" + APP_CONFIG)

Also use Genshi with a custom render that uses Filters.

Genshi is the template your template could smell like.

TL;DR

If you are using a framework you are thinking “I am going to do this right the first time to save time and so that I don’t have to think about it later.” if you are using function soup, you are thinking “I am going to get this bastard up as quick and dirty as possible and put lipstick on this ugly pig some other day.”

Share
This entry was posted in intermediate.programming and tagged , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 3.00 out of 5)
Loading ... Loading ...

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

If you are going to post code please use:
<pre lang="php" escaped="true"> YOUR_CODE_HERE </pre>

Change the lang to mysql, python, lisp, whatever. This will escape your code.