Writing Better Code

Write Readable Code

In your programming assignments, you are expected to write well-documented, readable code. There are a variety of reasons to strive for clear and readable code. Since you will be working in pairs, it will be important for you to be able to read your partner's code. Also, since you will be working on OS/161 for the entire semester, you may need to read and understand code that you wrote several months earlier. Finally, clear, well-commented code makes your TAs happy!

There is no single right way to organize and document your code. It is not our intent to dictate a particular coding style for this class. The best way to learn about writing readable code is to read other people's code. Read the OS/161 code, read your partner's code, read the source code of some freely available operating system. When you read someone else's code, note what you like and what you don't like. Pay close attention to the lines of comments which most clearly and efficiently explain what is going on. When you write code yourself, keep these observations in mind.

Please clearly indicate the code you have added to OS/161 by enclosing it with comments such as:

    // BEGIN GROUP XX's A2 SOLN

    /* Your actual code here, along with descriptive comments */

    // END GROUP XX's A2 SOLN

In simple cases, such as adding or changing a single line of code in an existing function, just place the comment at the end of the line.

Here Are Some General Tips For Writing Better Code:


Angela Demke Brown
Last modified: Sat Oct 1 14:29:59 EDT 2005