Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7
If it's just for the fancy, I'd prefer to choose this style instead: if(1) { { { { printf("%d", 1); } } } } // ;-D
21st Oct 2019, 3:01 PM
HonFu
HonFu - avatar
+ 10
Some recommend using them all the time.
21st Oct 2019, 3:34 AM
Sonic
Sonic - avatar
+ 9
Hi Dynamic Squid. I use curly brackets most of the time. I need them especially when I have more than one instruction, within the condition. For clarity as well.
21st Oct 2019, 1:30 AM
Manual
Manual - avatar
+ 6
Also, it helps to make your code more readable, organized, easier to debug.
21st Oct 2019, 1:32 AM
Nootnoot
Nootnoot - avatar
+ 6
I would prefer to not use them, if I don't need them, to make the code shorter, but that means I'll have to add them manually and take them out each time I decide I need more than 1/only 1 argument after all, so it's probably more convenient to just always write them. Or you switch to Python, where you don't need them at all. 😜
21st Oct 2019, 7:09 AM
HonFu
HonFu - avatar
+ 5
For single line statement no need to use. like in your code you have single line statement in if condition so Doesn't matter to use of Curly brackets. But for multiline it's necessary like for loop.
21st Oct 2019, 5:38 AM
A͢J
A͢J - avatar
+ 5
Dynamic Squid, to say that the reason for the existence of curly brackets is to be able to write multiple statements in a single line might be stretching it a bit.
22nd Oct 2019, 12:26 AM
Sonic
Sonic - avatar
+ 4
Just make sure that if the code you write is to be maintained by others, especially those coming from a Python background that if they add more statements under the if condition, they don't forget to add the curly braces. If not, their logic will be wrong.
21st Oct 2019, 5:42 AM
Sonic
Sonic - avatar
+ 4
if you want to do fancy look of your code then you can use the curly brackets for the single statement also 😀
21st Oct 2019, 2:39 PM
Raju Ram Sau
Raju Ram Sau - avatar
+ 4
AJ || ANANT || AC || ANANY || AY that's the hats off performance And yes you already know I have use @.😀
21st Oct 2019, 6:16 PM
Raju Ram Sau
Raju Ram Sau - avatar
+ 4
HonFu that's a very attractive piece of code 😁. Love the semicolon in the last comment as well 👍
22nd Oct 2019, 12:22 AM
Sonic
Sonic - avatar
+ 4
Arpan yes.
22nd Oct 2019, 12:23 AM
Sonic
Sonic - avatar
+ 3
One word. Scope.
22nd Oct 2019, 6:57 AM
Stephen Matthew
Stephen Matthew - avatar
+ 2
I use curly brackets whenever there is more than one line of code after the specified statement(for, while, etc.). If there is only one line, then I don't use them. It looks better to me.
21st Oct 2019, 3:47 AM
Kourosh Sajjadi
Kourosh Sajjadi - avatar
+ 2
Must use when there are more than 1 statement
21st Oct 2019, 8:55 AM
Arpan
Arpan - avatar
+ 2
This is called intend poeted :
21st Oct 2019, 12:03 PM
ant
ant - avatar
+ 2
I will use curly brackets for one-line blocks if I feel the code is "important", or complicated. Curly brackets and maybe a line of whitespace between the brackets and the line of code lets the code breathe and makes it more prominent. For less important code I will drop the brackets. Imo there is a difference between if (i == 0) return options.default; and if (i == 0) { return options.default; } When skimming and reading through code.
21st Oct 2019, 1:20 PM
Schindlabua
Schindlabua - avatar
+ 2
For Single line of code no need of curly brackets if your code is multiline then you must use curly brackets otherwise it will generate an error
22nd Oct 2019, 8:39 AM
Saleem Dummar
Saleem Dummar - avatar
+ 2
For me I use curl braces when writing the main function which is obvious and the I use them when writing codes for control statements
22nd Oct 2019, 12:42 PM
Joel Kanyi
+ 1
Raju Ram Sau I can make face of your also and print in that.😁😁
21st Oct 2019, 3:07 PM
A͢J
A͢J - avatar