Code style
A friend and I were discussing curly bracket placement for code blocks (in curly bracket languages of course), and we think it might possibly be the most flamewarred programming topic of all time.
Personally, I have always coded with the initial curly bracket on the same line as the function/method header, like so:
In Java, this seems to be a fairly common practice, although in code style guidelines for my Java classes at MTU it was not stressed. However, it is a different story when you get to C++ or C. I find that most C coders I know place each curly bracket on its own newline, like so:
Because I was always taught that staying consistent in my code style, I do not use that style even when coding C. I thought it was interesting; however, when I perused MTU's C++ code style guidelines (found here), they clearly state: "Brackets { }: these must always be placed on their own line."
After thinking about this for a few moments, I thought about some of the non-curly bracket languages I frequently program in (Lisp and Python), and each of them have strict code style guidelines as well. Lisp doesn't have any whitespace restraints, however, if you do not indent properly, you risk catching simple errors, and the wrath of other Lispers. For example this code follows Lisp style:
whereas this does not:
even though the Lisp compiler will let you do that. Python is much more strict about whitespace and such, forcing the programmer to write clear code.
So in the end, even though a programmer may find himself/herself coding in a certain style for one language, he/she should be willing to code to the style more widely accepted for another language rather than the style he/she may be the most comfortable with (this occurs most specifically between Java and C in my opinion).
Personally, I have always coded with the initial curly bracket on the same line as the function/method header, like so:
public static void main(String[] args) {
System.out.prinln("Hello");
}
In Java, this seems to be a fairly common practice, although in code style guidelines for my Java classes at MTU it was not stressed. However, it is a different story when you get to C++ or C. I find that most C coders I know place each curly bracket on its own newline, like so:
int main(int argc, char **argv)
{
printf("Hello\n");
return 0;
}
Because I was always taught that staying consistent in my code style, I do not use that style even when coding C. I thought it was interesting; however, when I perused MTU's C++ code style guidelines (found here), they clearly state: "Brackets { }: these must always be placed on their own line."
After thinking about this for a few moments, I thought about some of the non-curly bracket languages I frequently program in (Lisp and Python), and each of them have strict code style guidelines as well. Lisp doesn't have any whitespace restraints, however, if you do not indent properly, you risk catching simple errors, and the wrath of other Lispers. For example this code follows Lisp style:
(defun foo ()
(format t "Hello~%"))
whereas this does not:
(defun foo ()
(format t "Hello~%")
)
even though the Lisp compiler will let you do that. Python is much more strict about whitespace and such, forcing the programmer to write clear code.
So in the end, even though a programmer may find himself/herself coding in a certain style for one language, he/she should be willing to code to the style more widely accepted for another language rather than the style he/she may be the most comfortable with (this occurs most specifically between Java and C in my opinion).
Labels: c, code style, java, lisp, programming


0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home