Almost all the code I've seen until now uses the following style for curly braces:
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
Visual Studio also defaults to this. However I have read Code Complete 2nd edition and Steve McConnell argues against this style. "Avoid unindented being-end pairs (...) Although this approach looks fine, it (...) doesn't show the logical structure of the code. Used this way, the begin and end aren't part of the control construct, but they aren't part of the statements after it either." Steve McConnell recommends using the pure block style, which emulates Visual Basic (where there's no curly braces):
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
or this (begin-end block boundaries):
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
Although I tend to agree with McConnell's reasoning, all the books I read, the classes I attended, and Visual Studio, use the first style, so I find it a bit weird. What do you think?
It's funny when thieves accuse other thieves of stealing. Ai companies just blatantly siphoned all the knowledge of the internet without consent and are now selling it with their service... so excuse me if I find this a bit ironic.
Question
Andre S. Veteran
Almost all the code I've seen until now uses the following style for curly braces:
void swap(int &a, int &b) { int temp = a; a = b; b = temp; }Visual Studio also defaults to this. However I have read Code Complete 2nd edition and Steve McConnell argues against this style. "Avoid unindented being-end pairs (...) Although this approach looks fine, it (...) doesn't show the logical structure of the code. Used this way, the begin and end aren't part of the control construct, but they aren't part of the statements after it either." Steve McConnell recommends using the pure block style, which emulates Visual Basic (where there's no curly braces):
void swap(int &a, int &b) { int temp = a; a = b; b = temp; }or this (begin-end block boundaries):
void swap(int &a, int &b) { int temp = a; a = b; b = temp; }Although I tend to agree with McConnell's reasoning, all the books I read, the classes I attended, and Visual Studio, use the first style, so I find it a bit weird. What do you think?
Link to comment
https://www.neowin.net/forum/topic/630296-curly-braces-placement-and-indentation/Share on other sites
63 answers to this question
Recommended Posts