diff --git a/docs/CodingStandards.html b/docs/CodingStandards.html index 700fe1b8838..3eb9f0c5aef 100644 --- a/docs/CodingStandards.html +++ b/docs/CodingStandards.html @@ -53,14 +53,24 @@
+This document attempts to describe a few coding standards that are being used in +the LLVM source tree. Although no coding standards should be regarded as +absolute requirements to be followed in all instances, coding standards can be +useful.
-This document intentionally does not prescribe fixed standards for religious issues such as brace placement and space usage. For issues like this, follow the golden rule:
+This document intentionally does not prescribe fixed standards for religious
+issues such as brace placement and space usage. For issues like this, follow
+the golden rule:
-If you are adding a significant body of source to a project, feel free to use whatever style you are most comfortable with. If you are extending, enhancing, or bug fixing already implemented code, use the style that is already being used so that the source is uniform and easy to follow.
+If you are adding a significant body of source to a project, feel
+free to use whatever style you are most comfortable with. If you are extending,
+enhancing, or bug fixing already implemented code, use the style that is already
+being used so that the source is uniform and easy to follow.
-The ultimate goal of these guidelines is the increase readability and maintainability of our common source base. If you have suggestions for topics to be included, please mail them to Chris.
+The ultimate goal of these guidelines is the increase readability and +maintainability of our common source base. If you have suggestions for topics to +be included, please mail them to Chris.
@@ -78,11 +88,17 @@ The ultimate goal of these guidelines is the increase readability and maintainab
+Comments are one critical part of readability and maintainability. Everyone +knows they should comment, so should you. :) Although we all should probably +comment our code more than we do, there are a few very critical places that +documentation is very useful:
+Every source file should have a header on it that describes the basic purpose of +the file. If a file does not have a header, it should not be checked into CVS. +Most source trees will probably have a standard file header format. The +standard format for the LLVM source tree looks like this:
//===-- llvm/Instruction.h - Instruction class definition --------*- C++ -*--=// @@ -93,36 +109,59 @@ Every source file should have a header on it that describes the basic purpose of //===----------------------------------------------------------------------===//-A few things to note about this particular format. The "-*- C++ -*-" string on the first line is there to tell Emacs that the source file is a C++ file, not a C file (Emacs assumes .h files are C files by default [Note that tag this is not necessary in .cpp files]). The name of the file is also on the first line, along with a very short description of the purpose of the file. This is important when printing out code and flipping though lots of pages.
+A few things to note about this particular format. The "-*- C++ -*-" +string on the first line is there to tell Emacs that the source file is a C++ +file, not a C file (Emacs assumes .h files are C files by default [Note that tag +this is not necessary in .cpp files]). The name of the file is also on the +first line, along with a very short description of the purpose of the file. +This is important when printing out code and flipping though lots of pages.
-The main body of the description does not have to be very long in most cases. Here it's only two lines. If an algorithm is being implemented or something tricky is going on, a reference to the paper where it is published should be included, as well as any notes or "gotchas" in the code to watch out for.
+The main body of the description does not have to be very long in most cases. +Here it's only two lines. If an algorithm is being implemented or something +tricky is going on, a reference to the paper where it is published should be +included, as well as any notes or "gotchas" in the code to watch out for.
+Classes are one fundemental part of a good object oriented design. As such, a +class definition should have a comment block that explains what the class is +used for... if it's not obvious. If it's so completely obvious your grandma +could figure it out, it's probably safe to leave it out. Naming classes +something sane goes a long ways towards avoiding writing documentation. :)
+Methods defined in a class (as well as any global functions) should also be +documented properly. A quick note about what it does any a description of the +borderline behaviour is all that is necessary here (unless something +particularly tricky or insideous is going on). The hope is that people can +figure out how to use your interfaces without reading the code itself... that is +the goal metric.
-Good things to talk about here are what happens when something unexpected happens: does the method return null? Abort? Format your hard disk?
+Good things to talk about here are what happens when something unexpected +happens: does the method return null? Abort? Format your hard disk?
+In general, prefer C++ style (//) comments. They take less space, +require less typing, don't have nesting problems, etc. There are a few cases +when it is useful to use C style (/* */) comments however:
-To comment out a large block of code, use #if 0 and #endif. These nest properly and are better behaved in general than C style comments.
+To comment out a large block of code, use #if 0 and #endif. +These nest properly and are better behaved in general than C style comments.
+In all cases, prefer spaces to tabs in source files. People have different +prefered indentation levels, and different styles of indentation that they +like... this is fine. What isn't is that different editors/viewers expand tabs +out to different tab stops. This can cause your code to look completely +unreadable, and it is not worth dealing with.
-As always, follow the Golden Rule above: follow the style of existing code if your are modifying and extending it. If you like four spaces of indentation, DO NOT do that in the middle of a chunk of code with two spaces of indentation. Also, do not reindent a whole source file: it make for incredible diffs that are absolutely worthless.
+As always, follow the Golden Rule above: follow the +style of existing code if your are modifying and extending it. If you like four +spaces of indentation, DO NOT do that in the middle of a chunk of code +with two spaces of indentation. Also, do not reindent a whole source file: it +makes for incredible diffs that are absolutely worthless.
+Okay, your first year of programming you were told that indentation is +important. If you didn't believe and internalize this then, now is the time. +Just do it.
@@ -189,9 +238,17 @@ Okay, your first year of programming you were told that indentation is important
+If your code has compiler warnings in it, something is wrong: you aren't casting +values correctly, your have "questionable" constructs in your code, or you are +doing something legitimately wrong. Compiler warnings can cover up legitimate +errors in output and make dealing with a translation unit difficult.
-It is not possible to prevent all warnings from all compilers, nor is it desirable. Instead, pick a standard compiler (like gcc) that provides a good thorough set of warnings, and stick to them. At least in the case of gcc, it is possible to work around any spurious errors by changing the syntax of the code slightly. For example, an warning that annoys me occurs when I write code like this:
+It is not possible to prevent all warnings from all compilers, nor is it +desirable. Instead, pick a standard compiler (like gcc) that provides +a good thorough set of warnings, and stick to them. At least in the case of +gcc, it is possible to work around any spurious errors by changing the +syntax of the code slightly. For example, an warning that annoys me occurs when +I write code like this:
if (V = getValue()) { @@ -199,7 +256,10 @@ It is not possible to prevent all warnings from all compilers, nor is it desirab }
-gcc will warn me that I probably want to use the == operator, and that I probably mistyped it. In most cases, I haven't, and I really don't want the spurious errors. To fix this particular problem, I rewrite the code like this:
+gcc will warn me that I probably want to use the == operator, +and that I probably mistyped it. In most cases, I haven't, and I really don't +want the spurious errors. To fix this particular problem, I rewrite the code +like this:
if ((V = getValue())) { @@ -429,7 +489,9 @@ for you. C++ is just a tool that wants you to master it. :)Writing Iterators
-Here's a pretty good summary of how to write your own data structure iterators in a way that is compatible with the STL, and with a lot of other code out there (slightly edited by Chris):
+Here's a pretty good summary of how to write your own data structure iterators +in a way that is compatible with the STL, and with a lot of other code out there +(slightly edited by Chris):
From: Ross Smith@@ -750,14 +812,16 @@ Ross Smith <ross.s@ihug.co.nz> The Internet Group, Auckland, New Zealand -A lot of these comments and recommendations have been culled for other sources. Two particularly important books for our work are:
+A lot of these comments and recommendations have been culled for other sources. +Two particularly important books for our work are:
- Effective C++ by Scott Meyers. There is an online version of the book (only some chapters though) available as well.
- Large-Scale C++ Software Design by John Lakos
-If you get some free time, and you haven't read them: do so, you might learn something. :) +If you get some free time, and you haven't read them: do so, you might learn +something. :)