I know this
thread is a little over a year old, but I just recently got into Java. I'm constantly studying coding style and portability along with general good coding practices, algorithms, and performance tuning. I'm a long time C programmer, and mostly studied all algorithms and coding style in C.
However, Java syntax isn't very different from C or C++. When I read the coding standards on javaranch, I was amazed by some of the suggestions. For instance, "Don't use continue/break"?!? Continue and break are most definitely needed in loop constructs. Especially if you're thinking about performance! I'll openly admit, continue and break are highly misused at times, but they are still 100% necessary. The reasoning behind not using them at all was not reason enough to stop using them. If people are misusing continue/break, they seriously need to fall back to common coding. I'm talking about going all the way back to the "Basic programming" course!
do/while is also a needed construct. I think maybe the author behind the coding standards didn't fully understand the idea behind a do/while loop? In a do/while loop, you always want that loop to execute at least once. Now, this is great when you have a complex loop that depends on a complex condition, but you want it to execute at least one time BEFORE
testing the condition. In the situation the coding standards suggest, you must test the complex condition FIRST then go into your loop body, then RETEST that condition. If it's a highly complex condition that involves a good bit of time, you've just tested that condition twice! Wow, you better hope your boss or your application doesn't depend on time constraints or performance in this case! Not to mention since we took out the useful 'break', we can't even break out of this loop successfully in an attempt to save processor time and performance issues... We must complete the entire complex loop going through each check! Heaven forbid if we want to 'continue' back to the top of the loop to save time and performance in order to recheck that condition to 'break' out of the loop. You're stuck wasting time and performance with these standards!
Some may say that you have to sacrifice some performance in order to keep code readable and stable. Sure, I buy into this theory as well. However, do/while, continue, and break are 3 very well known and very easily understood areas in programming. You cannot cater to newbies when you program. If that was the case, we'd all be writing useless software that prints "Hello, World!"... The newbies are supposed to LEARN how to program, and they will. They will learn how to use a break, continue, and most definitely, do/while! If they have not learned any of these, we shouldn't cater to their lack of knowledge, but they should reevaluate their programming skills and peek inside of their books when they see a do/while...
Basically, code readability is important. It's a great thing to write code that is readable.
You should practice good style all of the time... However, it is absolutely absurd to remove needed keywords or constructs in order to achieve this. Even in the world of C/C++, goto is needed on very few occasions and can be written in a VERY readable manner. However, you have the die hard "I hate goto" subscribers out there. It's quite senseless to remove goto completely, when goto can save your life when it comes to performance and actually maintain good readability!
However, we're talking about Java, and I say, please DO use continue, break, and do/while loops. Just use them in the correct sense and moderation!
Put it this way, it's a good idea to assume that if you have about 10 or 20 reasons to 'break' out of a loop, you need to really rethink your code design. Same with continue. I generally only have use for a 'break' or 'continue' once or at the most twice in my loops. If I notice that I'm using it more, I redesign my algorithm! It's getting a bit crowded and more than likely, I'm overdoing the functions purpose anyway!
With do/while loops, think about the purpose of them. If you absolutely, under ANY CONDITION need to have a loop execute at least once, use do/while. That's why it's there, people! If you don't understand the purpose of a do/while, go back and read your books again! do/while is in almost every single high level programming language. If you can't grasp it's use or the idea behind it, you aren't coding correctly in the first place... because it's a common loop and I feel it's safe to bet that you are messing up in A LOT of other areas as well.
Another few points, I don't agree with the 120 character line size... That's WAY too much. I feel as if the person that wrote this is definitely a windows programmer that always uses very large IDE's... You must think outside of your box when you're writing standards for style... You can't just think "well, everyone uses a GUI text editor..." Not everyone does! Us Unix folks use VI. Which, when used from a terminal on a 1024x768 screen, isn't going to enjoy a 120 line piece of code! A good rule of thumb here is to not go over 90 for your line buffer. 80 is the STANDARD. On a common terminal, 80 characters is the width. So if I have to telnet or ssh into your box and use a terminal based text editor, I'm screwed with your 120 character lines... Talk about ugly code!
Also, let me mention in this long winded reply, that method design isn't really covered well. Another good general rule is method size. I maintain a lot of code here at work, and I see method/functions spanning up to 9,000 lines!!! No, I'm not kidding. I had to break up a function in C that spanned 9,010 lines in a file. That is absolutely 100% improper, unreadable, unnecessary, ugly, stupid, and more than likely worthless. For myself, I usually consider that if I'm going over 100 lines in a single method/function, I'm starting to lose the purpose of that method/function. I've written method that have been up to 120 lines, but even after I wrote it, I went back over my code and made sure that I couldn't break up that method into maybe one other method... I think a method over 200 lines can always be broken up into smaller more manageable methods.
A method should focus on ONE purpose and ONLY one purpose. For instance, the method
shouldn't calculate how long it takes to get to the moon and back and how old your grandmother was when she got her first kiss and then return X, it should simply do what it has to do to get X and return it! Usually, if your methods are exceptionally long, you should be breaking that single method up into more methods that have a single purpose. The exception to this rule is a method that uses a switch() construct. Switch constructs can get long, but are highly readable...
Another good way to tell if you need to break up your methods is to count the number of variables you're declaring and using. If it's over 8, it's a good idea to start to think about your code. That 9,010 line function that I rewrote had about 50 defined variables in it... That's just silly.
Anyway, I'll be quite happy to give more suggestions. I spend A LOT of time studying these areas of programming since I do a lot of code maintaining at my job... Algorithms, Style, Performance, and Portability are my specialties!! I can suggest good books on it, and also give pointers... If you disagree with my style, and have a good reason, I always love to hear the reasons... I'm quite open to changing my opinion, if someone can prove my ideas obsolete!
Thanks for reading this long response that is a year late!
[ July 21, 2003: Message edited by: Geekoid Programmer ]