Okay guys. Brand new to Java, well brand new to any type of programming. I am having trouble understanding the need to indent, and how to know when, and were to indent. I also am having trouble understanding why the closing brackets } will be shown way at the bottom of a page instead of at the end of a string. Thank you for any, and all advice. Kevin
Indenting is necessary because it makes the code easier to read. Suppose I have the following two code snippets:
In (a), it is obvious that do stuff only occurs when i is 0. In (b), it takes longer to figure that out. The impact of this becomes larger for bigger chunks of code. So everybody indents to make life easier.
Basically, you indent everytime there is some control structure, such as a method, loop or conditional. (If you don't know what these are yet, you'll learn soon enough.) The JavaRanch style guide also gives some tips.
Closing brackets end a method/loop/conditional. In small programs, they are at the bottom of the page because that is where everything stops. In larger programs, they can appear earlier. (Quotes are the symbol at the end of a string.)
I like having an editor that can fix up the indenting. It's easy to indent properly but code wrong:
The indenting looks right, but we need curly braces to put "a++" and "b++" in the same block. If you're using Eclipse, hit Ctrl-Shift-F or right-click and select format. If you're using an editor that doesn't know Java, like NotePad, remember to double check your indenting carefully!
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
"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
Joined: Jan 29, 2003
BTW: In the eternal battle over curly braces, I come down solidly for putting opening braces on their own line. There is no "right" answer; be prepared to conform to team standards at work or ranch standards if you join the cattle drive.
Ok, there is a right answer: put em on new lines. The only two reasons for K&R opening brace on the end of a line are "We always did that in C" (who cares!) and "It saves vertical space" (your method is too long!)
Saving vertical space has nothing to do with a method being too long. Have you ever done an action form for a JSP that has maybe 10 - 20 form fields? You have to make a getter and setter for each field. Suddenly you're taking about 20 - 40 methods in an action form (each method maybe 2-3 lines of code). Well putting the open curly braces on the same line can save a lot of space and scrolling, especially over the entire web project.
I prefer putting the opening curly brackets on the same line as the method signature.
Not that it really matters, just offering a different perspective.
Joined: Dec 12, 2004
Thank you all for the very helpful info. I am learning new things everyday.