Wednesday, September 06, 2006

» Code formatting +

I try to be consistant about code formatting. I pick a convention and stick to it. But I like the convention to make sense, as well as being easy to read (easy for me, I could care less if you can read it, heh).

If you've seen any of my code in my posts, you probably noticed that I use parentheses for everything -- method invocation, conditionals / other control structures, built-in methods like require, &c. So I write like this:

require('mickeyds')
if (combo_meal and a_drink)
supersize('mr pib')
else
puts('you cheapskate, pay the extra buck')
end
case (eat)
when (/good/)
i_m_lovin_it
else
get_refund("I'm goin' to taco bell!")
end

But I've been thinking lately that it doesn't really make sense to parenthesize with control structures, unless it is for the explicit purpose of grouping. They aren't methods, so they shouldn't be written like methods. I think I'm going to start writing them without the parens and see how I like it. I'm still not sure about require, and puts and so forth. They are methods, but then they are not regular methods -- they are top-level methods. Mabye I'll try writing them with space rather than parentheses as well.

3 Comments:

Anonymous Anonymous said...

Being able not to use parenthesis is very nice for metaprogramming (it looks much better). Consider a simple example:

class Dragon < Creature
  life          1340
  strength  451
  charisma  1020
  weapon    939
end

as opposed to

class Dragon < Creature
  life(1340)
  strength(451)
  charisma(1020)
  weapon(939)
end

The first style looks much better to me, it's almost like a configuration file.
September 20, 2006 at 6:45 AM

 
Blogger Unknown said...

Of course I meant "domain-specific languages" when I said "metaprogramming". So much for 8am...
September 20, 2006 at 6:47 AM

 
Blogger MonkeeSage said...

I think you're right. Your example looks much better with spaces than parentheses.

Ps. 8am...I know! I need some coffee!
September 20, 2006 at 8:42 AM

 

Post a Comment

<< Home