45

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 :)

200_success
  • 7,286
  • 1
  • 43
  • 74
Spaceman
  • 1,319
  • 13
  • 42
  • 3
    I guess, someone just loves curly braces. :) – CuriousMind May 19 '15 at 05:45
  • 14
    The braces form a [_block statement_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/block). Unlike some other languages, a block in JavaScript does not create a new scope (for variable declarations, for instance). So the usage here seems off to me. – Ted Hopp May 19 '15 at 05:47
  • I don't haha, I want to remove them but I don't want to break things by removing them. @CuriousMind – Spaceman May 19 '15 at 05:48
  • Is there any benefit at all to a block statement in java script. @TedHopp? That link has some good info thankyou. – Spaceman May 19 '15 at 05:51
  • It looks like they are trying to denote dependence of the inner variables on the outer variables, in the ugliest way possible. This gains you nothing but difficult to read code (my opinion, obviously), I say delete it. – Rob M. May 19 '15 at 05:51
  • 2
    A block statement most definitely has value in JavaScript. As the link describes, it's often used with `if..else`, `for`, and `while` (not to mention `do..while`). It just has no benefit in the code you posted. – Ted Hopp May 19 '15 at 06:04
  • Thanks @TedHopp I will begin removing this silly code :) – Spaceman May 19 '15 at 06:08
  • 1
    It could be just a matter of how code ended up after changes. Say it had been `if(someVariable == someValue){…}` and then the code changed so that the block was required all the time and the `if` taken out. – Jon Hanna May 19 '15 at 09:50
  • 2
    Maybe the original coder transcribed that code from another language where that sort of block structuring creates a closure, and they thought it'd create a [closure in JavaScript](http://stackoverflow.com/q/111102/4014959) too. Or they just left the structuring like that because they think it looks nice. :) – PM 2Ring May 19 '15 at 09:59
  • 7
    @Spaceman The correct action isn't to go "begin removing this silly code". The correct action is to discuss the practise with the developer(s) using this practise and work out their motivations first -- ask them *why* they do this rather than talking down to them as if you have ultimate context about their decisions (which you don't). Don't just trample on other people's conventions to look superior, especially just after you've started at a new place of employment -- that's not how you form lasting relationships with people or productively collaborate on code. – Chris Down May 19 '15 at 12:57
  • @ChrisDown you make a very valid point I will hunt down the creator of the code and discuss with them. It wasn't really a goal of looking superior I just don't like excess code that doesn't do anything. – Spaceman May 19 '15 at 13:00
  • 2
    This might be ported from/to some other language where blocks actually mean something. Therefore the extra blocks mean nothing here, but are super-useful when comparing side-to-side javascript code with the other implementation. – Agent_L May 19 '15 at 13:37

5 Answers5

45

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...)

Michael Geary
  • 28,450
  • 9
  • 65
  • 75
  • "missing the `var`..." - that might be intentional, if they were trying to document where variables were used. Nesting with that intent, seems like you can only go parallel or deeper unless you go global – Izkata May 19 '15 at 15:32
10

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.

paul
  • 101
  • 2
  • 1
    The code is as is nothing was omitted. There must be a way of acheiving the folding without using code that will increase file size when bundled ie in comments. – Spaceman May 19 '15 at 23:14
8

As far as I can tell the braces are only a hint to the developer who is reading the code.

My guess is the braces and nesting are only to illustrate that the list contains an item which contains an id. This might be intended to help the reader understand that if they edit any of that code, or move it, then also edit the inner code to match.

joelparkerhenderson
  • 34,808
  • 19
  • 98
  • 119
3

These braces are just to make code more readable that's what some programmers thinks. The braces has nothing to do with any kind of other functionality. People do code wired. Cheers for a new kinda experience. :-P

Areeb Gillani
  • 440
  • 4
  • 25
1

See the code with compiler view.

Curly brackets are just to define scope. Not else more.

Consider, here is a example.

for(var c in b)
{
    a += c.somefunction();
}

This code is similar to

for(var c in b)
    a += c.somefunction();

i.e. by default for loop has scope till next statement.

Similarly,

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

This is just defining scope and adding statements inside that scope. Though you declare variable in that scope, the Hoisting concept will bubble up all variable to top.

Declaring variable inside a scope is just like;

if(true){
 var _occurrence = getObjectByValue(_occurrences, 'tourID', booking.tourID);
}

.

In simple words,

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

is equal to:

var _occurrences = getOccurrences($('#ddlTours').val());
if(true){
    var _occurrence = getObjectByValue(_occurrences, 'tourID', booking.tourID);
    if(true){
        _occurrenceID = _occurrence.occurrenceID;
    }
}
Laxmikant Dange
  • 7,606
  • 6
  • 40
  • 65
  • 1
    http://www.w3schools.com/js/js_hoisting.asp I had a quick read on the concept of Hoisting. Its an interesting feature that i didn't know existed. I still don't understand how it in this case is useful. – Spaceman May 21 '15 at 01:10