Do you sometimes skip braces?

This isn’t a thread about if it’s a good/bad idea to skip braces, I just want to know if you personally do it, and if you do when?

I normally skip braces in loops if I don’t need them, especially if it’s two loops, a if statement, and then a method. Since I don’t think having three end braces in a row improve readability of the code. But I never skip braces if I need to use a else statement, since I think that makes it more difficult to read the code.

I know it’s a bad practice, but I do skip braces sometimes. This is from my SceneCollider class.


@@for (Class<? extends Entity2D> class1 : collisionMap.keySet())
@@    for (Entity2D entity : entities)
        if (class1.isInstance(entity))
        {
            List<Entity2D> collidables = tree.retrieve(entity);

@@            for (Entity2D entity2 : collidables)
@@                if (collisionMap.get(class1).isInstance(entity2) && entity != entity2)
                    // Check collision
@@                    if (entity.getPolygon().intersects(entity2.getPolygon()))
                        entity.collision(entity2);
        }

I do even have comments, and it is totally fine. Never got into syntax errors due to this.

If there is more than one statement total inside the block or nested blocks, I don’t skip the brace.


for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
@@        if (condition)
@@            for (int k = 0; k < 10; k++)
                System.out.println("Stuff");
        else {
            System.out.println("Other");
            System.out.println("Stuff");
        }
    }
}

I also often skip braces (obviously only when it’s allowed by single statement blocks).

if (number < 100)
      number %= 50;
else {
      number %= 300;
      System.out.println("Number");
}

I dont ever skip them except in a case like:

if (number < 100) number %= 50;

It’s a very difficult job trying to prevent myself from confusing myself from confusing

Never.

Usually if I need a simple if-else statement I do things like this.


//example 1
return map.contain(key) ? map.get(key) : null;

//example 2
bgColor = bgColor == Color.gray ? Color.white : Color.gray;

That way I skip large if statements, and to me that’s perfectly readable.

I always skip them as much as I’m allowed to do it! ;D
Not only in for () loop cases, but also for everything else like while (), do/while (), if (), else if () and else.
Of course I’m pretty aware that else keyword refers to the most immediate if (). So no problems when careful! ::slight_smile:
As many modern languages (Python, Ruby, CoffeeScript, etc.) have already proven, indentation is enough to both visually & programmably determine scope. 8)

No, never.

I skip braces for loops and conditions, but I am not doing it, if there is an else block. Also I don’t do single line ifs and fors anymore, because it got in the way when placing breakpoints during debugging.

Never, ever. Eclipse is set to add them automatically if I forget.

Cas :slight_smile:

I do it only with if loops just for code readability but with other loops skipping braces makes me confusing and I finish with a new bug caused by that statement. :stuck_out_tongue: :clue: :yawn:

I never skip braces, I find it more unclear to read , in any circumstances where it would be horrendously complex where it can be avoided such as a big ass if statement I will nest them instead for instance if(a == b && b == c && c == d && d == (5&s)*r) (exaggerated of course) I would do if(a == b && b == c && c == d){if(d == (5&s) * r)
Simplifies it a little bit. An actual example from my collision software is where I isolate x checking and y checking of the coordinates.

Whenever it seems easier to understand what I’m writing.

I usually try to skip braces on control flow with a single line statement. However, in the case of if-else, and similiar, they both have braces, or both must not have braces.

Ultimately it comes down to what I find more appealing and readable when looked at, that comes before rules for me.

Relatedly: can anyone quickly explain how to auto-format code on the server when committing to SVN? Lazy I know but a Google search won’t elicit an actual explanation in a nicely recorded thread on JGO…

Cas :slight_smile:

i use jindent for years. it’s not free but very powerful. comes with integration plugins for netbeans, eclipse or intellij - and a cli interface which would allow you to hook it in. i know git would be easy, not sure about svn.

http://www.newforms-tech.com/products/jindent/about

Correction: if there is more than one statement total inside the block, you can’t skip the brace.

That’s why skipping the braces is such a bad idea to start with. Down the road you “might” (read: will) need to add statements to that block, and you “might” (read: will) forget to add the braces.

I don’t do it often, however I early exit a lot to prevent nested blocks:



// Normally
if(!isDead())
{

// Do all this code

}

// Exit early
if(IsDead()) return;

// Skip all this code

I do it quite often if I want to pretty much just get out of the method, so rather than having a bunch of conditional statements inside the method that check for true/false and execute one after another, I just switch the logic around and exit early.

Proper example (not Java but still, shows what I mean):


  public override void MousePressed(InputHandler.MouseButton button, int x, int y)
        {
            // If there is no entity to place and no layer, exit early
            if (toPlace == null || layer == null || placed) return;

            // If the left button is not pressed, exit early
            if(button != InputHandler.MouseButton.LEFT) return;

            // Check if there is already something in the cell
            if (layer.GetCellAt(xx, yy).GetOccupant<Entity>() != null) return;

            // Place the object in the cell
            layer.GetCellAt(xx, yy).SetOccupant(toPlace);

            Sprite s;
            if (toPlace.TryGetComponent(out s))
                subject.Notify("placedEntity", s);

            // It has been placed, prevent any further input
            placed = true;

        }

Kind of avoided braces right?

Looks a lot nicer than:


  public override void MousePressed(InputHandler.MouseButton button, int x, int y)
        {

            if (toPlace != null && layer != null && !placed){
                if(button == InputHandler.MouseButton.LEFT){
                    // Check if there is already something in the cell
                    if (layer.GetCellAt(xx, yy).GetOccupant<Entity>() == null){
                    // Place the object in the cell
                    layer.GetCellAt(xx, yy).SetOccupant(toPlace);
                    Sprite s;
                    // I avoid braces here as I know NOTHING else will ever be called here
                    if (toPlace.TryGetComponent(out s))
                        subject.Notify("placedEntity", s);

                    // It has been placed, prevent any further input
                    placed = true;
                    }
                }
            }
        }

Never, never, never, never, … and never! ;D IMO, shouldn’t even be valid syntax.

I do do what @Yemto mentions earlier, though, and use ternary operations fairly frequently.

And yet, I find your latter example easier to understand.