• Post Reply Bookmark Topic Watch 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Case For Good Formatting

Good formatting of program code is not just something you do at the end of a project to make your code pretty; it is an essential part of development.  Good formatting:
  • helps you avoid some kinds of bugs, especially brace mismatch bugs
  • makes the code much easier to read, for you and for the next developer who looks at it
  • helps make the code easier to maintain and modify

  • Indentation And Style

    There are many ideas on style, on how much to indent, and whether to use spaces or tabs.  The important thing is to find a commonly used style and stick with it.  You may have requirements from your work or school; you may have to use different styles over your career.  We're going to concentrate on the styles commonly used with Java.  (I'll be using the word "brace" for what is sometimes called curly brackets.)
    The things to notice are 1) the opening brace is at the end of the first line of the block, 2) every following statement is indented the same amount within the block, and 3) the closing brace is on a line of its own, "outdented" back to the same level as the first line of the block.  Braces are mandatory, even in places where Java syntax would allow to leave them off.

    Indentation is created with spaces or tabs.  Four spaces or one tab per indentation level is standard, although Google and other companies use two spaces.  The argument against tabs is that they are sometimes rendered as eight spaces.  If you use tabs, turn on the feature to convert them to spaces.  Don't mix tabs and spaces.

    There is a variation of the above style that is sometimes used.
    The only difference is in this style, all braces are on their own line.  Braces of the same block line up.  Proponents of this style say it's easier to see the blocks.

    How To Format As You Code

    Many text editors and IDEs help you to format your code correctly.  Usually pressing tab will indent one level and shift-tab will "outdent" one level.  Pressing <enter> at the end of a line will indent the next line at the same level as the previous line.

    There is a technique that you can use to help you keep your indentation straight.  When you start a block, immediately type a closing brace without anything between the brace.  Then enter all the code for that block inside the braces.  So for instance, if you were creating a class with a main() method, first you would write this:
    and then this
    and so on.

    Using An IDE To Format

    All major IDEs have tools to help you format your code.

    IDEKey Sequence
    EclipseCtrl-Shift-F
    IntelliJ IDEACtrl-Alt-L
    NetbeansAlt-Shift-F


    [autolink: HowToFormatCode]
     
    I want my playground back. Here, I'll give you this tiny ad for it:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
      Bookmark Topic Watch Topic
    • New Topic