Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of these superfluous curly braces?

Tags:

javascript

So I have recently started at a new place of employment and I have run across a format of javascript which makes me question its purpose. ( in particular the brackets {})

var _occurrences = getOccurrences($('#ddlTours').val()); {     var _occurrence = getObjectByValue(_occurrences, 'tourID', booking.tourID);     {         _occurrenceID = _occurrence.occurrenceID;     } } 

To me it almost looks like an attempted object construction. i.e.

var _occurrences : // Ignoring = getOccurrences($('#ddlTours').val()); {     _occurrence : // Ignoring getObjectByValue(_occurrences, 'tourID', booking.tourID);     {         _occurrenceID : _occurrence.occurrenceID;     } } 

But as I understand it will execute it like.

var _occurrences = getOccurrences($('#ddlTours').val()); var _occurrence = getObjectByValue(_occurrences, 'tourID', booking.tourID); _occurrenceID = _occurrence.occurrenceID; 

Or is it so _occurrence gets delete and does not sit around as its encapsulated and we assign a var that outside of the encapsulation. Does that actually work as a performance improvement? i.e.

Global var a = 1 {     b = someFunction()  // After execution because of encapsulation it poofs???     for(var c in b)     {         a += c.somefunction()     } } 

Another option is that its just bad code?

Or perhaps its meant as a logical separation of code to help the dev?

I was just wondering if someone could shed some light on this for me :)

like image 595
Spaceman Avatar asked May 19 '15 05:05

Spaceman


People also ask

What is the purpose of curly braces?

In writing, curly brackets or braces are used to indicate that certain words and/or sentences should be looked at as a group.

What is the purpose of placing the curly braces on separate lines inside of the method?

The biggest advantage to having the opening curly brace on a separate line is that the curly braces will always line up visually (assuming that we are also using good horizontal spacing in our code).

What does double curly braces mean?

Creating and executing a simple template Handlebars expressions are put into double curly braces {{expr}} for HTML-escaped content; otherwise, use triple curly brackets {{{expr}}} to avoid HTML-escaping.

Why do we use block of statements with braces in C++?

Braces are used around all statements, even single statements, when they are part of a control structure, such as an if-else or for statement. This makes it easier to add statements without accidentally introducing bugs due to forgetting to add braces.


2 Answers

You are right to question those curly braces. They don't do anything at all. The code inside the braces executes just the same as it would if the braces weren't there. It's clearly a mistake to have them there like that.

As you mentioned, it looks like somebody could have thought that the curly braces would introduce a block scope, perhaps causing a variable to go out of scope after the braces are closed. But JavaScript doesn't have block scope for var variables! (It does have block scope for let, but only in the newer JavaScript engines that support let.)

Or maybe they just thought it would be a good way to document where the variables are used. It's not.

Adding to the humor here, the code appears to be missing the var for _occurrenceID entirely - so it's probably creating a global variable unintentionally!

The way you rewrote the code without the curly braces is indeed how it will actually execute. It is a better representation of what the code actually does and is how the code should be written. (Fixing the missing var of course...)

like image 151
Michael Geary Avatar answered Oct 08 '22 04:10

Michael Geary


Or perhaps its meant as a logical separation of code to help the dev?

I'm going to make 2 assumptions here:

  1. the developer was not incompetent
  2. you left out a lot of lines between the curlies

(if 1 is not true then all bets are off)

Do-nothing curly-bracket blocks do have a purpose - in various text editors they mark sections that can be collapsed and thus removed from sight. I often do this if I have 50+ lines that I know work but I have to constantly scroll past. Put curlies around the content (mind the nesting), click the "collapse/fold" icon in the gutter -> code disappears. My particular editor will remember folded blocks so I don't need to re-fold each time.

like image 44
paul Avatar answered Oct 08 '22 03:10

paul