It's possible, but bad style...

As a direct follow-up to my [previous post](/en/blog/2007/11/Informatiklehrer), some more observations. Seems like it is common to discourage something because it’s “bad style”. As an example, accessing static class attributes using an instance instead of using the class object.

By itself, there’s nothing wrong with the statement. That is bad style for sure, and if you think you really need it, you better be able to explain why another solution wouldn’t cut it. But why? Because you’ll get points deducted in the exam if you write things that way, that’s why. Uh, yes. Nothing like a clear answer.

The actual reason, of course, is different. If you access the static property via an instance, people, and that is in most cases, the future you, will get confused when reading the code, because something acts as a static property instead of an instance property, although it looks just the same. One can always look at the class declaration, but that takes time and rips you right out of the understanding process. That is potentially harmful, and you should avoid it if at all possible.

Is that explanation so difficult? Saying it’s simply bad style for no apparent reason might stop people from using it in tests. But without reasons, there’s nothing to stop them from using it in real life, and that’s where it counts.

Written on November 27th, 2007 at 03:39 pm

0 Comments

    New comments can no longer be posted because it got to annoying to fight all the spam.