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:
- Easier to read.
- Many times you or another programmer will
have to go back and review
code you wrote a while ago. Code that is difficult to read will
take longer to decipher, even if you wrote it yourself. It's
even more difficult to read someone else's code that doesn't
follow industry standard.
- Easier to maintain.
- Clients frequently require changes to existing
programs. Whether you wrote the program or someone else did,
it's difficult to modify and maintain code that violates
standard practices: it can be difficult to find the module
you need to change or the values that need to be updated.
- Easier to debug and avoid coding errors.
- As you're coding, if you're not using
proper standards like good indentation, spacing, documentation,
self-documenting identifiers, etc. it can be very difficult to
locate problems with your code. One of the biggest problems I
see in class is when a student has a missing brace or semi-colon
and can't find it because their indentation is all over the place.
If you follow standards properly, you will rarely make mistakes
like forgetting a brace or semi-colon. If you name variables
properly, you're less likely to type a variable name incorrectly.
If you document well, it will be easy to find the complicated
section of code that contains a logic error.
- More robust and reliable.
- Code that follows industry standards tends to
run better and is
more reliable. It tends to be cleaner (e.g. no unused variables,
unnecessary program structures, statements, etc) and more efficient.
- More user-friendly and consistent.
- You'll focus on this in term 2.
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:
- Documentation: using appropriate amounts of internal
program documentation (comments)
- Using proper indentation and spacing.
- Using Next-Line or End-Of-Line block style.
- Using self-documenting identifiers and following a
language's standard naming conventions.
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]