I often have to fix other people’s code. You can tell coders who have been coding for years simply by the feel of their code.
One of the problems with java is the simple fact that you can end up with a spaghetti-junction of objects all calling, calling back, implementing, sub classing, overriding, running ejb’s etc etc and it can be a bit of a nightmare to find your way through.
The key here (although you can reverse engineer into a UML Model with Netbeans) is really in the way that the code is written.
Things I do to make code more readable
- Initials of variables
All my strings for instance start with s - sName, sUsername
integers I , iCount, iGold
doubles d
and so on for different types of variables or objects.
Components on forms I use a 3 letter start up.
txtTEXTBOX
lblLABEL
stcSTATICTEXT
cmbCOMBOBOX
Also in the code using the java doc function, within Netbeans if you type /** enter it will auto document the next function and you just “fill in the blanks”, javaDoc is fantastic and well worth looking into. If you have “java doced” your code, not only does Netbeans pick up the documentation when you are typing or using your own functions but also you can generate a website that documents your classes!!! How Call
ALT-SHIFT-F - auto format your code, very useful.
Comment long bracketed loops. I always put // from next to a bracketed loop that is too long to see in one screen. e.g.
for (int iCount=0;iCount<10;++iCount) {
system.out.println("Hello World "+iCount); } // From count to 10 icount
Also comment all calls to EJB’s to say what they do -
// Get this job’s History
JobManagerBean.getHistory(sJobNumber);
Beautiful Code!
Lisp programmers write beautiuful code, why? Because you have to, it becomes unreadable otherwise. In a way I wish all programmers would learn Lisp - because it makes you think in a different way and see software development from an almost etherial height. When I get some further research time I want to work on a lisp implementation for netbeans.
Remember that you will not be around forever, don’t punish the next poor programmer who has to work on your code. I like my code to be really easy to read and understand.
Code Rewriting
You know that sometimes I will rewrite a whole subroutine if I’m not happy with it. Programmers are artists, the difference is that the result of our art is a system for people to use or some form of output. Well written, neat, fast, efficient code means happy users and that is the caling of a programmer, to make the lives of the users easier.
If your software makes it harder. It needs rewriting, or maybe replacing with a pen and paper.
Be passionate about your coding and your users will be passionate about your software.
August 27, 2008 at 9:56 am
Is there a reason why you wouldn’t use Netbeans to tell you the types of things, rather than encode the types in the names? Surely if you hover over ‘ok’ in: ok.setIcon(icon); NB will tell you that ok is a JButton.
August 27, 2008 at 10:07 am
Hi Ricky
Yes ofcourse you can ROLL your mouse over - and Netbeans will in future make it even easier to see what is going on in your code, but I do prefer to just be able to read it.
It’s just as easy to code in sensible code as just button1, button2, button3. Some of my forms we have for some of the bigger systems we are working on can have over 150 controls so any clues in the MILES of code to what is going on that can be viewed at a glance is an advantage.
Good point though.
August 27, 2008 at 10:40 am
If I would write a blog with this title, I would write the exact opposite.
I would say that jobNumber is more readable than sJobNumber. Netbeans shows me what type it is.
With a good name it’s not too hard to guess the type . “count” is certainly some kind of number, “name” a String, okButton a Button.
I always choose a self explaining name for methods instead of putting a comment which says almost the same. And if still needed the comment should be above the called method instead of the calling line. Imagine updating the doc for a method in all callers.
August 27, 2008 at 10:45 am
True
I guess you have to understand I come from an ancient line of Lisp programmers. These techniques are something I’ve used for years and force my poor programming team to do the same!
I do agree with you about function names but again using JAVADOC is fantastic when we have a huge project and that REQUIRES the use of the /** comments and we have a whole bunch of different programmers working on the same system at once. - I don’t want them fiddling with Beans that have been signed off - but I want them to understand what it does just by typing the name of the function into their netbeans.
August 28, 2008 at 2:44 pm
Lisp programmers do have a convention of using a trailing p to mean predicate, but I don’t think that they in general include tags to say what variables hold.
You have a static type system to keep track of your types. I suggest you stop duplicating its information - every unchecked duplication is scope for error.