This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Why Indent? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why Indent?" Watch "Why Indent?" New topic

Why Indent?

Kevin Lamb

Joined: Dec 12, 2004
Posts: 3
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.
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32628

Welcome to JavaRanch!

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.)

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
marc weber

Joined: Aug 31, 2004
Posts: 11343

"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
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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!)

Seriously, pick what makes sense to you. Cheers!
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
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.
Kevin Lamb

Joined: Dec 12, 2004
Posts: 3
Thank you all for the very helpful info. I am learning new things everyday.

Thanks agian

I agree. Here's the link:
subject: Why Indent?
It's not a secret anymore!