• 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

A quetsion on indentation

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When writing a program as a biginner in java and as a student of java we are all prompted to use a certain style ie;


and yet in most examples I see of the SCJP i see

why the difference in techniques?
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://en.wikipedia.org/wiki/Indent_style
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't have an answer to this question. But I'm wondering if Steve could explain how the wiki link answers your question. The wiki link describes different styles and explains a bit about the history of them, but doesn't answer the specific question of why, when learning Java, books often use one indent structure, but on the SCJP, a different indent structure is used.

Is the answer (inferred from the wiki link) simply 'there are different styles, and each is appropriate, depending on preferences?
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
https://coderanch.com/t/395597/java/java/Java-Style
https://coderanch.com/t/390346/java/java/inconsistency-style-code
https://coderanch.com/t/394100/java/java/Coding-Style-Standards
https://coderanch.com/t/394982/java/java/experienced-replies-only-coding-standards
Just a few I found in this forum of the probably hundreds of times this question has been asked on JavaRanch.

In fact, there is currently a thread in the Cattle Drive forum about this very subject.
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

why, when learning Java, books often use one indent structure, but on the SCJP, a different indent structure is used.

There are various reasons for choosing an indent style, including (but not limited to) readability, publishing costs, standard conventions, etc.

Is the answer (inferred from the wiki link) simply 'there are different styles, and each is appropriate, depending on preferences?

That's the inference I would have hoped you got from the article.

Regards...
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you can see from the links above, many developers (myself included) are strangely passionate about one style over another. But despite all the arguments as to why one style is "obviously superior" to another, it primarily comes down to what people are accustomed to.

So the style used in a particular context (e.g., a certain textbook or a certification exam) depends largely on the backgrounds of those who wrote the material, and this is why the history provided by the wiki article might be interesting.

My advice is:
  • Use one, but get comfortable with both (because you're not going to be able to avoid the one you don't like).
  • When it comes to arguments regarding which is better, don't engage.
  •  
    Steve Morrow
    Ranch Hand
    Posts: 657
    Spring VI Editor Clojure
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    * When it comes to arguments regarding which is better, don't engage.


    Bah! 1TBS! Anything else is heresy!

    *runs off to storm the castle*




    P.S. Just kidding, of course...
     
    marc weber
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Steve Morrow:
    ... 1TBS! Anything else is heresy!


    I'll just say: No argument here.
     
    Ranch Hand
    Posts: 165
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by marc weber:

    I'll just say: No argument here.



    I guess I'm a heratic then.
     
    Ranch Hand
    Posts: 531
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Curley's have been around for decades and one of two flavors have dominated. One in particular has the added benefit of being able to fit more code on a smaller surface so it is ideal for books, mags and small screens.

    if(condition) {
    ...
    }


    You can trace this form back to B language, the precursor to C language:

    http://cm.bell-labs.com/cm/cs/who/dmr/kbman.html

    Later it became known as K&R style for Kernighan and Ritchie, co-authors of a wildly popular book on the C Language. Ritchie actually designed C and he based it on his B language, which he based on BCPL, which is why C++ should've been P language

    You cannot go wrong with that style. However, I never use it. I don't like it. I'm not interested in saving space. I use what's commonly known as the BSD style. Given the choice between K&R and the correct style, I choose to use the correct style. Namely, BSD. After all, why use the wrong style? IMO it is un-American to use anything but this style:


    [ September 02, 2005: Message edited by: Rick O'Shay ]
     
    Ranch Hand
    Posts: 243
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    BSD Style seems more sensible to me, but every place I've ever worked uses K&R style. As a practical matter, use only K&R style. You can't fight City Hall.
     
    author and iconoclast
    Posts: 24207
    46
    Mac OS X Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I can't say why SCJP folks tend to use the BSD style, but I know why so much Java literature uses K&R: because that's the style that all the APIs were originally written in, and all the example code. Virtually all of the core APIs are still formatted this way.

    Some of the Eclipse platform libraries use what I feel is a horribly ugly style: K&R inside of methods, but all method headers butted up against the left margin -- i.e., no indentation inside of class bodies! This gets my vote for the worst indent style for Java.
     
    Ranch Hand
    Posts: 15304
    6
    Mac OS X IntelliJ IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ritchie actually designed C and he based it on his B language, which he based on BCPL, which is why C++ should've been P language

    Hmm, that is interesting even said in joking.
     
    Steve Morrow
    Ranch Hand
    Posts: 657
    Spring VI Editor Clojure
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    This gets my vote for the worst indent style for Java.

    I don't know; I've seen worse "styles" than that (sounds like the old Visual Age way of doing it)...

    As a side note, Sun's Code Conventions for the Java� Programming Language recommend K&R, so that may have something to do with it's apparent pervasiveness in Java.
     
    Jeff Pollet
    Ranch Hand
    Posts: 49
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Marilyn--

    Actually, the links you provided from doing searches of this forum answer the more general question: "Why are there different styles?"

    Daniel's question, and this was part of my point before, was specifically about the style found in most books vs. the style found on the SCJP. And sure enough, there are some good answers to this, aside from the more general answers you and Steve provided--people provided them. These two, for instance, seem to be good answers to Daniel's question:

    "One in particular has the added benefit of being able to fit more code on a smaller surface so it is ideal for books, mags and small screens."

    Thank you, Rick.

    "Virtually all of the core APIs are still formatted this way."

    Thank you, Ernest.

    Seems to me that both Marilyn and Steve have seen the 'what's the *correct* style?' question so many times that they misread at least part of what Daniel was asking. Yes, there is not one correct style, and one could have figured that out by doing some searches/research--who knows, Daniel very well may have. But that doesn't answer the question posed: Namely, why do so many books have it one way--when you're supposed to be learning from them how to pass a test, but the test materials have it another way? I think it's a good freakin' question, one that deserves a better answer (if one is so inclined to answer at all) than 'look at all of the different styles, and pick one that is right for you'.

    I know that people get frustrated when questions are asked a million times--heck, I've only been reading this forum for a few months and *I* get frustrated when somebody comes on asking people to do their homework, or how to set the path. But if I had asked Daniel's question I would have liked anybody putting the smackdown on me (i.e. "Just a few I found in this forum of the probably hundreds of times this question has been asked on JavaRanch) to have read my question with at least something like 'benefit of the doubt' in mind before chastising me. It makes those of us newbies who *are* trying very hard to follow the etiquitte here reluctant to ask questions, even after we have done lots of research and lots of searches.
     
    Marilyn de Queiroz
    Sheriff
    Posts: 9109
    12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    SCJP does not require a particular style of indentation/braces.
    SCJD requires that you follow the Sun Style Guide which includes the K&R style curly braces.

    Many times it is not the developer that decides which style to use or which they prefer, but it is the employer that makes that decision.
    [ September 02, 2005: Message edited by: Marilyn de Queiroz ]
     
    Marilyn de Queiroz
    Sheriff
    Posts: 9109
    12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Jeff Pollet:
    Marilyn--

    Daniel's question, and this was part of my point before, was specifically about the style found in most books vs. the style found on the SCJP.

    But if I had asked Daniel's question I would have liked anybody putting the smackdown on me (i.e. "Just a few I found in this forum of the probably hundreds of times this question has been asked on JavaRanch) to have read my question with at least something like 'benefit of the doubt' in mind before chastising me. It makes those of us newbies who *are* trying very hard to follow the etiquitte here reluctant to ask questions, even after we have done lots of research and lots of searches.



    It wasn't meant as a "smackdown". I'm sorry I phrased it in such a way that you could read it as such. It was a hurried answer (especially since I had just answered a very similar question in the Cattle Drive forum) -- not an excuse, but sort of an explanation.

    Re: the style in most books.
    I'd guess that the books were trying to save space.
    Sun is becoming more vocal about promoting the K&R style as the Java Style of coding.

    Re: vs. the SCJP
    The SCJP exam is written in a style that mainly tries to fit the question into a very small space and it doesn't really follow either the K&B style or the other style either.

    Re: the BSD style (I don't know what BSD stands for. Perhaps Rick or someone else can enlighten us)
    I personally prefer it because:
    a) It was the first style I learned
    b) It is part of the JavaRanch Style Guide which it is my duty to enforce on Cattle Drive students.
    c) I think it is more readable to have matching curly braces in the same column.
    d) I think it is easier to debug.
    e) It was required by my employer, so I got used to seeing it that way.
    [ September 02, 2005: Message edited by: Marilyn de Queiroz ]
     
    Ernest Friedman-Hill
    author and iconoclast
    Posts: 24207
    46
    Mac OS X Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Berkeley Software Distribution, as in BSD UNIX.
     
    Wanderer
    Posts: 18671
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    [Daniel]: When writing a program as a biginner in java and as a student of java we are all prompted to use a certain style

    I've got to disagree right at the outset. We aren't all prompted to use the same style at all. It depends on where (or whom) you learn Java from.

    and yet in most examples I see of the SCJP i see [...]

    why the difference in techniques?


    As it happens I just addressed this in this thread. Basically, SCJP exam questions are limited in the number of lines they can take up, and in order to fit the code into that space, it was sometimes (often) necessary to eliminate vertical whitespece wherever it was deemed least necessary. Generally K&R style was the starting point, and further vertical compacting of the code was performed as necessary. The results were often not pretty, but should be readable to anyone with just a bit of practice. Code in the real world can have all sorts of different styles after all, and in my opinion any professional programmer should be able to read a wide range of diffferent styles, regardless of what they prefer.

    [Jeff]: Is the answer (inferred from the wiki link) simply 'there are different styles, and each is appropriate, depending on preferences?

    Sounds good to me.

    [Rick O'Shay]: You cannot go wrong with that style.

    Sure you can - if it contradicts the style of your current employer.

    [EFH]: I can't say why SCJP folks tend to use the BSD style

    I would think not. Especially since they don't. At least, the example code show at the beginning of this thread would seem to claim that SCJP code is, well, closer to K&R than to BSD. Some authors of SCJP books do indeed prefer BSD - but I think that's roughly comparable to the split among the rest of the book-writing community. Depends on the authors' preference more than anything else. The actual SCJP rarely uses BSD, and often uses a much more compacted style than K&R would have.

    Actually, the links you provided from doing searches of this forum answer the more general question: "Why are there different styles?"

    Actually if you read them, a couple of the threads do address the same point Rick O'Shay made. (I don't recall seeing specific mention of EFH's point.) As well as many other points which weren't directly part of the original question, true. I took Marilyn's post as a sort of "here are some links to additional info" rather than "here are the links that you should have read in the first place before asking this question".

    [MdQ]: Sun is becoming more vocal about promoting the K&R style as the Java Style of coding.

    Have there been recent changes in this area? I know that Sun coding standard started as a sample coding standard which was used by one particular group at Sun. At some point (1999?) Sun changed this to "This document reflects the Java language coding standards presented in the Java Language Specification". Still, that was a while ago. Have there been other recent changes?

    [MdQ]:

    SCJP does not require a particular style of indentation/braces.
    SCJD requires that you follow the Sun Style Guide which includes the K&R style curly braces.


    Yup, very true.
     
    Marilyn de Queiroz
    Sheriff
    Posts: 9109
    12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I first came into contact with the Sun Style Guide in 2001. But it seems that only in the last two or three years I've been hearing from some people that it is supposed to be "the" Java style guide.
    [ September 03, 2005: Message edited by: Marilyn de Queiroz ]
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic