Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Java: 'if' Statement - Indentation

JavaFAQ Home » Java Notes by Fred Swartz Go to all tips in Java Notes by Fred Swartz


Bookmark and Share

Java: 'if' Statement - Indentation

Indent to make programs readable

There are several meathods to make programs readable. How can you easily make the reader see which statements are inside the true part and false part of an if statement.

The best way to show this is to indent the statements that are inside. To do this you move the statements to the right by a few spaces. People commonly use two, three, or four spaces. Choose one number (eg, I use 2 or 3), and use it for all programs.

Java doesn't care about your indentation -- it is for humans (including yourself!).

Example 1 - No indentation - BAD BAD BAD

Here is the paintComponent() method from a previous page without indentation. This is small, so it's easy to see which statements are in the true and false parts. If the if statement is much larger, it will be unreadable without indentation.
   public void paintComponent(Graphics g) {
   super.paintComponent(g);
   if (marks < 50)
   g.setColor(Color.red);
   else
   g.setColor(Color.black);
   g.drawString("Score = " + marks, 10, 50);
   }

Example 2 - No indentation and no line breaks

Even a very short method is almost unreadable when you take out the line breaks and spaces. Here is the same method:
public void paintComponent(Graphics g) {super.paintComponent(g);if (marks<50) g.setColor(Color.red);else g.setColor(Color.black);g.drawString("Score = " + marks,10,50);}

'if' Statement - if inside if

if inside if

You can put an if statement inside another if statement.

Example -- series of tests

This code is correctly indented, but ugly and hard to read. It also can go very far to the right if there are many tests.
   if (score < 35)
      g.setColor(Color.magenta);
   else
      if (score < 50)
         g.setColor(Color.red);
      else
         if (score < 60)
            g.setColor(Color.orange);
         else 
            if (score < 80)
               g.setColor(Color.yellow);
            else
               g.setColor(Color.green);

Nearest 'else'

If you use braces, there is no problem with deciding which else goes with which if For example,
   if (age < 24) {
      if (height > 200) {
         c = Color.red;
      }
   }else{
      c = Color.blue;
   }
Because the true and false parts are both single statements, you might want to leave out the braces and write:
   if (age < 24) 
      if (height > 200) 
         c = Color.red;
   else                // DANGER: which 'if' goes with this 'else'
      c = Color.blue;
But this is WRONG, because 'else' always goes with the nearest 'if' when there are no braces. This code is the same as:
   if (age < 24) {
      if (height > 200) 
         c = Color.red;
      else
         c = Color.blue;
   }
These kinds of errors are very hard to find. This is another good reason to always use braces.
 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy