« Java String Tricks | Main | SWT Claims: Show me the money »

Code Quality

These code quality metrics proviced by PMD and Eclipse are quite nice, it points out all sort of strange things. But there are some thing it doesn't get...

For example I came across a class written by another developer (not sure who at this point). The first thing that caught my attention about that class is the incessent use of this. to refer to local fields. And he would never use a class name to refer to another static field, which is what tripped eclipse to check. After reviewing the class and parent class in question I came across a doozie... all of the methonds and fields were static, except that the fields were initilized by a non-static constructor! It's not like it's a forced option, since you can use static { ... } to initilize static fields. There are just some stupid patterns that you can never imagine until you see them.

PMD definatly needs a "StaticFieldReference" rule, perhaps they also need a "StaticInitInConstructor" rule, where it barks if you set the value of a static field in the same class inside the constructor of a class. Manipulation is ok.

Comments (2)

You're right in one regard, it's not in and of itself evil. I just come form the schools of thought that variable names shouldn't conflict and that you sohuld be as expressive as necissary, but no more. So this means that 'this' is superfluous except in cases where names conflict, and then we can go to my first school of thought and conclude you are doing somehting wrong. Out of visibility variable names are just asking for coding errors IMHO.

But Pyhton's OO methodology remindes me of doing OO in C: no magically hidden steps (which can be good or bad depending on your needs).

Something wrong with using 'this' for all local field references? I always do this - a habit picked up from Python, I guess, but I son't see that it does any harm. Quite the opposite, I find.

Post a comment


About

This page contains a single entry from the blog posted on February 3, 2003 4:43 AM.

The previous post in this blog was Java String Tricks.

The next post in this blog is SWT Claims: Show me the money.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33