Jump to content

Curly brackets or not?


Recommended Posts

A few days I was given the following code that has no curly brackets after "if(event.pitch == pitch1)" and some other places. Here:

function HandleMIDI(event) {
   if(event instanceof Note) {
       if(event.pitch == pitch1) event.pitch = pitch2;
       else if(event.pitch == pitch2) event.pitch = pitch1;
   }
   event.send();
}

 

 

Would it make more "sense" (or is it best practice) to write it the following way, always keeping the curly brackets?

function HandleMIDI(event) {
   if(event instanceof Note) {
       if(event.pitch == pitch1){
       event.pitch = pitch2;
       }
       else if(event.pitch == pitch2){
       event.pitch = pitch1;
       }
   }
   event.send();
}

 

In here, it shows the code with the extra brackets:

https://www.w3schools.com/js/js_if_else.asp

Danny Wyatt (formerly known as 3ple here on the forum)
Musician, Music Producer, Songwriter, DJ
Official Website: http://www.iamdannywyatt.com  - Join my Discord: https://dwyatt.me/discord

Logic Pro 10.6.3 • M-Audio Fast Track Pro 4x4 • MacBook Pro 13" Mid 2012 || 2.5 GHz Dual-Core Intel Core i5 || 8GB 1600 MHz DDR3 RAM • macOS Catalina 10.15.7

Link to comment
Share on other sites

Think of it like this. you have an if CONDITION and a STATEMENT. The CONDITION is the thing inside the parens(). If that condition is matched then the STATEMENT is executed.

 

That STATEMENT could be a single command, in which case there is no need for curly braces. Or the STATEMENT could be a whole block of code surrounded by curly braces. The curly braces makes that whole block of code kind of like the single STATEMENT, if you see what I mean.

 

Here is example without curly braces:

 

if( myVar = true) yourVar = "yes";

 

Here is example of same code with curly braces, which in this case are optional curly braces:

 

if( myVar = true) {
   yourVar = "yes";
}

 

Here is an example with two commands in the statement block...so curly braces are required:

 

if( myVar = true) {
   yourVar = "yes";
   theirVar = "absolutely";
}

 

its all about readability...whatever works. I tend to always use curly braces just because if I think I might add more than a single command to the statement block...its already structured. And its just consistent. but sometimes for some very simple situations I might not so that the whole thing can be on a single line. Its really just about readability.

OSX 12.x (Monterey) on OpenCore - Logic Pro 10.7.4, VePro7, Mainstage3 - 5,1 MacPro 3.46ghz x 12 96gb ram

Link to comment
Share on other sites

Think of it like this. you have an if CONDITION and a STATEMENT. The CONDITION is the thing inside the parens(). If that condition is matched then the STATEMENT is executed.

 

That STATEMENT could be a single command, in which case there is no need for curly braces. Or the STATEMENT could be a whole block of code surrounded by curly braces. The curly braces makes that whole block of code kind of like the single STATEMENT, if you see what I mean.

 

Here is example without curly braces:

 

if( myVar = true) yourVar = "yes";

 

Here is example of same code with curly braces, which in this case are optional curly braces:

 

if( myVar = true) {
   yourVar = "yes";
}

 

Here is an example with two commands in the statement block...so curly braces are required:

 

if( myVar = true) {
   yourVar = "yes";
   theirVar = "absolutely";
}

 

its all about readability...whatever works. I tend to always use curly braces just because if I think I might add more than a single command to the statement block...its already structured. And its just consistent. but sometimes for some very simple situations I might not so that the whole thing can be on a single line. Its really just about readability.

 

 

I see... so it doesn't "hurt" having them, but if it's just one command you can remove them to make the code cleaner and easier to read, right?

Danny Wyatt (formerly known as 3ple here on the forum)
Musician, Music Producer, Songwriter, DJ
Official Website: http://www.iamdannywyatt.com  - Join my Discord: https://dwyatt.me/discord

Logic Pro 10.6.3 • M-Audio Fast Track Pro 4x4 • MacBook Pro 13" Mid 2012 || 2.5 GHz Dual-Core Intel Core i5 || 8GB 1600 MHz DDR3 RAM • macOS Catalina 10.15.7

Link to comment
Share on other sites

Depends on your definition of "cleaner to read". Some software houses will require all their developers to consistently always use curly braces and even dictate exactly how they should be indented every time, etc. Leaving off the curly braces for short bits of code is shorter and could be cleaner perhaps...or it can also be considered more appropriate to put them there to make the IF sections stand out a certain way with indented code. If I am going to put the statement on another line indented, I use curly braces. I only use without on the same line, but that's just me.

OSX 12.x (Monterey) on OpenCore - Logic Pro 10.7.4, VePro7, Mainstage3 - 5,1 MacPro 3.46ghz x 12 96gb ram

Link to comment
Share on other sites

Depends on your definition of "cleaner to read". Some software houses will require all their developers to consistently always use curly braces and even dictate exactly how they should be indented every time, etc. Leaving off the curly braces for short bits of code is shorter and could be cleaner perhaps...or it can also be considered more appropriate to put them there to make the IF sections stand out a certain way with indented code. If I am going to put the statement on another line indented, I use curly braces. I only use without on the same line, but that's just me.

 

Yeah, cleaner in the sense of having less code and less lines to look at.

But yes, if everything is organized and indented, it probably makes more sense sometimes, even if it's just one line.

Danny Wyatt (formerly known as 3ple here on the forum)
Musician, Music Producer, Songwriter, DJ
Official Website: http://www.iamdannywyatt.com  - Join my Discord: https://dwyatt.me/discord

Logic Pro 10.6.3 • M-Audio Fast Track Pro 4x4 • MacBook Pro 13" Mid 2012 || 2.5 GHz Dual-Core Intel Core i5 || 8GB 1600 MHz DDR3 RAM • macOS Catalina 10.15.7

Link to comment
Share on other sites

also if you think there is any possibility at all that later you may have to add a second line of code to the block, then just put the curly's there to begin with.

 

For scripting small stuff like this it really doesn't matter that much...just use whatever makes your script easier for you to read and keep track of.

OSX 12.x (Monterey) on OpenCore - Logic Pro 10.7.4, VePro7, Mainstage3 - 5,1 MacPro 3.46ghz x 12 96gb ram

Link to comment
Share on other sites

also if you think there is any possibility at all that later you may have to add a second line of code to the block, then just put the curly's there to begin with.

 

For scripting small stuff like this it really doesn't matter that much...just use whatever makes your script easier for you to read and keep track of.

 

Makes sense. Thanks!

Danny Wyatt (formerly known as 3ple here on the forum)
Musician, Music Producer, Songwriter, DJ
Official Website: http://www.iamdannywyatt.com  - Join my Discord: https://dwyatt.me/discord

Logic Pro 10.6.3 • M-Audio Fast Track Pro 4x4 • MacBook Pro 13" Mid 2012 || 2.5 GHz Dual-Core Intel Core i5 || 8GB 1600 MHz DDR3 RAM • macOS Catalina 10.15.7

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...