Left or Right Curly Braces

Should you put your curly braces on the left or right?

function foo(){
    //My curly brace is on the right.

function bar()
    //My curly brace is on the left.

For the longest time I really did not think it mattered. In fact I found it was easier for me to read my code if I kept my curly braces on the left. While watching JavaScript the Good Parts by Douglas Crockford on Pluralsight I learned of a situation where it actually does make a difference.

It has to do with returning an object literal from a function.  It is not just any object literal, like how you might assign it to a variable and return that, it is actually constructing the object literal after the return statement.  To show this I created a Fiddle.

In my JavaScript I created three functions, each one returning the same object literal and value.  I then took that result and set it to the value of a text box.  Just like Douglas Crockford said in the video, the one with the curly brace on the left (testThree) silently fails.

function testOne(){
        value: true

function testTwo(){
    var result = 
            value: true
    return result;

function testThree(){
        value: true

I included testTwo just to show that it will not happen if you create your object literal as a variable.  Douglas Crockford goes on to explain that this happens because JavaScript tries to automatically insert the semicolons as a way to make it easier for people to write JavaScript.  So it puts the semicolon after the return statement, thus you will not get that object literal.  In fact if you just output the result of the function without trying to access the object literal testThree will give you undefined while the others will give you an object.

Is this enough to make me want to change my ways and put my curly braces on the right?  It is definitively something I am considering because it will help me avoid this kind of situation altogether.  I know it will be a hard change though, I have been placing my curly braces on the left for a very long time.  Will I ever encounter this situation if I didn’t change, who knows?  This makes me wonder if there are features like this in other languages where the curly braces on the left or right really does matter.

About SheldonS

Web developer for over 15 years mainly with Microsoft technologies from classic ASP to .NET 4. Husband, father, and aspiring amateur photographer.

Posted on June 9, 2014, in Bits, C#, JavaScript and tagged . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: