Programming Lesson for These Two Weeks

Submitted by swarut on Mon, 06/04/2012 - 23:33


Do Not Use DBMS's Enum

This applies not only for rails but for all programming environment.  There are reasons behind this which were written at [http://komlenic.com/244/8-reasons-why-mysqls-enum-data-type-is-evil/ ]. Briefly, some important reasons are

  1. Enum is not a standard SQL : Using Enum makes your code less portability. Only MySQL and some DBMS that support Enum.
  2. Enum is not reusable : It is hard to reuse the Enum value.
  3. Enum is less flexible : If in the future, you want to add some description to the enum. It is impossible to do.
  4. Enum is expensive to build : If you had a table with Enum field, changing Enum member (adding a new one, or editing the existing one)  will cause the table to be rebuilt. 

Instead of using Enum, it is better to split that field as another model stored in another table. Using this way,  it is easier to reuse and extend the data usage in the future.


Rails's Before_filter with Only One Method Is Not Good

It is normal to use before_filter hook in Rails. However, if that before_filter was invoked by only one method, it was not good. Instead, write the invoked code directly on the calling method. Use before_filter if there are multiple methods that want invocation.


Make A Clean Commit History before Pushing Code to Git for Being Reviewed

Since last year, I adjusted one of my programming habit: to commit the code very often.  Given one feature I am developing, I will commit the code every times I make a chance. It is like I am writting a history of my code, which is not good for other people to read. According to these two weeks development, the preferable way is to squash the commits of the same feature into one or a few commits.  I found that the approach I am ease with is to commit as I do normally, short and often. However, after the feature is finished, I will use git reset HEAD~x where x is the number of commit for the feature you want to push. This will reset the track and all change are put to stagging stage. Then, I can rewrite the commit message to describe what this feature do in brief.