-
-
Save dbrady/844950 to your computer and use it in GitHub Desktop.
# Coders from from C often like this style: | |
log status == OK ? "OK" : "PROBLEM" | |
log "All done." | |
# But since if returns a value in ruby, you can spell it out: | |
log if status == OK | |
"OK" | |
else | |
"PROBLEM" | |
end | |
log "All done." | |
# I favor this style, because it pushes the whole expression out and preserves the vertical registration of if/else/end | |
log if status == OK | |
"OK" | |
else | |
"PROBLEM" | |
end | |
log "All done." |
If it fits in 80 chars I occasionally do a one-line if/then/else:
if status == OK then log "OK" else log "PROBLEM" end
Yay! We can still be friends! I feel exactly the same way.
I've shown style C to a couple of recovering PHP programmers in the past and had them absolutely freak out over it.
By default, emacs and TextMate use style C. Sublime Text 2 (a recent addition) uses style B and it's horrific to me.
I strongly prefer style C. I suppose I prefer the haskell/python style of indentation of control blocks. The close of a block should never be at a different indent level than the open of it in my mind.
Oh, that's interesting. In that context, I generally favor
if status==OK; log "OK"; else log "PROBLEM"; end
But I note that I'm in a very different context there--I am coding at a bash prompt (with ruby -e) or at an IRC entry window, and when I'm thinking in one-liner mode I go for the semicolon as fast as possible.
Hmm, I'd probably still do
log(if status==OK; "OK"; else; "PROBLEM"; end)
But only if it was long and complicated, like with multiple nested ifs; otherwise I'd probably just use the ternary ?: operator.
If we're still doing Ruby you don't need any of those semicolons:
puts(if status==OK then "OK" else "PROBLEM" end)
works just fine.
I start with style A and if it gets too long I switch to style C. I would have to fight with my editor to get style B, which is good, because style B is awful.
Also, I never use style A if there are side-effects involved.