Thursday, April 8, 2010

Indentation, indentation...

So, lately I see many developers prefer what I call ASCII Art Indentation style such as:


Class Foo
def bar(params = {})
Barf.find(params.merge :key => "value",
:key2 => "value2", ... and so on )
end
end


with the claim that it reads better, and you know exactly where to look for things.

I say it's completely opposite. It is no where near intuitive.

  • Say you have to change params to parameter_name_that_makes_more_sense ? you have to adjust your whole code base to reflect your tiny change, and apparently no one does.

  • For something like:

    Sundae.new(Icecream.with_fruit.no_sugar.all(:include => [:nutrition_facts, :production_plan]), :whipped_cream => WhippedCream.no_fat.first(:conditions => ["make is not :make and calorie < :calorie", {:make => "Booga", :calorie => 120}]))

    How do you systematically define the ASCII Art Indentation scheme that every one will follow and give exactly the same indentation? Where do you look for the make of the whipped cream? Not easy.



And for some reason, people loath my style of indentation: 1 more soft tab when you open a paren, brace, or bracket followed by a new line. and new line and one fewer soft tab before the close of the matching paren, brace, or bracket. It is said it's hard to see where things are, and it increases the number of lines. But behold:

Sundae.new(
Icecream.with_fruit.no_sugar.all(
:include => [:nutrition_facts, :production_plan]
),
:whipped_cream => WhippedCream.no_fat.first(
:conditions => [
"make is not :make and calorie < :calorie",
{:make => "Booga", :calorie => 120}
]
)
)


  • You can choose not to indent if your list is small enough, less than, say, 40 characters, half the 80x24 line.

  • You can write your code easily to indent stuff for you, consistently.

  • You can instruct people easily to indent the exact same way and give the exact same result.

  • You know exactly where you miss your close paren.

  • You know exactly where you can find your make of the whipped cream.

  • Your code rarely falls off the right edge while leaving an airstrip on the left side.



Prove me wrong, someone.

1 comment:

Harbor Sparrow said...

Inconsistency and unprintability are the two crimes that I can't forgive.