Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comments on the coding guide

 
Jim Venolia
Ranch Hand
Posts: 242
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 242
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Robert D. Smith
Ranch Hand
Posts: 212
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 242
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use vim, which has a setting to expand tabs to spaces.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic