What is the best indent/brace style? I use the same style as the K&R book when I'm writing in C:\[# int main(void) { if (cond) { //something } else { // something else } switch (something) { case 'A': //Something /* FALL-THRU */ default: break; } return 0; }\]
I do something similar in Java, but I put the opening brace of a function on the same line as the function name and parameters.
In C++ I use another style, where each brace is on a line of it's own:\[# int main() { /// DOC if (something) { // Do Something } else { // Do Something Else } }\]
I've seen this brace style used in ivy league institutions and some GNU foundation stuff, but I don't like it so much :( note the function return type is right above the function name, instead of to the left of it :-P \[# int main(int argc, char *argv[]) { if (something) { // Do something; } else { // Something else; } }\]
There is no such thing as a 'best' one, IMO. If you are writing code by yourself, any readable format should do, use the one you are used to, and it will be fine. If you are writing a project with other people, sit down and discuss which style you should use, and keep it thorough the project.
Why you use in different language different brackets? You can use same style which you prefer everywhere. However I haven't seen mixing different brackets style like in C example
No, generally you'll want to use the style that is most common for the language you're using. If you're working for a company, they generally have their style they use internally. Using a consistent style means other developers in the same language will be able to read your code more easily. That said, several of the brace styles you've mentioned are fairly interchangeable in the C/C++/Java/JS world. Return type on its own line is just ugly, though :p So are brackets indented on their own.
but i believe those both brackets can be used according to convention in almost every language asdfsd { asasda asdas } asdasd { asdasda asdasd }
That's correct, Tomas. Languages that use braces generally do not care about whitespace (that's usually why braces are used).
Join our real-time social learning platform and learn together with your friends!