• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Coding conventions

 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Max,
Thanks for the excellent book. In it, you state that the coding conventions posted at Sun's javasite are "Use of and adherence to these standards are a required portion of the SCJD exam". This implies that minor deviations may be grounds for a quick failure. Can this possibly be true ? I cannot find anything in my instructions or at the Sun cert site that says this.
Consider, for instance, their statement in the coding convention document regarding indentation (note the use of the words "should" and "must"):
"Four spaces should be used as the unit of indentation. The exact construction of the indentation (spaces vs. tabs) is unspecified. Tabs must be set exactly every 8 spaces (not 4)."
Can anybody even make any sense out of this statement ?
In my own work, I have no intention of using spaces for indentation or setting tabs at 8 unless somebody forces me to.
It also seems that this document is pretty irrelevant as Sun's own engineers seem to ignore it.

kktec
SCJP 1.4 & SCWCD
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ken,

"Four spaces should be used as the unit of indentation. The exact construction of the indentation (spaces vs. tabs) is unspecified. Tabs must be set exactly every 8 spaces (not 4)."
Can anybody even make any sense out of this statement ?


This is telling you that the first indent will have 4 spaces. An indent from there may be 8 spaces or 1 tab.

Personally I love this. I have worked for years in C programming, where everyone has their own ideas of how many spaces to use for indentation, and how big tabs should be. The idea of changing tab sizes when not all editors support it is insane.
So Sun have dictated that the size of a tab will be 8 spaces. I view this the same way as their declaring that a short is 16 bits. It saves so much confusion.
But having each indent at 8 spaces is a bit too large, especially when you are trying to code using fully spelt out variable / method / class names. Setting the standard indent to 4 spaces is a decent size - easy to see the indent without loosing too much of your 80 columns. And you don't have to type 32 spaces if you have 8 indents - 4 tabs will do. Lovely.

In my own work, I have no intention of using spaces for indentation or setting tabs at 8 unless somebody forces me to.


In your own work that you are doing for yourself, you are free to set whatever standards you like.
But when working for an employer you frequently have to follow their standards, even if you dont agree with them.
It is easy to follow Sun's standards, and there are plenty of free Java applications you can run that will validate whether you are meeting Sun's standards or not, so why not follow their standard? (I use CheckStyle for this).
In the old assignment, 15% of the available marks were assigned to Coding Standards. This is not explicitly declared in the new assignment, but I would not be surprised to find that it is being considered. If you loose 20% you will fail. Do you really want to throw easy marks away?

It also seems that this document is pretty irrelevant as Sun's own engineers seem to ignore it.


The argument that someone else has gotten away with something does not make it OK for you to do it.
Also some of the code you see at the Sun site or download with your JVM may be:
  • written prior to the standard being published
  • written by someone external to Sun


  • By the way - there are also tools available to check your documentation also meets Sun standards. I use Sun's doccheck for that.
    Regards, Andrew
    [ August 30, 2003: Message edited by: Andrew Monkhouse ]
     
    Ken Krebs
    Ranch Hand
    Posts: 451
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Andrew,
    Thanks for your reply. I probably come off in my original posting as a guy who just wants to do it my own way. Actually, I'm not, I prefer to use a set of good, consistent standards. This is really good practice when working on any sort of project whether you're working alone or as part of a team.
    There are really 2 issues in my post:
    #1 How will deviation from the standards affect grading for the test ?
    #2 The quality of the coding conventions specified by Sun with regard to indentation.
    Regarding issue #1:
    - I do follow most of the recommendations in the standard. On most of the other parts of the assignment, you're graded not so much on your choices but as to the reasoning behind them and the consistency of how that reasoning is applied. Should this aspect really be much different ?
    - Does anybody who's submitted the test have any feedback on how closely they followed the indentation issue and whether they think it hurt them on the test if they didn't follow it precisely ?
    Regarding issue #2:
    Sun quote: "Four spaces should be used as the unit of indentation. The exact construction of the indentation (spaces vs. tabs) is unspecified. Tabs must be set exactly every 8 spaces (not 4)."
    Can anybody even make any sense out of this statement ?


    This is telling you that the first indent will have 4 spaces. An indent from there may be 8 spaces or 1 tab.


    I must be thick headed but I really don't see how you get this (i.e. the first indent is 4 spaces) out of that statement.
    If what you say is correct, this standard is really absurd, IMO:
    - I think that using spaces for indentation is a real waste of time and effort, after all aren't the standards supposed to make programmers more productive, not less. It's too much typing.
    - Arbitrarily mixing spaces and tabs is even worse. Why be inconsistent and waste time trying to be consistently inconsistent ?
    - Setting tabs at 8 while limiting line length to 80 makes for too much broken up code when used with good descriptive method and variable names. This hurts readability defeating the purpose of the standard.
    My own preference is probably the one that most programmers would use given a choice, always use tabs set at 4 for indentation. Would they really mark me down for this ?
    To enhance my productivity, I also want the identation to be easily enforceable by my editing tool, in my case it's currently Eclipse. You wouldn't happpen to know how to set this (i.e. 4 spaces first) up for Eclipse, would you ???
    kktec
    SCJP1.4 & SCWCD
     
    Andrew Monkhouse
    author and jackaroo
    Posts: 12200
    280
    Mac IntelliJ IDE Firefox Browser Oracle C++ Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Ken

    [Ken] Can anybody even make any sense out of [the above] statement ?
    [Andrew] This is telling you that the first indent will have 4 spaces. An indent from there may be 8 spaces or 1 tab.
    [Ken] I must be thick headed but I really don't see how you get this (i.e. the first indent is 4 spaces) out of that statement.


    Sun provide some examples on the page detailing indentation. In particular there is this one describing how to indent a wrapped if statment:

    As you can see, "doSomethingAboutIt() is indented four spaces from the start of the "if" statement.

    To enhance my productivity, I also want the identation to be easily enforceable by my editing tool, in my case it's currently Eclipse. You wouldn't happpen to know how to set this (i.e. 4 spaces first) up for Eclipse, would you ???


    Sorry, I don't use Eclipse, although I am thinking about giving it a go.
    I set up my editor to have tabs convert to 4 spaces. That way I could just hit tab and 4 spaces would be automatically inserted. There was not a single tab character in any code I submitted, and shift tab moved the line back through 4 spaces.
    Regards, Andrew
    reply
      Bookmark Topic Watch Topic
    • New Topic