• 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

Why Indent?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kevin,
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.)
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
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

http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for the very helpful info. I am learning new things everyday.

Thanks agian

Kevin
 
reply
    Bookmark Topic Watch Topic
  • New Topic