Friday, May 05, 2006

Instance Variables: Top or Bottom?

A couple of weeks ago, a colleague and I had a discussion on the merits of different programming paradigms. The conversation, like many others, eventually wandered to the topic of the shortcomings of Java, an oppressive land where Everything Is An Object, Nouns are kings and Verbs are pawns, as Steve Yegge tells you in his blog. Java has it all wrong, once more we concluded; programming languages are truly expressive when their primary notions are expressed through functions (verbs), not objects (nouns).

But then my colleague said something that with which I didn't fully agree. He said that if you put a class's instance variables at the top of the file, before the method definitions, it shows bad style. After all, objects (nouns, data) are secondary to functions, and the most important parts of your code should appear at the top; therefore, your methods should appear above your instance variables.

At first I accepted this line of reasoning, but after thinking about it more I realized that I actually disagree. Don't get me wrong -- I'm not trying to make a statement here about the relative merits of different programming paradigms. I just think that since a class's methods refer to its instance variables but not vice versa, the instance variables should appear first. This way, when you're reading the code for the class's methods for the first time, the names of the instance variables are already familiar to you.

Well, that's how I feel about the subject :)

No comments: