Faculty of Applied Science and
Engineering, University of Toronto
CSC181: Introduction to Computer Programming, Fall 2000 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Code Conventions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ContentsFormatting Statements, Declarations and Definitions
IntroductionWhy Have Code ConventionsThe most compelling reason for you, as a student, to use code conventions, is that they improve the readability of your code. Readable code can help you (and your teammates, if you're working on a team) understand your code better, and this is especially crucial when you try fixing your code, an ongoing process which takes almost 80% of the time for a programming assignment. Perhaps more importantly, readable code helps the markers understand it. It is hard to convince a marker that your code is correct (or partially correct) if it cannot be understood in the first place. AcknowledgementsThis document borrows heavily from Code Conventions for the Java(TM) Programming Language, which is publicly available from Sun Microsystems' Java Web site and Java Guidelines by lecturer Paul Gries, which is included in the CSC148 Course Handbook, 6th ed., from the Department of Computer Science, University of Toronto. This document was compiled and is maintained by Ray Ortigas. Comments should be sent to rayo@dgp.toronto.edu. CommentsComments help other programmers understand your code. Sometimes these are people on your programming team, people who have replaced you, or complete strangers. Even if you're programming alone, comments can help you understand code that you may have previously understood, but do not understand now. There is nothing more frustrating than trying to re-understand code that you originated. Generally, you should place comments before the code being documented. The following sections describe how to write particular types of comments. Header CommentsEach source file should begin with a comment which contains the following items:
Class/Struct CommentsClass comments should be short, describing the purpose of the class, and need not mention available functions/methods. They should not contain any implementation details, either. Users typically won't have access to the code inside your functions. They'll only see the class and its interface. Therefore, it doesn't help at all-it even hurts-to discuss private variables and functions, algorithms used, and so on. Even if they can see the internal details, it is very likely that they'll want to ignore these. In a project with tens of thousands of lines of code, it is necessary as a programmer to rely on abstractions.
Function/Method CommentsFunction comments must include the following items:
The comment should explain exactly what the function does, without explaining how it does what it does. Users probably won't be able to see what the code inside the function does, so they must rely on the function comments to determine how to use the function. The reader cannot see your local variables, so you should not mention them. Likewise, they cannot see the private instance variables and functions, so you should not mention them either.
Variable CommentsAll instance variables and almost all other variables need a comment describing their purpose. It is also a good idea to document any relationships between variables and any restrictions on the range of values the variables can take.
Internal CommentsImplementation details should be documented in internal comments. In general, you should comment groups of related statements, like loops or key sequences in the code.
If you keep your functions short, then there won't be much need for internal comments. (On a somewhat related note, short functions are also easier to debug.) NamesNaming conventions make programs more understandable by making them easier to read. They can also give information about the purpose of the identifier-for example, whether it's a constant, function, or class-which can be helpful in understanding the code. Class/Struct NamesClass/struct names should be nouns, in mixed case with the first letter of the name and the first letter of each internal word capitalized. Try to keep your class/struct names simple and descriptive. Use whole words--avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).
Function/Method NamesFunction/method names should be verbs, in mixed case with the first letter lowercase and the first letter of each internal word capitalized. Names for functions/methods which get and set an attribute of a class
should be prefixed with
Variable NamesVariable names should be in mixed case with the first letter lowercase and the first letter of each internal word capitalized. Variable names should be short yet meaningful. The choice of a variable
name should be mnemonic--that is, designed to indicate to the casual observer
the intent of its use. One-character variable names should be avoided
except for temporary throwaway variables, e.g.
Constant NamesConstant names should be all uppercase with the words separated by underscores
("
General FormattingSome general formatting guidelines are described here. More specific guidelines, as they pertain to classes, functions, etc. are described in the next section. IndentationFour spaces should be used as the unit of indentation. The exact construction of the indentation (spaces vs. tabs) doesn't really matter, but it should be consistent-do not mix spaces and tabs. Blank LinesBlank lines improve readability by setting off sections of code that are logically related. One blank line should always be used in the following circumstances:
Line LengthAvoid lines that are longer than 80 characters. Such lines are difficult to read, especially when your code is printed out since they wrap and span multiple lines. If your editor doesn't display the line width, you can always make a line of 80 characters and paste it in to check whether you've gone over. If you're reading this electronically, you can copy and paste the following line into your file (make sure to align it at the left margin):
Wrapping LinesWhen an expression will not fit on a single line, break it according to these general principles:
Formatting Statements, Declarations and DefinitionsCompound StatementsCompound statements are statements that contain lists of statements enclosed
in braces (" Class/Struct DefinitionsWhen coding classes/structs:
Function/Method Declarations/Definitions/CallsWhen coding function/method declarations/definitions/calls:
Additionally, when coding function/method definitions:
Control StatementsWhen coding control statements, such as those involving the keywords
Additionally, when coding
Additionally, when coding
Programming PracticesConstantsAvoid coding constants directly (also known as using magic numbers).
Use
AssignmentsAvoid assigning several variables to the same value in a single statement. This is hard to read. Separate assignments.
ParenthesesIt is generally a good idea to use parentheses liberally in expressions involving mixed operators to avoid operator precedence problems. Even if the operator precedence seems clear to you, it might not be to others-you shouldn't assume that other programmers know precedence as well as you do.
Boolean ExpressionsAvoid testing boolean expressions for equality with
Returning ValuesTry to make the structure of your program match the intent.
Special CommentsUse |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
CSC181 Home Page | Last updated on 2000-12-01 by Ray Ortigas. Adapted from Java Guidelines (in CSC148 Course Handbook, 6th ed.). Copyright 1999 Paul Gries, Department of Computer Science, University of Toronto. Adapted with permission from Code Conventions for the Java(TM) Programming Language. Copyright 1995-1999 Sun Microsysytems, Inc. All rights reserved. |