Programming Style and Standards

Things To Do First

Things To Do Later

What are Standards?

Important! Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. Code for readability.
(Attributed to John Woods)

When we talk about coding standards or code/programming style, we're talking about how your code looks - proper indentation and spacing, self-documenting identifiers, an appropriate amount of documentation, etc. Sticking with programming standards is important for any professional programmer. Writing code that doesn't follow industry standard is considered amateurish, or what some call "Mickey Mouse" programming (no offense to famous rodents). That kind of bad programming has a negative effect: it makes it difficult for you to go back and find errors, make changes, see how you solved a difficult algorithm; it makes it difficult for other programmers to do the same things. This costs a company/client more money and time. Following good programming style and standards prevents you from wasting your client's valuable time and money.

A program that is syntactically and logically correct will run and function just fine, but a program that does not follow industry standards when it comes to coding and UI design is just as bad! Why?

Programs that follow industry standard are:

Note If you love your editor, it will love you back. What?! What I mean is, if you type your code in a natural way, the editor will help you use proper indentation, and even help you see mistakes that you might have made as you type. For example, in many editors, if you type an opening brace ( { ) and press ENTER, it will automatically indent for you, and might even add the closing brace for you. Similarly, if you type a statement and forget the semi-colon, the editor thinks you're just typing a very long statement (recall that long statements broken over multiple lines should have an indent on the second and subsequent lines) and will indent for you when you press ENTER. When you see this, you would wonder, why did it just indent when I'm not typing a multi-line statement? Then you would realize it's because you forgot your semi-colon!

Tip: In IntelliJ, pressing Ctrl-Alt-L in the editor window will automatically format your code.

Standards and Coding Guidelines for Java

Chapter 1.9 of your textbook gives you a number of guidelines that you should follow. There is additional information in the textbook supplement Expanded Guidelines on Programming Style and Documentation

Additionally, be sure you read the Coding Standards for this course. It is expected that all work you submit will follow these standards.

You should become familiar with these guidelines, not just for this course, but also for later courses, and eventually for any professional work you do outside the college.

Note According to the Java standard, the proper number of spaces for an indent level is 4 spaces! Additionally, they state that a line of code should not exceed 80 characters. Lines that are longer than 80 characters should be broken up into multiple lines. Remember to indent the second and subsequent lines! If your editor is not indenting the correct number of spaces or telling you when your line of code is too long, go into your editor's settings and change your tabs to 4 spaces, and set the right margin to 80 characters (this should show you a vertical line after column 80 so you know where it is).

The standards and guidelines include:

Note What do we mean by self-documenting? A self-documenting identifier is an identifier whose name clearly indicates its purpose. For example, a variable called numOfStudents probably contains the number of students. A method called printHeadings() probably prints some headings for something. A class called Employee probably models an employee object at a company.

Exercises

1. Reformat the following code segments by hand so that they follow proper standards:

a)

public class BadCode { public static void main(String[] 
args){ System.out.println("Flowers for Algernon");}}

b)

public class 
BadCode { public static void main(String[] 
args){ System.out.print("6/4*2 is ");System.
out.println(6/4*2);}}

2. Add documentation to each of the corrected code segments above.

[solutions]