Saturday, July 10, 2010

Vietnam - 4. Simplifying

How do I simplify the following problems?
Super long functions
Make staffs understand that ONE function should do ONE and only ONE specific task! Normally, the a function should contain no more than 20 LOC for our PHP web projects, excluding comments and line separators. Unless, it's a controller function.

Big fat controllers
To simplify the project, we should keep the controller skin, model fat!
Why? All the logics are in the controller, we shouldn't make it more complicated than it should be!

Logics found in the views
All the logics should be in the controller. The purpose of the view is displaying the data. The decoration should be in the CSS. Sound simple, but making all staffs understand this concept is not as simple as it should be. I need to give them concrete examples.

Zend_Form not being used
Most of our staffs do not understand Zend_Form. They think it's too complicated. I have to show them a concrete example of Zend_Form. The code from controller has been reduce from about 100 LOC to about 20 LOC.
Here are some features from Zend_Form:
  • Has a lot of built in validators. Can validate number, string, phone number, credit card...
  • Separate the code from HTML
  • Can be decorated with Zend_Decorator
  • Reusable code
  • Easier to debug
Zend_Form is quite complex, example and documentation can be found here:
http://akrabat.com/php/simple-zend_form-example/
http://framework.zend.com/manual/en/zend.form.html

Variable name has no meaning
This is very hard to simplify! One simple rule is no abbreviation.
As most of the data type we are dealing with are string, number, array, Zend_Db_Table_Row, Zend_Db_Table_Rowset, therefore, I defined the following rules for our developers:
  • Zend_Db_Table_Row - variable must end with Row. E.g.: $userRow
  • Zend_Db_Table_Rowset - variable must end with Rowset. E.g.: $userRowset
  • Array - we use plural to indicate array or self-describing name. E.g.: $userRows, $usernames, $userProfile
  • String - normal variable name. E.g.: $username, $firstName,...
  • Number - normal variable name. E.g.: $userAge, $earning,...
The most important concept I've taught our staffs about naming convention are:
  • Variable/Class names should be noun unless you have a good reason for it not to be noun.
  • Function names must be verb.
  • Always use self-describing names.
  • Never use shortcut.
English is a second language for our developers, therefore, we have to limit the vocabularies being used.
  • All functions return a boolean must start with is, has, can.
  • Function start with set accepts only ONE input parameter. E.g.: setEmail($email), setAge($age), setPassword($password)...
  • Function start with update accepts an array as input parameter. E.g.: updateUserProfile($userProfile), updatePostalAddress($postalAddress), updateCreditCardInfos($creditCardInfos)...
  • We have a lot of problems with naming the action controller. Here are some examples of inconsistency: getUsersAction, listUsersAction, showUsersAction,... I forced all developers to name it show. In fact, any action name from controller that involve displaying data must be started with show. Similar for edit/update/change & delete/remove/del & add/insert/insert-new/new...
Magic numbers
Magic numbers can be eliminated by moving to a constant file.

Global variables
Global variables can be used but it need to be well explained & self-describing. However, we should try to minimize the use of global variables.

It cost me a lot of time to put our staffs on track. Here are some fundamental rules for my staffs:
  • All functions must have comments and @author.
  • Global variables must have comments.
  • Write short simple functions.
  • No magic numbers.
  • Variable and function names must be self-described.
  • No abbreviation.
  • If you think that section of code you've wrote is complex, then it is! Find a way to simplify it.
Everything should be made as simple as possible, but not simpler. Albert Einstein.

    No comments:

    Post a Comment