Introduction | This lecture discusses coding style, including why using good style while writing our programs can help us debug them faster. We will discuss in detail four important aspects of style: names, alignment, locality, and comments. Most decisions that we make about formating our programs closely involve one of these four aspects. You should also start reading in Vermeulen, The Elements of Java Style. Specifically, this lecture covers items 5-15, 25-26, and 32-37 (which appear on pages 5-18, 25-26, and 31-36 in Vermeulen's book). |
Coding Style |
Programmers spend an enormous amount of time reading and studying code when
they are writing, testing, and debugging their programs.
Using good programming style allows this process to proceed much more easily.
When writing a program using iterative-enhancement, it is an excellent idea
to beautify your code at the end of each enhancement, before proceding to
the next one; each enhancement should be the best thatit can be, before
continuing.
Ultimately, this strategy will save you time compared to the strategy often
used by students: ignore style until the program is completely written.
This is a penny-wise, pound-foolish strategy.
It is much harder to "finish" a poorly-styled program, because it is harder
to read and understand it; (software) engineers must learn to practice
techniques that overcome human nature; this is one example.
In the real world, companies have their own style guidelines, which all their programmers must follow (see Vermuelen's book). In this way, code written by different programmers is consistent (and therefore more easily readable by other programmers). So, it is not unreasonable for me to ask you to write in a certain style, as consistently as you can. We will use four general principles to discuss issues in programming style (backwards, they are the acronym CLAN).
|
Good Names |
Programmers get to choose identifiers that name variables (and as we will
see later in this course methods, parameters, classes, exceptions, etc).
We should choose descriptive names.
Yes, we should also try to choose short names, but descriptiveness is more
important.
A long descriptive name is better than a short unclear one.
Of course, a short descriptive name is optimal.
Beginning programmers typically choose names that are too short: they
abbreviate too much, or use just single letter names.
Rather than declaring So far, we have learned the fllowing Java naming conventions.
|
Alignment (indenting) |
Generally, we use whitespace to present our programs (to humans, not
computers) in an easy to read and understand form.
Remember that adding extra whitespace doesn't affect the meaning of our
programs (the sequence of tokens is still the same), but it does affect
how a program is displayed in the editor while we are reading it.
Using extra whitespace will make the program "longer" but easier to read. In fact, in one early style of written English (scriptio continua), words were strung together with no intervening whitespace. Itwasstillreadablebutveryslowanddifficulttocomprehend. Sometimes smaller isn't simler. Alignment involves mostly using horizontal whitespace. The most important use of alignment is showing which statements are controlled by which control structures, with the controlled statements indented to appear inside the statements that control them. This relationship is the essence of using control structures, so highlighting it is critical.
There is a pattern in how we write control structures.
For example in the block after main(), all statements are indented at the same level.
Likewise, in an if statement we use the following forms (depending on
whether or not the statement contolled by the if is a block)
Many programmers adopt a style that ALWAYS use blocks in if statements (and loops), even if they contain just ONE statement. On the positive side, such an approach makes it very easy to add/remove statements (when debugging/enhancing programs), because the block is already there; otherwise going from one to more statements requires adding a block, and going from multiple to one statement requires removing the block. On the negative side, blocks, when they are unneccessary, make the program a harder to read. So, choose whichever of these options you think is better, but be consistent with your choice. I like "blocks where necessary" but Vermeulen likes "always blocks".
Finally, identically to if statements, we align a for loop by
indenting the statement that is their body.
I cannot overemphasize how important it is to use proper alignment in control structures. A major source of programming errors for beginners is not understanding which statements are controlled by which control structures: these can get tricky with expression statements inside if statements inside loops. Proper alignment makes such relationships much simpler to see. I have seen students spend 2 hours trying to debug a program; at which point then finally spend 10 minutes aligning its statements (because I refuse to help them until they do), and then they solve their problem by themselves in 1 minute. If you expect to debug your programs, it is imperative that you use proper alignment whenever you add/remove code to/from them.
Another use of alignment occurs when declaring a sequence of variables;
rather than doing so haphazardly, we can align the types, names, initial
values, and comments.
|
Locality (paragraphing) |
Locality is the most subjective of the style rules.
It involves mostly adding extra vertical whitespace (blank lines).
By grouping statements together and then placing blank lines between groups,
we create the programming equivalent of paragraphs in prose writing (where
each paragraph contains related sentences).
In a written paper, students would never put all the sentences into one long
paragraph; likewise, students would never make every sentence into its own
paragraph.
So, we should always use a more reasonable grouping (some number of related
lines) for paragraphing in our programs.
Typically, each code group should contain a half-dozen statements. The magic number 7+/-2 is also used for psychological reasons: it represents the number of items typically usable in the brain's short-term memory. Whenever a large number of statements appear in a block of code, use blank lines to group them into a smaller number of related sequences. We can write a preface comment (see below) that acts as a topic sentence for the paragraph of code. A for loop and try-catch almost always start their own group; so do complicated if statements. Locality is more art than rules; I encourage you to examine the groupings that I use in my sample and solution programs and try to critique and ultimately emulate them. |
Comments |
We document our programs with comments.
While we try to express ourselves as well as we can with Java code, there is
always other useful information about a program that we would like
communicate to programmers reading our code (including ourself, while
debugging it, or at some future date when we are enhancing our code).
Such information is for programmers, not the computer: not the instructions
saying HOW the code works (that is for the programmer and computer), but
WHAT the program does and WHY it does it (that way).
We supply this information in comments.
There are a few different categories of comments that frequently reappear.
|
Miscellaneous Style Rules |
Finally, here are some miscellaneous style rules
|
Problem Set |
To ensure that you understand all the material in this lecture, please solve
the the announced problems after you read the lecture.
If you get stumped on any problem, go back and read the relevant part of the lecture. If you still have questions, please get help from the Instructor, a CA, or any other student.
|