Sunday, May 24, 2009

Lessons from Robert Martin Keynote (What killed Smalltalk could kill Ruby too)

Robert Martin gave the keynote at this years Rails Conf. Beside learning about some programming history and other interesting bits of knowledge , there are a few lessons that most developers can take away from the talk.

1. Clean Code has zero WTF/min

Most interesting  way of measuring if the code is clean. All programmer should take note of this. Even when i read my own code after some time, I tend to think "WTF, what made me write that crap".

Peer review and refactoring helps in this aspect but many development teams don't do it. It takes quite a lot to make programmer's write tests when we are always faced with deadlines.

One reason is that during school,  TDD isn't really emphasized when doing software projects. Perhaps, universities and polytechnic should start teaching people what are good practices, things like Design pattern's, TDD, source control and so on. Grade projects not only on the functional and design aspects but also on the practices aspect. See if they write tests and see if their tests are maintained still work when the project is done.

2. Rules/Laws of TDD
The laws


  1. You may not write production code until you have written a *failing* unit test.



  2. You may not write more of a unit test than is sufficient to fail, and not compiling is failing.



  3. You may not write more production code than is sufficient to pass the current failing test.



3. Professionalism
Robert also talks about what being professional means. Simple to say it means not bowing down to fear and not taking corners when the going gets tough.

[youtube=http://www.youtube.com/watch?v=YX3iRjKj7C0&hl=en&fs=1]

Links

RSpec
Ward Cunningham on Wikipedia
Ward Cunningham on Twitter
Robert Martin's Page at Object Mentor
Robert Martin Page at Wikipedia
Rules of TDD

Thursday, May 21, 2009

Wednesday, May 20, 2009

Using Ruby and ERB for code generation

In my current project (using Java, Spring MVC and ExtJS), we have a lot of modules for data entry and since there were similarities in all the modules (and the fact is we had like 20 of these to code) we decided to take a closer look at code generation.

But what tool to use? Stumble upon ruby in the book Code Generation in Action and found a chapter on code templates and the example uses Ruby and ERBs. I thought wasn't ERB's used in Rails like JSP's? I thought i needed to write web programs in order to generate the code. But boy was i wrong, we could actually use ERBs from any Ruby programs.

Sample of the ruby file - test.rb

[sourcecode language='ruby']
require 'erb'

erb=ERB.new(File.open("test.erb").read());
name='Stephen';
code=erb.result(binding);
print code;
[/sourcecode]

Sample of the ERB file - test.erb

[sourcecode language='ruby']
public class <%=name%>{
}
[/sourcecode]

What's happening here, the name variable that you defined in test.rb is passed to the erb file when erb.result(binding) is execute and it returns the results of the erb file. You can pass in arrays, hashes from the code to the erb file easily just by calling the erb.result(binding) method.

Our experience - Great ! We wrote generators to generate our javascript menus, spring MVC views (which are in ExtJS) to Spring Controller to Hibernate DAOs. It does save us sometime in do repertitive work.

If your project has lots of similar components that you need to code. Why not take a look at code templating using Ruby and ERB.

Monday, May 18, 2009

Decoding URI/URL strings

In one of my last post, I talking about Finding the Url paramters from a URL using javascript.

But there is a problem with the code, what if the url has some HTML encoding inside like %20 (which represents a space) or some other special html encoding.

You will need to use decodeURI function in javascript. The function will decode html special charecters to their correct form so things like Hello%20World will become Hello World.

[source lang='jscript']
function gup( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return decodeURI(results[1]);
}
[/source]

Thursday, May 14, 2009

Creating directories with date using batch files in XP

This is how you create directories with todays date using a batch file for xp/win server command prompt. Useful when you are doing backups

[sourcecode language='shell']
for /F "tokens=2-4 delims=/- " %%A in ('date/T') do set var=%%A%%B%%C
md %var%
cd R:\MACHINE PROGRAMS\%var%\
[/sourcecode]

Copy and paste the code directly, any space where it's not suppose to be can cause the script to break (like a space between the = and %%A%%B%%C.
To use the date just use the variable %var%.

Tuesday, May 12, 2009

Daily Cycle for Developers

Why have a daily cycle? It like a checklist that get you off to a start when you reach work. Much like before the airplane files, they have to go through a checklist to make sure everything is checked ok. LIke going to work before you start coding, probably you should ensure that your environment is ready for you to begin work.

This is my daily cycle when I go to work.

8:00am Start up all my pcs

8:15am
Ensure my development servers are working

Update code from SVN and try to build
Make sure none of the code breaks in my environment, even though we have a build server.

Look at the bug db to see if there are any new bugs

Plan what to do today from the backlog/sprint list/bug list

8:35am
Have some breakfast before the stand up meeting
Can't work on an empty stomach
Do some work

9:30am
Daily Stand up meeting

5:00pm
Ensure source code control backup script backs up correctly.
Make sure the code is safely backedup from the svn server.