Other Software > Developer's Corner
Java and braces ... how do you keep track of them?
tsaint:
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:
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:
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 ---public class TraverseDirectory { private static void processDir(File dir) { System.out.print( (dir.isDirectory() ? "[D] : " : "[F] : ")); System.out.println(dir); } private static void traverse(File dir) { processDir(dir); if (dir.isDirectory()) { String[] children = dir.list(); for (int i=0; i<children.length; i++) { traverse(new File(dir, children[i])); } } } /** * Sole entry point to the class and application. * @param args Array of String arguments. */ public static void main(String[] args) { traverse(new File("new_dir")); }}
This is, admittedly, slightly different to their code style.
tsaint:
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:
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.
Navigation
[0] Message Index
[#] Next page
Go to full version