27 January 2013

Writing Codes for Human Beings


Writing codes, in a human understandable format is very important thing in software engineering. Choosing good names for variables, methods, tables, columns and etc. is one of them. Writing comments *always* is not something good. It is another sign of code smell. Anyway, I will not go deep inside this concept. May be later :)

Now, I want to a simple way to make your codes in more readable form. This is a very simple trick. If you know the capabilities of your toolkit (I mean, programming language), you can do such these tricks very quickly.

Suppose, you have a scheduled job which runs every 15 minutes. It always searches some tables, 15 minutes before SYSDATE. (Actually, in production environment, you store last execution time of your program or you have a wise algorithm for selecting ). You can do this such a date arithmetic as shown below:

select date '2012-01-20' - ( (1*60*15) / 86400 ) from dual;

But this is not readable .The numbers can be difficult to read. You can use some constants( like, instead of 86400 you can define a constant SECONDS_IN_A_DAY) ; but instead of this, you can use a very simple function (to_dsinterval) as shown below:

select date '2012-01-20' - to_dsinterval('00 00:15:00') from dual;

This usage is more human readable. 0 days, 0 hours, 15 minutes and 0 seconds(actually 15 minutes).

Knowledge is power, please do not forget...

No comments: