Thursday, July 23, 2009

Simplifying Complex Code

I saw an example in code today that cried for simplifaction. Actually, it reminded me of the first piece of code I had reviewed by someone in Cambridge and it got hacked to pieces. It was a bit hard to swallow, but that kind of feedback definitely helped me in the long run.

So here is the perfectly logical, but unnecessarily complex piece of code...

_method cit_circuit.is_bearer?
l_bearer? << _if _self.cit_circuit_type.code = "Bearer"
_then >> _true
_else >> _false
_endif
>> l_bearer?

_endmethod
$

... which can be greatly simplified to...

_method cit_circuit.is_bearer?

_return _self.cit_circuit_type.code = "Bearer"

_endmethod
$


Any time you see an if/then/else block of code that returns a boolean that should raise a red flag for you that your code structure is too complex.

No comments: