Coding Style

Just like human languages, programming languages have a particular syntax and grammar. If you make a mistake, you end up with a program that is invalid, or "gibberish." And just as publishers and periodicals have "house styles" that govern how to indent paragraphs, how many spaces to put after a period, which abbreviations to use or avoid, etc., companies and programming projects also adopt style guidelines.

imaginereadingatextwherenothingwascapitalized,therewasno spacebetweenwordsandaroundpunctuationmarks,andparagraphswere separatedbyonlyatinyblankspace.thetextwouldpreserveits meaning,butbenearlyimpossibletoread. Furthermore, if every article (or every paragraph) in a magazine adhered to a completely different style, it would be almost as confusing. Programming is no different, except that it is far easier to make a syntactically correct program unreadable by using poor or inconsistent style. Good style is essential to programming, and a substantial portion of your homework grade will be based on style.

Programming style generally encompasses how you indent your code, when you put spaces around operators, and how you name variables, functions, and classes. There are many opinions about what constitutes the best style, and most companies and projects adopt their own guidelines to ensure consistency. In CIS 110, we generally follow the textbook's style guidelines. These are typical of the rules you are likely to encounter elsewhere, but represent a particular set of choice among many possibilities. Here are some of the highlights (where these highlights differ from the booksite's style guide, follow the highlights):

Every .java file you submit must start with a file header comparable to the one below. Often times we will provide you a template, but you can also cut and paste from another file or from the example below. Your comment does not have to be formatted identically to this one, but it must start with your name, PennKey, and recitation, followed by instructions for how to run your program, and a description of what the program does.
	/*  Name: Benedict Brown
	 *  PennKey: bjbrown
	 *  Recitation: 200
	 *
	 *  Execution: java HelloWorld
	 *
	 *  Prints "Hello, World". By tradition, this is everyone's first program.
	 *
	 */

Everyone agrees on the importance of a file header, but every project and company has its own format. It's common to include the name of the file, copyright notice, date, and list of authors at the very top to assert ownership. Some projects include a list of changes to the file, a list of any other files the program needs, etc. Depending on the country, failure to assert copyright at the top of file may implicitly release it into the public domain. (This was true in the United States until 1989. Early source code to the UNIX operating is widely assumed to be in the public domain because it was distributed without copyright notices before that time. AT&T famously settled a lawsuit against the University of California over the UNIX copyrights when this issue came to light.)

We use the checkstyle program to automatically detect many (but not all) violations of the textbook's style guidelines. On most assignments, you will see the output of checkstyle when you submit the program. If there are any warnings that we have not specifically told you to ignore, you should correct them and resubmit or risk a style deduction. Once we cover the terminal/command prompt, we will show you how to run checkstyle yourself, before submitting.