Two Laptop Bag*
The moose likes Beginning Java and the fly likes Comments on the coding guide Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Comments on the coding guide" Watch "Comments on the coding guide" New topic
Author

Comments on the coding guide

Jim Venolia
Ranch Hand

Joined: Sep 07, 2013
Posts: 154
    
    2

This one, right here.

I spent most of the '90s as a consultant, learning new coding styles every 3-6 months. Most of them, like Where To Put The Braces (tm) are easy enough to adjust to. Want/don't want whitespace here and there? Fine. But certain things just burn my butt.

1) Never use do..while loops. Bzzzt. do..while loops have their place, and any decent editor makes it easy to find the closing brace to get what the end condition is. Your example is too simplistic to be realistic. A better one would be:



This is a maintenance nightmare. Sure, you could prolly put 'several lines of code' into it's own method, but oftentimes that's even less clear. Don't say "just set notDone to false and use a while loop", because you don't know what the done condition is, and IMHO adding phony booleans like notDone just to avoid a do..while loop is less clear.

2) Do not compound increment or decrement operators. Why not? These are basic and commonly used idioms, if you don't understand exactly what "foo(i++):" or "foo(--i);" do then I honestly don't want you mucking around in my code. I can guarantee you that if you don't grok foo(i++) then that will be the least of your problems. Same for the example of (y += 100 * y++). I can sorta see the reasoning behind this one (note the coding style uses x++, which is simpler). But IMHO this is again a common idiom that any programmer should understand.

2b) What's the problem with allowing i++ but not ++i? It depends on the algorithm. It's like saying you can only use a flathead screwdriver, never a phillips. Reasoning being you can use a flathead on a phillips screw.

I never saw the prohibitions above in any coding standard before. The one I always saw prohibited the ternary operator ('?'), usually because "it's too hard for beginners to understand". You're going to work on my 10 file, 3000 line program and you can't figure out how '?' works?

I agree 110% Hungarian Notation is Evil And Must Die. My favorite example is:

Hungarian Notation applied to English:
artThe adjHungarian nGovernment vHas vOrdered pnIts adjCivil nServants
infToSpend artThe adjSame nAmount prepOf nCash prepOn adjOpenSource nProjects
advAs pnThey vSpend prepOn adjProprietary nSoftware


/ spent way too much of my life in meetings hammering out coding standards
// usually arguing against one thing or another
/// Actually had 1 phb that insisted on no extra parens, rational being "it slows down the compiler". This was 2001 or so.
//// Not bad, only 3 nits with your coding standard




It's a no-brainer. We just need to take it to the next level to turn this into a win-win situation. The best practice is to get rid of the low-hanging fruit first. Ping me with an agenda so we can go flag up on this thing
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Jim Venolia wrote: . . . pnIts . . .
Surely its is an adjective there?

Most of us disagree with those parts of the style guide. They are only suggestions. The really importantBitsWhichGetOverlookedAreTheBitsAboutSeparatingTokensWithSpaces. And line lengths.
Jim Venolia
Ranch Hand

Joined: Sep 07, 2013
Posts: 154
    
    2

Campbell Ritchie wrote:
Jim Venolia wrote: . . . pnIts . . .
Surely its is an adjective there?

Most of us disagree with those parts of the style guide. They are only suggestions. The really importantBitsWhichGetOverlookedAreTheBitsAboutSeparatingTokensWithSpaces. And line lengths.


And tabs vs spaces. Was a tab lover until I wrote a voice mail system in Python, now I'm like a reformed smoker
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Jim Venolia wrote: . . . tabs vs spaces. . . . now I'm like a reformed smoker
Only 40 miles from here smokers smoke tabs

And read this
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10426
    
    8

I always love these type of discussions! In my experience people are often more passionate about the code style guidelines than the actual code itself. Please do carry on!
Now where did I leave my popcorn.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Robert D. Smith
Ranch Hand

Joined: Oct 04, 2013
Posts: 143
    
    5
Jim Venolia wrote:
Campbell Ritchie wrote:
Jim Venolia wrote: . . . pnIts . . .
Surely its is an adjective there?

Most of us disagree with those parts of the style guide. They are only suggestions. The really importantBitsWhichGetOverlookedAreTheBitsAboutSeparatingTokensWithSpaces. And line lengths.


And tabs vs spaces. Was a tab lover until I wrote a voice mail system in Python, now I'm like a reformed smoker

Do text editors that handle indentation for you, such as JEdit, use spaces or tabs? Probably depends on the settings of the individual editor. Guess I need to do some testing.


There are worse crimes than burning books. One of them is not reading them. Ray Bradbury
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Robert D. Smith wrote: . . .
Do text editors . . . use spaces or tabs? Probably depends on the settings of the individual editor. . . .
I usually use gedit, where there is an option to convert tabs to spaces. I think they usually have that as an preference which you have to select. It may differ if you paste code which already has tabs in.
Jim Venolia
Ranch Hand

Joined: Sep 07, 2013
Posts: 154
    
    2

I use vim, which has a setting to expand tabs to spaces.
 
GeeCON Prague 2014
 
subject: Comments on the coding guide