aspose file tools*
The moose likes Beginning Java and the fly likes A quetsion on indentation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A quetsion on indentation" Watch "A quetsion on indentation" New topic
Author

A quetsion on indentation

Daniel .J.Hyslop
Ranch Hand

Joined: May 23, 2005
Posts: 55
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?


an island in the sun <br />with a language of many tongue?
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

http://en.wikipedia.org/wiki/Indent_style
Jeff Pollet
Ranch Hand

Joined: Jul 20, 2005
Posts: 49
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?


newbie, please be gentle.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9043
    
  10
http://www.coderanch.com/t/395597/java/java/Java-Style
http://www.coderanch.com/t/390346/java/java/inconsistency-style-code
http://www.coderanch.com/t/394100/java/java/Coding-Style-Standards
http://www.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.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

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...
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


  • "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    sscce.org
    Steve Morrow
    Ranch Hand

    Joined: May 22, 2003
    Posts: 657

    * 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

    Joined: Aug 31, 2004
    Posts: 11343

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

    I'll just say: No argument here.
    Stephen Boston
    Ranch Hand

    Joined: Jul 14, 2005
    Posts: 165
    Originally posted by marc weber:

    I'll just say: No argument here.


    I guess I'm a heratic then.


    Steve<br /> <br />No matter where you go, there you are.<br /> <br />"My evil self is at the door, and I have no power to stop it."
    Rick O'Shay
    Ranch Hand

    Joined: Sep 19, 2004
    Posts: 531
    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 ]
    Rick Portugal
    Ranch Hand

    Joined: Dec 17, 2002
    Posts: 243
    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.


    IBM 286, SCJP, SCWCD, EIEIO
    Ernest Friedman-Hill
    author and iconoclast
    Marshal

    Joined: Jul 08, 2003
    Posts: 24168
        
      30

    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.


    [Jess in Action][AskingGoodQuestions]
    Gregg Bolinger
    GenRocket Founder
    Ranch Hand

    Joined: Jul 11, 2001
    Posts: 15292
        
        6

    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.


    GenRocket - Experts at Building Test Data
    Steve Morrow
    Ranch Hand

    Joined: May 22, 2003
    Posts: 657

    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

    Joined: Jul 20, 2005
    Posts: 49
    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

    Joined: Jul 22, 2000
    Posts: 9043
        
      10
    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

    Joined: Jul 22, 2000
    Posts: 9043
        
      10
    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
    Marshal

    Joined: Jul 08, 2003
    Posts: 24168
        
      30

    Berkeley Software Distribution, as in BSD UNIX.
    Jim Yingst
    Wanderer
    Sheriff

    Joined: Jan 30, 2000
    Posts: 18671
    [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.


    "I'm not back." - Bill Harding, Twister
    Marilyn de Queiroz
    Sheriff

    Joined: Jul 22, 2000
    Posts: 9043
        
      10
    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 ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: A quetsion on indentation
     
    Similar Threads
    Calling main method
    Question about notation I never learned
    Why take code out of Main?
    concat 2 strings using constructor
    main method signature..