Lecture #19: Programming Style
November
16, 1999
Bob
Pitha
writing good programs
There
are many different qualities that define a good program
it does what
its supposed to
it doesnt
crash
its easy to
use
its fast
and efficient
it comes in
a neat box
its easy to
maintain
its easy to
understand
writing good programs
There
are many different qualities that define a good program
it does what
its supposed to
it doesnt
crash
its easy to
use
its fast
and efficient
it comes in
a neat box
its easy to maintain
its easy to understand
working vs. good
You
can write a program that works and is still not very good
as you
probably know by now, programming isnt always easy
its often
not worth writing a program if youll only use it once
so you will
usually want to keep the program, and use it more than once
you may need
to fix bugs
or add new
features
or give it
to someone else to do those things to it
ugly code
If
you write ugly, unreadable code, nobody will be able to read it (nor will they
want to)
if you need
to fix a bug, it can seriously affect how easy it is to find and fix
if someone
else looks at your code, they may make fun of you
example: bad code

example: silly code

why these are bad
Heres
a broken version of the last one.
It might
take a long time to find the error

what goes into a good program
Its
readable
Its
easily maintained
Its
logically laid out
analogy to writing
When
you write something (say, a story or an article) you try to write well
you want
your readers to understand your point
in fact, a
good writer will write in ways that help make the point
sentence
length
paragraph
length
word choice
grammar
relevant quote

what are elements of programming style?
Naming
Formatting
Consistency
Magic
numbers
Comments
Naming
whats in a name?
Labels
something
variable
function
other things
we havent talked about yet
Tells
you its purpose
name qualities
A
good name should be:
informative
it should tell you something
concise no
longer than necessary
memorable
you should remember what it is
pronounceable
most people subvocalize
what are the issues?
When
you look at a declaration
you should be
able to guess what the variable is
how it will
be used
supplemented
by comments
When
you look at the code
you
shouldnt have to keep referring back to the declaration
the name
should remind the user of the meaning
informative
Depends
on how close the usage will be to the declaration
if its used
very near the declaration, its not as important that the name carry as much
information
if the use
and declaration are far apart, you want the name to be as informative as
possible
Examples:
numberOfPoints
vs. npoints
elementIndex vs. index vs. i
concise
Really
long names are a pain
lines get
long and unwieldy
numberOfItemsInList
vs. listSize
This
gets tied up with the informative part
more
informative usually means longer
try to avoid
unnecessary length
memorable
If
all your names are similar, it doesnt matter how informative they are
the reader
will still mix them up
like reading
a novel where there are 3 characters named John
pronounceable
This
seems silly, but unpronounceable names can really interfere with readability
Dont
use weird abbreviations
smrhshscrtch
acimam
rngcmf
frnhtTmp
general idioms
Active
names for functions
getTime()
printTable()
validateCoords()
Nouns
for variables
numVertices
capacity
accuracy
If
you change the code, be sure to change the names if necessary
occasional
inaccuracy is worse than generally meaningless names
Formatting
formatting?
Good
formatting can make a huge difference
use spaces
carefully
indent lines
to show structure
use
parentheses to resolve ambiguity
spaces
In
many cases, the compiler ignores spaces and tabs
but the
human eye is very se nsitive to spacing and distance
things run
together if there are no spaces
![Text Box: for(n=0;field[n]!=\0;n++)](./Lecture19_files/image005.gif)
![Text Box: for (n=0; field[n] != \0; n++)](./Lecture19_files/image006.gif)
indenting
Weve
already discussed this a little
Indenting
statement blocks can really help show your programs structure
example

parenthesize
The
operator precedence and associativity rules remove ambiguity for the compiler
but that
doesnt mean its always crystal-clear for the reader!

dont get too clever
Some
things are perfectly legal C but dont make programs readable at all

abuse
Some
constructs (obviously the ?: operator is a good example) invite abuse
they can be
very powerful when used correctly
they can be
a nightmare when used incorrectly