topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday December 12, 2024, 10:20 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Java and braces ... how do you keep track of them?  (Read 7338 times)

tsaint

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 497
  • Hi from the a*** end of the earth
    • View Profile
    • Read more about this member.
    • Donate to Member
Java and braces ... how do you keep track of them?
« on: August 17, 2010, 07:22 PM »
 As a beginner in java programming, one of my biggest hassles seems to be trying to keep track of pairs of braces, especially now I've gone past classes of 1 screenful of code.
I'm using Geany (Linux, text editor) - I'm aware of IDEs, but my main point is just techniques/suggestions from experienced coders to save me having to wait to see compile errors which indicate I've stuffed up the braces yet again
thanks
tony


Shades

  • Member
  • Joined in 2006
  • **
  • Posts: 2,939
    • View Profile
    • Donate to Member
Re: Java and braces ... how do you keep track of them?
« Reply #1 on: August 17, 2010, 07:34 PM »
A text editor that supports folding also shows (relatively) quickly if there is either an excess or a shortage on braces.

On Windows the big name (freeware) text editors support this functionality and I have to assume that Linux (with their mantra: everything is a text file) has several text editors available that support this as well.

Eóin

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 1,401
    • View Profile
    • Donate to Member
Re: Java and braces ... how do you keep track of them?
« Reply #2 on: August 18, 2010, 04:38 AM »
I personally use indentation, all code inside a brace is indented an extra level. Also I tend to never put braces at the end of a line, by starting them on a new line they themselves line up.

Finally, I break up the majority of my functions, only a rare few exceed 1 sceenful of code.

For example I would write this Traversing a Directory example as the following;

Code: Java [Select]
  1. public class TraverseDirectory
  2. {
  3.     private static void processDir(File dir)
  4.     {
  5.         System.out.print( (dir.isDirectory() ? "[D] : " : "[F] : "));
  6.         System.out.println(dir);
  7.     }
  8.  
  9.     private static void traverse(File dir)
  10.     {
  11.         processDir(dir);
  12.  
  13.         if (dir.isDirectory())
  14.         {
  15.             String[] children = dir.list();
  16.             for (int i=0; i<children.length; i++)
  17.             {
  18.                 traverse(new File(dir, children[i]));
  19.             }
  20.         }
  21.     }
  22.  
  23.  
  24.     /**
  25.      * Sole entry point to the class and application.
  26.      * @param args Array of String arguments.
  27.      */
  28.     public static void main(String[] args)
  29.     {
  30.         traverse(new File("new_dir"));
  31.     }
  32. }

This is, admittedly, slightly different to their code style.

tsaint

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 497
  • Hi from the a*** end of the earth
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: Java and braces ... how do you keep track of them?
« Reply #3 on: August 18, 2010, 05:07 AM »
thanks for both suggestions... I've been less than fastidious about the amount of indenting, so thats a wake up call.
And yes, the text editor I'm using has folding - stupid not to use it.
Thanks for both suggestions.

Deozaan

  • Charter Member
  • Joined in 2006
  • ***
  • Points: 1
  • Posts: 9,778
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: Java and braces ... how do you keep track of them?
« Reply #4 on: August 19, 2010, 03:17 AM »
Indentions is a good way. Code folding works as well. But having an IDE with syntax highlighting (including syntax errors) is probably the best I can think of.

mwb1100

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,645
    • View Profile
    • Donate to Member
Re: Java and braces ... how do you keep track of them?
« Reply #5 on: August 19, 2010, 12:44 PM »
I'm using Geany (Linux, text editor)

Geany highlights matching braces when your cursor is next to one, which should be some help.  It also has options to "auto-close" certain constructs (including 'curly brackets'). That might be helpful or irritating, depending on your preferences.

tsaint

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 497
  • Hi from the a*** end of the earth
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: Java and braces ... how do you keep track of them?
« Reply #6 on: August 19, 2010, 05:14 PM »
The problem I've been having is that it doesnt do me much good to see one red brace when I need to do a lot of scrolling to see its match.
So I'm thinking it's the folding which is the key to being able then to see the matches, either with careful indentation else the brace highlighting

mwb1100

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,645
    • View Profile
    • Donate to Member
Re: Java and braces ... how do you keep track of them?
« Reply #7 on: August 19, 2010, 05:59 PM »
it doesnt do me much good to see one red brace when I need to do a lot of scrolling to see its match.

Of course - I should have also mentioned the "jump to matching brace" command (defaults to Ctrl-b) in geany, which again can help a lot.

Geany also has 'indentation guides' you can turn on - but they don't act on the language syntax, just the physical indenting in the document. However, they can help you notice when something isn't right.

tsaint

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 497
  • Hi from the a*** end of the earth
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: Java and braces ... how do you keep track of them?
« Reply #8 on: August 20, 2010, 05:16 AM »
thanks for these useful ideas... that's great.

Renegade

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,291
  • Tell me something you don't know...
    • View Profile
    • Renegade Minds
    • Donate to Member
Re: Java and braces ... how do you keep track of them?
« Reply #9 on: September 11, 2010, 08:12 PM »
Why not just use an IDE like Eclipse?
Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker