Welcome Guest.   Make a donation to an author on the site September 18, 2014, 10:48:24 PM  *

Please login or register.
Or did you miss your validation email?


Login with username and password (forgot your password?)
Why not become a lifetime supporting member of the site with a one-time donation of any amount? Your donation entitles you to a ton of additional benefits, including access to exclusive discounts and downloads, the ability to enter monthly free software drawings, and a single non-expiring license key for all of our programs.


You must sign up here before you can post and access some areas of the site. Registration is totally free and confidential.
 
Your Support Funds this Site: View the Supporter Yearbook.
   
   Forum Home   Thread Marks Chat! Downloads Search Login Register  
Pages: [1]   Go Down
  Reply  |  New Topic  |  Print  
Author Topic: Java and braces ... how do you keep track of them?  (Read 2716 times)
tsaint
Charter Member
***
Posts: 429


Hi from the a*** end of the earth

see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« on: August 17, 2010, 07:22:41 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

Logged
Shades
Member
**
Posts: 1,654


see users location on a map View Profile Give some DonationCredits to this forum member
« Reply #1 on: August 17, 2010, 07:34:01 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.
Logged
EĆ³in
Charter Member
***
Posts: 1,400


O'Callaghan

see users location on a map View Profile WWW Give some DonationCredits to this forum member
« Reply #2 on: August 18, 2010, 04:38:10 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;

Formatted for Java with the GeSHI Syntax Highlighter [copy or print]
  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.
Logged

Interviewer: Is there anything you don't like?
Bjarne Stroustrup: Marketing hype as a substitute for technical argument. Thoughtless adherence to dogma. Pride in ignorance.
tsaint
Charter Member
***
Posts: 429


Hi from the a*** end of the earth

see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #3 on: August 18, 2010, 05:07:06 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.
Logged
Deozaan
Charter Member
***
Posts: 6,345



see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #4 on: August 19, 2010, 03:17:04 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.
Logged

mwb1100
Supporting Member
**
Posts: 1,309


View Profile Give some DonationCredits to this forum member
« Reply #5 on: August 19, 2010, 12:44:50 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.
Logged
tsaint
Charter Member
***
Posts: 429


Hi from the a*** end of the earth

see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #6 on: August 19, 2010, 05:14:05 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
Logged
mwb1100
Supporting Member
**
Posts: 1,309


View Profile Give some DonationCredits to this forum member
« Reply #7 on: August 19, 2010, 05:59:20 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.
Logged
tsaint
Charter Member
***
Posts: 429


Hi from the a*** end of the earth

see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #8 on: August 20, 2010, 05:16:51 AM »

thanks for these useful ideas... that's great.
Logged
Renegade
Charter Member
***
Posts: 11,367



Tell me something you don't know...

see users location on a map View Profile WWW Give some DonationCredits to this forum member
« Reply #9 on: September 11, 2010, 08:12:44 PM »

Why not just use an IDE like Eclipse?
Logged

Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker
Pages: [1]   Go Up
  Reply  |  New Topic  |  Print  
 
Jump to:  
   Forum Home   Thread Marks Chat! Downloads Search Login Register  

DonationCoder.com | About Us
DonationCoder.com Forum | Powered by SMF
[ Page time: 0.08s | Server load: 0.13 ]