28.7.06

Eloquent coding

A simple function I wrote 2 years ago looked like this:
function booleanToggle(bool, id1, id2) { 
// getObject is a super-old-school function kinda like $
var style1 = (typeof id1 == 'string') ?
getObject(id1, true) : id1.style;
var style2 = (typeof id2 == 'string') ?
getObject(id2, true) : id2.style;
if (eval(bool)){
style1.display = '';
style2.display = 'none';
} else {
style1.display = 'none';
style2.display = '';
}
}
I ran across it today and couldn't bear looking at it, so now it looks like this:
function booleanToggle(bool, id1, id2) {
$(id1).style.display = eval(bool) ? '' : 'none';
if (id2) { $(id2).style.display = eval(bool) ? 'none' : ''; }
}
I wanted to keep the same signature for backwards compatibility.

There's something about the change that I can't describe. I'm happy because there's less code and I don't think I sacrificed readability. I even made it more flexible, making the last argument optional.

If my prose were eloquent, I'd be able to explain this idea to junior engineers. But it's not.

No comments: