DEV Community

Cover image for {{{Do curly braces waste space?}}} πŸ“–πŸ“˜

Posted on


{{{Do curly braces waste space?}}} πŸ“–πŸ“˜

While writing an if-statement, have you ever wondered about the conventions of the curly braces that follow it? I mean, put curly braces in, don't put curly braces and the "style guide" section of whatever company we work in that tells us which ones to use. Well, I want to look at it in a more factual way. Does it do anything to the common intermediate language (or formerly just IL I believe) that get's drummed up by C# or another .NET language?

The plan is to give a short demonstration to provide us with the answer we are looking for, and then we can just go back to our daily lives like nothing happened. Or... we can take this to the belly of the beast (our company slack channels), drop the discussion in there and see what happens πŸ‘€.

Ok, I'm glued, so what happens when I put curly braces in?

Glad you asked, and I'm gonna waste these extra words to make it look like a complex research was conducted. We can see below a simple bit of code that is an if statement, which when true contains a "hello" being dumped into the console using the Dump method.

Firstly, to give a more detailed view of what happens, let's see the IL code that's spouted out when we don't have curly braces.

Without curly braces

Nothing special to see here, the top bit is the C# code, and the bottom is the Intermediate Language (IL) version of it. We can tell that some steps are taken before going to IL_0003 which let's us know that there's this string "hello"; it is then followed by IL_0008 which is the execution of the extension method Dump.

Adding the curly braces changes things interestingly, the step numbers (not sure what the name is) seem to have been altered.

With curly braces

There seems to be these extra NOP steps that are added to the list, thereby changing the chronology of the process. I didn't fully understand it's effect on size or time, but I compared it the only way I know how; by saving the file and right clicking to see if there was any size change.

With curly braces

The image above is with curly braces.... and below is...

Without curly braces

Mind 🀯 blown ... slightly.

I don't know what or how or why it does this but it certainly is interesting to see a difference.

What does this mean on a wider scale?

I'm not entirely sure, but if we were to extrapolate this into a massive real life project, strip all the unnecessary braces and compare I reckon it could display a nuget package or two worth of size difference...Maybe...

This definitely raises the question about how efficient we can/should be with our code, and could style differences be detrimental or a positive impact on the code base?

Anyway, if you drop this in the slack, please don't blame me for sudden invitations to a "match to the death" by your colleagues. Also, most of all, don't try this at home 🦺

Thank you for sticking around!

I hope to have more stuff underway as I sort my life out at the moment, there will be a few non-technical things I'd like to talk about this year especially concentrating on mental health. There's one in particular that I think could prove important so please watch out for that 😊 See ya round!

Top comments (0)

Advice For Junior Developers

Advice from a career of 15+ years for new and beginner developers just getting started on their journey.