Danny Wyatt Posted February 15, 2022 Share Posted February 15, 2022 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 Quote Link to comment Share on other sites More sharing options...
Dewdman42 Posted February 15, 2022 Share Posted February 15, 2022 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. Quote Link to comment Share on other sites More sharing options...
Danny Wyatt Posted February 15, 2022 Author Share Posted February 15, 2022 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? Quote Link to comment Share on other sites More sharing options...
Dewdman42 Posted February 15, 2022 Share Posted February 15, 2022 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. Quote Link to comment Share on other sites More sharing options...
Danny Wyatt Posted February 15, 2022 Author Share Posted February 15, 2022 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. Quote Link to comment Share on other sites More sharing options...
Dewdman42 Posted February 15, 2022 Share Posted February 15, 2022 exactly Quote Link to comment Share on other sites More sharing options...
Dewdman42 Posted February 15, 2022 Share Posted February 15, 2022 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. Quote Link to comment Share on other sites More sharing options...
Danny Wyatt Posted February 15, 2022 Author Share Posted February 15, 2022 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.