Fundamentals of Computer Science II (CSC-152 99S)


Java Style Guide

This document is still under development.

Programming, like writing, provides you some freedom to express the same concept in many different ways. Just as each writer develops his or her own personal style, so do programmers. Important aspects of programming style include

I expect you to follow some basic stylistic conventions in my class. While you will eventually develop your own style, few of you are experienced enough programs to make sufficiently informed decisions. In addition, a uniform style helps us discuss and share code. I will strive to use the same style, but may violate it ocassionally. If you use emacs as your editor, it will automatically enforce some of these rules.

Names

A typical program requires a significant amount of naming of variables, fields, functions, and classes. Strive for informative and distinguishable names. You should rarely use single-letter names (except, possibly, for loop counters). At the same time, you need not go overboard on the length of your names. For most purposes, five to ten characters suffice.

Sun (the developers of Java) recommends a particular capitalization style to help you distinguish the different things that you can name.

Whitespace

Java is whitespace-insensitive (at least in most cases). You can put as few or as many spaces, tabs, and carriage returns as you want in your programs. Hence, you should use whitespace to make your programs more readable, modifiable, and debugable.

Comments

Ordering

Examples

// Step through the list of people, printing out all the students.
for (int i = 0; i < REPETITIONS; ++i) 
{
  // If person i is a student then
  if (people[i].getRole().equals("student")) 
  {
    // Print out the name of person i
    System.out.println(people[i].getName() + " is a student");
  } // if
} // for

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS152/99S/Handouts/java-styleguide.html

Source text last modified Fri Jan 29 10:11:37 1999.

This page generated on Fri Jan 29 12:18:34 1999 by SiteWeaver. Validate this page.

Contact our webmaster at rebelsky@math.grin.edu