Stephen on Software aka SOS

May 24, 2009

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

Filed under: Programming — Tags: , — sljm @ 1:26 pm

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.

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

May 20, 2009

Using Ruby and ERB for code generation

Filed under: Programming — Tags: — sljm @ 5:22 pm

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

require 'erb'

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

Sample of the ERB file – test.erb

public class <%=name%>{
}

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.

Blog at WordPress.com.