Writing Good Programs
The only way to learn programming is program, program and program. Learning programming is like learning cycling, swimming or any other sports. You can't learn by watching or reading books. Start to program immediately. (On the other hands, to improve your programming, you need to read many books and study how the masters program.)
It is easy to write programs that work. It is much harder to write programs that not only work but also easy to maintain and understood by others – I call these good programs. In the real world, writing program is not meaningful. You have to write good programs, so that others can understand and maintain your programs.
Pay particular attention to:
- Coding Style:
- Read "Java Code Convention" (@ http://www.oracle.com/technetwork/java/codeconvtoc-136057.html or google "Java Code Convention").
- Follow the Java Naming Conventions for variables, methods, and classes STRICTLY. Use camel-case for names. Variable and method names begin with lowercase, while class names begin with uppercase. Use nouns for variables (e.g.,
radius
) and class names (e.g.,Circle
). Use verbs for methods (e.g.,getArea()
,isEmpty()
). - Use Meaningful Names: Do not use names like
a
,b
,c
,d
,x
,x1
,x2
, andx1688
. Avoid single-alphabet names likei
,j
,k
. They are easy to type, but usually meaningless. Use single-alphabet names only when their meaning is clear, e.g.,x
,y
,z
for co-ordinates andi
for array index. Use meaningful names likerow
andcol
(instead ofx
andy
,i
andj
,x1
andx2
),numStudents
,maxGrade
,size
, andupperbound
. Differentiate between singular and plural nouns (e.g., usebooks
for an array of books, andbook
for each item). - Use consistent indentation and coding style. Many IDEs (such as Eclipse/NetBeans) can re-format your source codes with a click.
- Program Documentation: Comment! Comment! and more Comment!