Coding Conventions, Styles, and Software Engineering Tips
Comments Specific to Labs 2 and 3
this.fieldname
to refer to
fieldname
inside the class definition. Sometimes it can
make things clearer.
Word wresult = new Word(w1 + w2); return wresult;
it would be cleaner to write it this way:
return new Word(w1 + w2);
On the other hand, you should create a variable when you are going
to use that value at least three times, especially if calculating that
value is expensive. (You don't want to calculate
An example: in lab 3, it is useful for testing purposes to know whether
you've fetched something from the cache or whether you had to go to main
memory. This is a borderline case in which diagnostic messages inside
the code is more reasonable. However, you can also create a public flag
Whatever you decide, you need to document your decision. Someone
reading over your code should be able to see that you have considered these
issues and resolved them.
On a related note, many of you did not think about how negative numbers
should be represented (especially those who used strings or arrays of
booleans to represent words). These are the kinds of issues that you need
to consider.
In general, don't make a field or a method public unless other classes
need to be able to access it.
Remember that, if CachedMemory extends Memory, that anything that is
in Memory is also part of a CachedMemory. This means that to get main
memory, you should call The only time that it makes sense to create an instance of Memory
inside CachedMemory is if CachedMemory does not extend Memory.
On a related note: it makes even less sense to reimplement Memory from
scratch inside CachedMemory. Use the code we give you!
On a related note, you should be able to specify the size of both
the cache and the main memory in the constructor.
cos(sec(tan(sin(3.14159))))
several times over if you can
avoid it.)
inCache
that is set to
true
if you got the data from the cache and false
if you had to get it from main memory. The driver could then (at its
option) examine this flag and print the appropriate message after
fetch()
returns.
Comments Specific to Labs 2 and 3
inc(Word(127));
? What should be
the result of dec(Word(-128));
? Should they "wrap around"?
Should they "stick" at the max and min values? Should you throw an
exception? Should you increase the size of the Word? There are several
possibilities, although some may make more sense than others.
super(main_memory_size)
inside
CachedMemory's constructor.
fetch()
, you should not
fetch data from main memory twice (one when you return the data, and
once when you store it in the cache, for example). Remember that we're
trying to simulate a real cached memory system, and fetching the same
piece of data from the cache twice in a row is exactly the sort of
inefficiency that caches were designed to reduce in the first place.
Fetching the data into the cache and then returning the data from the cache
makes more sense.