March 29, 2024

The ContactSunny Blog

Tech from one dev to another

Why I love curly brackets

4 min read
PHP Code

Curly brackets. Well, what can I say about them? They sure do have curves! And such curves, I’m addicted! I’ve been abusing them for as long as I remember. I abuse them even today. The day I saw a piece of code for the first time, over a decade ago, I knew I’d be using those curly brackets for the rest of my life. I’m talking about curly brackets in function definitions, loops, conditional statements.

I wrote my first ever program in C. As you might have guessed, it was a simple Hello, World! program. I’m not going to talk about the awesome, exciting, orgasmic, feeling that your first ever program gives you. We’ve all been through that. I’m going to tell you why I love those curly brackets so much even though they are not really mandatory in some cases today. For example, if your loops or conditional statements have only one statement within them, you could get away without using any curly brackets at all. And a few programming languages, such as Python, have different meanings for curly brackets and they don’t mean the same as they do in languages such as C, Java, or PHP.


Let’s start with a simple example:

if(var == 1) {
   return true;
}

That’s as simple as an if condition could get, you would think! You can actually write the same like this:

if(var == 1)
    return true;

It has the exact same meaning. Then there are ternary operators:

var == 1 : true ? false;

This looks sexy, don’t you think? The more short hand code you use, the more smart you are. That’s the notion. Consider the first and the last examples above. The three line code snippet is old-school, boring, and common. But the one line weird looking ternary code is uncommon, and sexy-looking. But it’s old-school too. When they are both old-school, why is one so common than the other? I can think of a couple of reasons — it’s easy to read and understand.

It’s always a matter of convenience you see!

Most often than not, you’d need to extend snippets like these to add more statements into the if block or the else block. That’s just how programming works. In case of the first example, your new code would look something like this:

if(var == 1) {
    x = 2*y;
    return true;
}

That makes sense, and it was easy to modify too. Now how would you modify the second example? You’d say like this:

if(var == 1)
    x = 2*y;
    return true;

Now if this is a C/C++, Java, C#, JavaScript or PHP code, you have managed to introduce a bug. Because this is the same as writing:

if(var == 1) {
    x = 2*y;
}
return true;

But this is not what you intended. So the only way to fix this is:

if(var == 1) {
    x = 2*y;
    return true;
}

Now there is no difference between this and the very first example. I’m not even going to talk about ternary code here, for obvious reasons.


Now let’s talk about programming languages or scripts which either completely take out the use of curly brackets, or make them optional. People say it’s convenient not to use curly brackets because it avoids a couple of key strokes. You can’t be serious if you are making this argument. How much time could you seriously be wasting keying in those curly brackets? But on a serious note, it’s not beautiful, in the sense that it makes reading and understanding code a whole lot difficult.

To read and understand code, you need to have some documentation. I wrote another post the other day talking about why I think documentation is important. When you are working against a deadline, which most of us usually are, understanding the code is the top priority, not figuring out where it starts and where it ends, or which level of indentation is for which loop. You’ll be wasting more time figuring this out than you would have typing those damn curly brackets (I really don’t see typing curly brackets as waste of time).

So yeah, I love curly brackets. And I’ll be using them for as long as I possible can. And I’d respect people who would use them too even if they are optional. It looks more organised, neat, and it makes life easy. Isn’t that the ultimate aim?

If you are a JavaScript person and are excited about the cool new way of defining functions accoring to ES2015, here’s a post which talks about something very similar.

Leave a Reply

Your email address will not be published. Required fields are marked *

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