Monday, June 29, 2009

Please use a web framework!

I have always been hyped over discussions about which framework is better and why. Today, I encountered a very weird debate regarding ditching web frameworks. Previously I have heard of making self-designed ones, but this one totally knocked my socks off. Then suddenly I realize there are lots of coders out there who still code this no-framework style.

Therefore I will list some reasons why we need a framework,
  1. Less code. As you may have noticed, writing boilerplate code, that handles incoming requests, deals with HTTP headers, HTML, basic parsing, low level SQL connections to databases is very boring. It also is very error prone. Above all, good news, it has already not only been accomplished, but also this code will improve and debug itself. Programmer (your) time is very expensive, I'm sure you have better things to do (such as writing code that provides you with entertainment or income).
  2. Such frameworks are coded by different people, and they may have had more expertise on the topic of their code. For example, ORM code is nothing to joke with. It requires specific knowledge of how SQL works on different systems as well as internal aspects of the programming language. It is more likely that the solution available in a framework has been debated over in mailing lists and seen many tweaks and hacks.
  3. Security. Open source frameworks with peer review are often more secure than locally hacked solutions
  4. Reinventing things. Some argue that use of frameworks will remove flexibility and performance. Well, bad news, so does using a high level language, so we might as well code all web apps in pure C, or assembler, how about taking advantage of GPU processing for web requests? And we should write our own OS, you know, the way the TCP stack is handled may be important. Or new interpreters and compilers. Often CPU microcode can be upgraded as a part of a web project to increase performance.
  5. Worldwide success. Success and rapid development (almost explosive in case of Rails) of many frameworks from Java land to PHP, Python and Ruby and any other language used for web coding shows something is happening and world reputable coders aren't playing cat's games.
The specific debate I have heard was against PHP's Symfony in favor of "pure" PHP. The first thing that I have to say here is that PHP may be easily viewed as designed to be the horror of the web coder:) SQL injections, and automatic type conversions have lead to many serious financial losses worldwide. Basically to me PHP without structure looks like a large pile of goo.

The debater points out that there is nothing new in such frameworks, all parts have been written previously. Then goes on to point out PEAR for DB Independence, but forgets to mention anything about ORM. A quick reference to the PEAR official site shows PEAR is not related to databases and is " PEAR is a framework and distribution system for reusable PHP components. "

He continues to point out that in Symfony checking authorization scripts and other validation scripts are in the heart of Symfony. This is obviously true. Anyhow as you may know, in such frameworks it is very easy to replace / ditch this code and use your own. This points out the ill understanding of such web frameworks and is probably the residue of older "closed up" frameworks at work in Javaland and .Netland.

The next point is switching to filing instead of using databases, which again is possible through frameworks, but has little reason for. He also suggests using mod_rewrite for url matching and so, but fails to indicate why it is superior to have to maintain .htaccess files as a part of your code base, which seems to me that is very unnecessary and bad.

The next attack is on Symfony's templating which is basically the same as PHP, anyhow for templating I am more favorite of limited languages such as that of Django's, anyhow the issue of power of syntax is not related to separation of code and view. He then backs smarty as a good substitute, and I agree, but don't know what sort of attack this is to frameworks.

Then he points out that there is nothing new in frameworks and has just made them easier. Yes exactly. Which is why we should use them, unless you have some sort of masochism fetish for maintaining low level code. In that case, I suggest APL.

What I am trying to point out is reuse codes, fellow programmers. Reuse, don't be afraid. Business web applications should not write code for accessing databases, manipulating strings, perform weird system calls, rewrite spellcheckers, calenders, or search engines, or database backends! They should be a simple expression of simple business logic.

It is however important to select a nice framework. The most important feature of a framework is not closing you down. You should select parts you need and ditch / replace other parts easily. Another thing I like is for frameworks to be lightweight and not impose lots of overhead. Also I hate nosy frameworks which impose a special coding style. You should also be able to have access to low level parts when you need. Such as raw sql, or, http request and response objects.

The main point I have is that being scared of change is bad; learning these frameworks are often very easy for the experienced coder. A coder that can only work with his own code is a red light for me. My POV here is that simply people who are fighting new concepts in coding, have never tried them, simply.

As a final roundup, I recommend Symfony, Rails, Django, as closely related friends, and also Spring is a nice thing in Javaland.

No comments:

Post a Comment