Some basic ideas:
-
Code should be written firstly for human readers. Hence it should be
extremely readable. For most problems, the code is as much a
specification of the problem as it is a solution to the problem.
Hence human readers will want to look at it often.
-
Logic should not be duplicated. Not even a single line. The reason
is not code size, but code maintainability. If the same
logic is in two places, subsequent "bug fixes"/enhancements might mistakenly be
applied only in one of the several places.
- Code should be self documenting. If the programmer's intention
can be expressed by (a) self-documenting code (e.g. giving good
variable names), or by (b) writing comments, always choose the
former to the exclusion of the latter. Avoid redundant comments
as well.
-
Match the form to the content. If an idea is algorithmically
important, it must become a method/subroutine; if the algorithm uses a
data structure, the corresponding variables in the code should be a
class. These are ways by which pieces of code can be implicitly
accorded importance.
-
Code should be optimized for efficiency only when strictly necessary
(when experiments indicate the need). Otherwise it should be
optimized for readability. Remember: 90% of the time is spent in 10%
of the code-- hence only 10% of the code need be written for efficiency.
- Automated testing scripts and makefiles are absolutely essential.
- A well written program is a work of art. Strive for perfection!
Classes:
-
Should contain only a few (5-6) instance variables. If you feel you
need more, it is probably better if you group them into 5-6 objects.
-
Instead of accessor/modifier methods, consider making the instance variables
public.
-
Each instance variable should have some logical significance reflected
in its name.
-
Each procedure/method should have some logical significance
reflected in its name+parameter list. Especially what side-effect it
will cause.
-
Ideal Size of Procedures/methods: at most 35 lines, each
at most 80 characters. Basic idea is that it should be contained
in a single screen. Long lines are to be entirely avoided.
-
Number of parameters to methods: No more than 5-6. If you need
more, it is probably better to group them into suitable objects.
Heirarchy:
Names:
Larger the scope, longer the name.
Global variables:
AVOID, AVOID, AVOID. If you must use, need to have VERY STRONG justification.
Position of open and close braces:
Bad positioning:
private void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals("Left"))
{
display.setText(" <---");
}
else if (s.equals("Right"))
{
display.setText(" --->");
}
}
Should be:
private void actionPerformed(ActionEvent e){
String s = e.getActionCommand();
if (s.equals("Left")){
display.setText(" <---");
}
else if (s.equals("Right")){
display.setText(" --->");
}
}
Basic idea is to get more into a screenful. Dont overdo this;
if you put the "}" on the previous line, you will make it inconvenient
to comment out "//" that line; or worse, when you comment out that line,
you will comment out the "}" unwittingly.
Indentation amount:
3 characters. Use an editor like emacs that sets indentation properly
as you type and also pretty prints the code when needed. Basic idea
is to use the 80 columns you have efficiently. If indentation is
too high, you will need to break lines into 2, or worse, go beyond 80 columns.