6

disclaimer: for simplicity sake, brackets will refer to brackets, braces, quotes, and parentheses in the couse of this question. Carry on.

When writing code, I usually type the beginning and end element first, and then go back and type the inner stuff. This gets to be a lot of backspacing, especially when doing something with many nested elements like:

jQuery(function($){$('#element[input="file"]').hover(function(){$(this).fadeOut();}));

Is there a more efficient way of remembering how many brackets you've got open ?

Or a second example with quotes:

<?php echo '<input value="'.$_POST['name'].'" />"; ?>
1
  • 4
    Let the IDE automatically put in the "ending" token when I type the "opening" one... Feb 21, 2011 at 20:01

9 Answers 9

23

I start out like this {}, then usually fill them with something. Whenever you type {, type a corresponding } and stick it on a new line. The worst thing you have to do in that case is fix indentation prior to committing.

Good syntax highlighters will often alert you to a problem, but not always. My preferred editor KATE, for instance, choked on a JSON formatted 'printf style' variadic argument string.

Don't trust bracket and paren highlighting! Always close what you open immediately after opening it and then fill in the gaps.

9
  • +1 For pointing out the fallibility of paren-matching, particularly with variadic functions and I find, mixed-mode code.
    – Orbling
    Jan 3, 2011 at 17:23
  • @Tim So you might write {} and then backspace and fill in {()} and then put the contents in?
    – JakeParis
    Jan 3, 2011 at 17:24
  • 1
    @JMC Creative - yes, almost always. If I open { or ( or even [, I close it on another line and the indent it accordingly. If I open too many, to the point that I indent more than three / four tab levels deep, I re-think what I'm doing.
    – Tim Post
    Jan 3, 2011 at 17:28
  • 1
    @Tim Post: Good point. Deep nesting in expressions is as much of an indicator of need for refactoring as deep nesting in control structures. Also, a lot of IDEs have an option to automatically add the closing delimiter after the cursor whenever you type an opener. I could never quite get used to it, though, and occasionally it got in my way, so I do this, too, instead.
    – Jon Purdy
    Jan 3, 2011 at 17:57
  • 1
    @Jon, what, no code folding? (ducks)
    – Tim Post
    Jan 3, 2011 at 18:42
34

Indentation

One-liners like that do introduce a bit of a hazard in terms of keeping the brackets correctly matched. Most decent editors will match the brackets visually to help, but it is easier to expand it.

(function($) {
    $('#element[input="file"]').hover(function() {
        $(this).fadeOut();
    }, function() {
        $(this).fadeIn();
    });
})(jQuery);

Note your example was incorrectly matched.

21
  • 5
    That's why python is great, it forces you to indent and all the programs look similar.
    – Will
    Jan 3, 2011 at 17:22
  • 2
    "incorrectly matched" .. Oh, I just did that to prove my point. :)
    – JakeParis
    Jan 3, 2011 at 17:23
  • 3
    @Will: Forcing you to adopt a particular style of indentation (and an inconsistent one at that) does not make a language great.
    – Josh K
    Jan 3, 2011 at 18:56
  • 4
    @Josh K yes it does. xkcd.com/353
    – Will
    Jan 3, 2011 at 19:08
  • 1
    @Will: A XKCD comic (while awesome) is not evidence to a language's benefits. I love Python, but if I could change one thing it would be the indentation scope.
    – Josh K
    Jan 3, 2011 at 19:12
6

Whenever I type an opening brace, my editor (Visual Studio with Visual Assist) inserts the closing one for me. Much easier...

1
  • Mine, Komodo Edit, does as well. That plus bracket-matching makes it a non-issue. Jan 25, 2011 at 7:22
4

Use a text editor that highlights matched parens as you type. A certain amount of count along the line, +1 for open, -1 for close, check you end back at zero will happen at some point, but very rarely if you have a good text editor.

Some command-line prompts probably do similar matched-paren-highlighting if you configure them for it, though my quick bash-in-konsole experiment didn't seem to do it by default.

In the past, I used to type each matched pair at once, then cursor back over the closing paren. Still do out of habit quite a bit. This sounds like what you describe, but there's no backspacing, only cursor-left into the parens, and cursor-right to escape back out.

3
  • 2
    I find a lot of editors default to inserting the matching paren/quote for you, often with some sort of automatic overwrite when typing the bracket after the content. If typing a long one-liner, then pre-typing the closing bracket is definitely a good idea.
    – Orbling
    Jan 3, 2011 at 17:29
  • 3
    If it types the closing bracket for me, I turn that off. Typing is too much a habit/reflex thing. With that option on, I'd end up with lots of duplicated brackets making a complete mess of my code for about 10 mins, then I'd probably smash the monitor with the keyboard.
    – user8709
    Jan 3, 2011 at 18:13
  • Me too. I end up in a right mess with automatic insertion switched on if I'm not paying enough attention. I'm not very pro my computer doing anything with my caret/focus automatically - I type fast, and flipping about with that causes consternation!
    – Orbling
    Jan 3, 2011 at 18:59
2

When i use an editor that does not close my braces/parenthesis automatically I create them both at the same time. Then move them to their ending location. This is convienent when you have a coding pattern that you have settled into. Even if your one lining it, creating opening and closing brackets at the same time wont hurt. I use the same practice with JavaScript, C#, and Ruby. Try it.

e.g.

Step 1

if(true)

Step 2

if(true){}

Step 3

if(true)
{
    //Put your code here
}
2
  • 2
    Argh at GNU style braces, and no space after if!
    – Orbling
    Jan 3, 2011 at 21:38
  • 2
    I believe GNU indents the braces by two?
    – Gauthier
    Feb 7, 2011 at 12:17
2

I almost never forget to close my braces. I guess there is a mixture at work.

  • I code in small pieces. I try to avoid methods longer than a screen. Use submethods instead.
  • Indentation helps, if I'm not sure, whether I have to close something
  • compiling. Yes - I use a language which warns me if I forgot a brace. But I get warnings seldom.
1

An editor with good syntax highlighting helps a lot.

I use Vim, for example, which by default (since 7.0, I think) highlights the matching paren/brace/bracket as you type or when the cursor is on one of them. It even highlights as an error when there are unmatched pairs.

If your editor does not do this, it is doing you a disservice. :)

0

If I miss it, the compiler will catch it. It's usually no big deal.

1
  • 1
    I've seen compilers spit out a huge load of junk from a missing parentheses or bracket, which took me hours to get to the bottom of the mess (caused errors in a later included file rather than the header the error was in). Prevention is the best cure in my mind. Feb 21, 2011 at 22:33
0

This is one reason I like non-C-type languages such as VB and Ruby, since they don't use curly brackets to mark blocks, or semicolons to end lines.

I know people tend to slag VB as "verbose", but I actually like the keywords it uses since the meaning is abundantly clear. I mean, it's easier to distinguish an "End If" line than it is to figure out if this curly brace ends a conditional, or a loop, or a function. Multiply this effect for nested blocks, and VB wins for scan-ability in my eyes.

Just putting my two cents out there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.