Feb 26, 2008

Mandatory vs. Optional Indentation

So here is a topic for great argument. Should indentation in a programming language be mandatory (à la Python, Haskell) or optional (à la C++, Java, etc.)?

Personally I've always been opposed to mandatory indentation, but my arguments against it are rather weak. I read an article praising mandatory indentation (the guy is a teacher so I can understand where he's coming from, having marked programming courses) and his arguments are a heck of a lot better than mine. He even hits it right on the nose as to why I don't like it: "Anytime somebody tries to impose rules that they follow 99% of the time anyway, they always focus on the 1% exceptions."

So I'm starting to think maybe mandatory indentation is not so bad. It would help make other people's code easier to modify, if not only for getting rid of retarded indentation schemes (see ClipShare) and getting rid of huge lists of closing curly brackets or end keywords, which are a pain in the ass anyway. However, I think enforcing this type of thing on the programming community would be as difficult as enforcing the := operator as the assignment operator to avoid the if (x = true) annoyance.

2 comments:

Anonymous said...

Mandatory indention would be ok if the language is parsable in only one way even without it (i.e. it is only mandatory for stylistic reasons).

Why? When you cut and paste code or transfer snippets (e.g. in email, or books) it is too easy to make errors in indenting if it must be consistent at the global scope.

It also makes it difficult if not impossible for the editor to do all the work for you as well.

Rob Britton said...

I agree.

I've recently been doing more work in Python and have been having mixed opinions about it. While I enjoy not having to put all the end keywords or closing curly brackets, sometimes it just looks weird to have something like this:
code with meaning
code with meaning
  code with meaning
  code with meaning
code with meaning
I find it weird that there is a lack of structure-defining syntax present here. I feel it's like drawing a UML diagram without any boxes, just words and arrows. It is understandable, just different.

Maybe it's just that I'm used to other languages. Maybe not. We'll see.