Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (798)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (865)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Do you sometimes skip braces?  (Read 21779 times)
0 Members and 1 Guest are viewing this topic.
Offline Yemto

Senior Devvie


Medals: 4
Exp: 3 years



« Posted 2015-02-08 05:39:15 »

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.
Offline SHC
« Reply #1 - Posted 2015-02-08 05:42:45 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
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.

Offline superminer362

Senior Newbie


Medals: 3
Exp: 1 year



« Reply #2 - Posted 2015-02-08 06:12:06 »

If there is more than one statement total inside the block or nested blocks, I don't skip the brace.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
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");
        }
    }
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CopyableCougar4
« Reply #3 - Posted 2015-02-08 06:14:11 »

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

1  
2  
3  
4  
5  
6  
if (number < 100)
      number %= 50;
else {
      number %= 300;
      System.out.println("Number");
}

Either wandering the forum or programming. Most likely the latter Smiley

Github: http://github.com/CopyableCougar4
Offline LiquidNitrogen
« Reply #4 - Posted 2015-02-08 06:21:39 »

I dont ever skip them except in a case like:
1  
if (number < 100) number %= 50;


It's a very difficult job trying to prevent myself from confusing myself from confusing
Offline 65K
« Reply #5 - Posted 2015-02-08 06:35:04 »

I just want to know if you personally do it,
Never.

Lethal Running - a RPG about a deadly game show held in a futuristic dystopian society.
Offline Yemto

Senior Devvie


Medals: 4
Exp: 3 years



« Reply #6 - Posted 2015-02-08 06:37:58 »

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

1  
2  
3  
4  
5  
//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.
Offline GoToLoop

Junior Devvie


Medals: 2
Exp: 1 year



« Reply #7 - Posted 2015-02-08 07:30:19 »

I always skip them as much as I'm allowed to do it!    Grin
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!   Roll Eyes
As many modern languages (Python, Ruby, CoffeeScript, etc.) have already proven, indentation is enough to both visually & programmably determine scope.   Cool
Offline EgonOlsen
« Reply #8 - Posted 2015-02-08 08:18:04 »

No, never.

Offline cylab

JGO Kernel


Medals: 195



« Reply #9 - Posted 2015-02-08 08:44:20 »

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.

Mathias - I Know What [you] Did Last Summer!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline princec

« JGO Spiffy Duke »


Medals: 1106
Projects: 3
Exp: 20 years


Eh? Who? What? ... Me?


« Reply #10 - Posted 2015-02-08 10:24:10 »

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

Cas Smiley

Offline Matth59

Junior Devvie


Medals: 8
Exp: 7-9 months


Excuse for the veeeery bad english, i'm Italian.


« Reply #11 - Posted 2015-02-08 10:38:07 »

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.  Tongue Clueless Yawn

Search me on Steam as Matth!
Offline lcass
« Reply #12 - Posted 2015-02-08 10:55:57 »

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.
Offline Roquen

JGO Kernel


Medals: 518



« Reply #13 - Posted 2015-02-08 11:09:36 »

Whenever it seems easier to understand what I'm writing.
Offline Husk

Senior Devvie


Medals: 20
Exp: 3 years


Want to learn everything.


« Reply #14 - Posted 2015-02-08 11:13:02 »

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.

Offline princec

« JGO Spiffy Duke »


Medals: 1106
Projects: 3
Exp: 20 years


Eh? Who? What? ... Me?


« Reply #15 - Posted 2015-02-08 11:17:04 »

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 Smiley

Offline basil_

« JGO Bitwise Duke »


Medals: 418
Exp: 13 years



« Reply #16 - Posted 2015-02-08 11:41:18 »

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
Offline KevinWorkman

« JGO Plugged Duke »


Medals: 288
Projects: 12
Exp: 12 years


HappyCoding.io - Coding Tutorials!


« Reply #17 - Posted 2015-02-08 15:41:53 »

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

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.

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
Offline Gibbo3771

JGO Kernel


Medals: 128
Projects: 5
Exp: 1 year


Currently inactive on forums :(


« Reply #18 - Posted 2015-02-08 16:44:27 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
// 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):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
  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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
  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;
                    }
                }
            }
        }

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline nsigma
« Reply #19 - Posted 2015-02-08 17:15:47 »

Never, never, never, never, ... and never!   Grin  IMO, shouldn't even be valid syntax.

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

Kind of avoided braces right?

Looks a lot nicer than:
...code...

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

Praxis LIVE - hybrid visual IDE for (live) creative coding
Offline Gibbo3771

JGO Kernel


Medals: 128
Projects: 5
Exp: 1 year


Currently inactive on forums :(


« Reply #20 - Posted 2015-02-08 17:21:22 »

Never, never, never, never, ... and never!   Grin  IMO, shouldn't even be valid syntax.

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

Kind of avoided braces right?

Looks a lot nicer than:
...code...

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

I took a lot of line breaks away to make the post smaller, let me adjust and see if you still agree :p lol.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline nsigma
« Reply #21 - Posted 2015-02-08 18:03:41 »

I took a lot of line breaks away to make the post smaller, let me adjust and see if you still agree :p lol.

 Grin

It's like asking a British English speaker (me) to read American English.  I mean, it kind of looks like it should make sense, but still ends up feeling like you're reading gobbledygook.  Tongue

Praxis LIVE - hybrid visual IDE for (live) creative coding
Offline Roquen

JGO Kernel


Medals: 518



« Reply #22 - Posted 2015-02-08 18:33:06 »

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.
Meh.  The notion that coding conventions can prevent bugs is even sillier than test units can.
Offline KevinWorkman

« JGO Plugged Duke »


Medals: 288
Projects: 12
Exp: 12 years


HappyCoding.io - Coding Tutorials!


« Reply #23 - Posted 2015-02-08 19:02:18 »

Meh.  The notion that coding conventions can prevent bugs is even sillier than test units can.

I might agree with you, if it weren't for the sheer number of times I've seen bugs caused by forgetting to add braces.

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
Offline princec

« JGO Spiffy Duke »


Medals: 1106
Projects: 3
Exp: 20 years


Eh? Who? What? ... Me?


« Reply #24 - Posted 2015-02-08 19:26:43 »

You are both right, IMHO... being allowed to miss braces out of single-line blocks was a huge design flaw that should have been taken out of Java and can lead to some very entertainingly hard to find bugs as @KevinWorkman says - exactly like that - however just having conventions never helps at all because they're not enforced. You need to have the enforcement automated.

Cas Smiley

Offline MrPizzaCake

Senior Devvie


Medals: 8
Exp: 3 years


Hello, another wonderful human being :)


« Reply #25 - Posted 2015-02-08 19:38:50 »

Not intentionally. Sometimes I forget, but I usually notice it right away.

How am I? Tough question, since emotions are confusing as heck Tongue
Offline Omnicrola

Senior Newbie


Exp: 1 year



« Reply #26 - Posted 2015-02-08 21:50:14 »

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

This is my preferred method. Just set it and forget it. No question about what is included and what isn't, and I don't have to worry about it.  I also <3 the setting that automatically places ; and { on the end of the line you're on.
Offline Perishtronic Studios
« Reply #27 - Posted 2015-02-13 15:48:11 »

I never skip braces!

This was very common when I first started programming

At first:
1  
if (blablabla) doStuff(); 

Yeah, I'm pretty sure I only need to do one thing

5 minutes later... I need to do more stuff on that if, add braces here and there and waste some precious time.. FUUUUUUU!
Offline Drenius
« Reply #28 - Posted 2015-02-13 16:52:30 »

I do always skip them where possible except for nested loops and have never encountered any difficulties while having way less characters in my code that make me slower at reading it.
Offline ziozio
« Reply #29 - Posted 2015-02-13 19:20:57 »

I always put braces on everything, I'm quite compulsive with this  Grin I find it makes the code more readable because its easier to see where the scope starts and finishes among lots of things. I don't think you lose anything by having them on.

I always put the brace on a new line too, but that's a whole other discussion!
Pages: [1] 2
  ignore  |  Print  
 
 

 
Riven (43 views)
2019-09-04 15:33:17

hadezbladez (4008 views)
2018-11-16 13:46:03

hadezbladez (1463 views)
2018-11-16 13:41:33

hadezbladez (4028 views)
2018-11-16 13:35:35

hadezbladez (777 views)
2018-11-16 13:32:03

EgonOlsen (4096 views)
2018-06-10 19:43:48

EgonOlsen (4681 views)
2018-06-10 19:43:44

EgonOlsen (2761 views)
2018-06-10 19:43:20

DesertCoockie (3656 views)
2018-05-13 18:23:11

nelsongames (3866 views)
2018-04-24 18:15:36
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!