Notes for June 22, 2005
This document supplements the readings, so there won't be everything we
talked about in class on this page.
Abstraction
One of the most important ideas in computer science is the notion of abstraction: ignore
unimportant details so that we can think about things at a higher level. Think of a radio.
Knowing which transistors heat up when you turn on the radio is unimportant as long as the radio
turns on. Even knowing that there exist transistors is also unimportant. If there were a bird in
the radio (a la The Flintstones), it would not
matter, as long as the radio continues to deliver broadcasts.
So what does this have to do with programming?
The job of the programmer is to write code that is useful for a client to use, but how
the code is written is irrelevant as long as it behaves according to the specifications
desired by the client. With respect to the IntList class, the data fields
elementData and size should be declared private, so that the client
cannot even know that these variables exist. This is abstraction at work! The client is able to
use the code to perform some higher-level function, while the code itself deals with the
nitty-gritty details. For all the client knows, the IntList could be implemented with a
linked list (which we'll cover next week) instead of an array.
Debugging:
When debugging your code, it is very important to test things piecemeal and from the simplest cases
to the most complex. If you start with the most complex method that depends on the simpler methods
and things break down, you can never be too sure where in the process it broke down. Always test
your code a lot and don't forget to try out corner cases. What happens if you add something
to the beginning of the list? to the end? And finally, don't forget: System.out.println
is your friend.
Reading:
In the reading,
there are several words that are boxed. Those are important words to know--words like
constructor, accessor, state, etc...
A word on style:
Indent your code properly: It is important that a person reading your code can immediately see
the structure of the code. If everything was flushed to the left, the code would just look like
gibberish.
Use comments wisely: You have to strike a balance between writing too little comments
and writing too many comments. A word of advice: one comment per line is too much. Basically, you
should comment at the top of every method with a brief description of what it does, unless it's a
method with a one-line body that is self-evident like accessors. Feel free to use the
precondition and postcondition style of commenting like in the IntList
example. There is no hard rule though. Programmers develop their own style of commenting. Try to
be concise without being obscure.
(Fine. That was actually many words on style...)