From: Andrew McDonagh said:
You created 15 classes all copied and pasted, then edited each one to
make their variable names and error messages reflect their real intent?
There were some other things different: Each class was used to retrieve
data from a different table in a relational database, each related to
other tables in different ways. Some tables were leaves, with only a
primary key and table-specific data. Other tables were parents with
foreign keys linking to records of other tables. In addition to the
main body of the code in each class that simply named the table
differently and named the variables and texted the error messages
differently, there were variations as to the additional fields beyond
the primary key, the name of the primary key, and the name and linkage
of foreign key, including instance fields that link lower-level records
into the parent record. So while I could possibly have factored out
some of the basic code in cases where the fields in the record matched
up exactly and only their names varied, that would have been a lot of
work and still I'd have to deal with all the pieces that didn't line up
such extra fields for some records and foreign keys with corresponding
instance-variable links.
So maybe I told only a half-truth when I described only the parts I
copied and renamed and failed to mention the parts that had to be
written anew for many of the classes.
Also, this was just a homework assignment to see if we could design a
structure of database tables appropriate for this data model and write
a Java JDBC program with a different class for each table to
appropriately reference the database, and get the whole thing working,
our very first non-trivial JDBC program ever. If this had been a
serious program to be maintained for years with real users, I would
still have used copy*paste to get the first draft working, to see if
the users are satisfied with the behaviour, then if there was extra
funding I would have refactored it to make the design cleaner. But this
was just one-time throwaway code for a class assignment, never to be
used again, so if I spent time refactoring before turning it in I would
have been late and gotten only half credit, and if I had turned in that
first draft but then spent effort refactoring it on my own, all that
effort would have been wasted because (1) nobody would ever see the
refactored version, and I wouldn't get extra credit for that additional
work, (2) I would have spent time on that instead of on my next
homework assignment thereby detracting from it, and (3) I already know
how to refactor and have done it many times whereas I never did a
serious JDBC program before and so the original copy&paste program
taught me useful stuff about various SQL commands I'd never used before
whereas refactoring would have taught me nothing new I didn't already
know.
So will you forgive me for writing throw-away copy&paste code in a time
crunch when nothing more was required or wanted?
Why not create one class which at runtime you give it the error
message etc?
Because I'd have to figure out a way to parameterized all the other
aspects of the various database tables that aren't uniform, i.e. invent
a whole new language for representing the relationships between
database tables, such that I could have a single db-relationship
compiler or emulator that somehow handled both cases of uniformness
between tables and cases of serious differences between tables, or else
have code that is half refactored and half not which would have been
uglier than having it *all* uniformly copy&paste.
By the way, in Lisp it would have been easier to refactor and write an
emulator because the syntax for data structures as literals exists
within the language. In Java by comparison I would have needed to write
the syntax for table relationships in XML and then learn how to parse
XML using DOM (because SAX would have been too painful for such an
application), and we weren't supposed to do any XML parsing yet at that
point in the course. As it turns out we *never* were required to do any
XML parsing in that class. But a week and a half ago during an idle day
I got the urge to teach myself XML parsing, and in about six hours I
taught myself enough to write a JSAX parser for a simple XML file
format I invented for specifying updates to a database, where my
program maintained a stack of XML syntax it was inside, used that to
fully validate the XML structure, and built a hash table of each update
request, and upon end-of-element event it converted the hash table into
a SQL command to perform the update. But I never got credit for that
because it wasn't an assignment for the course. The next day after that
I spent a similar amount of time to teach me JavaScript. I was going to
teach myself DOM next, but haven't gotten around to it yet, but I
browsed that part of the XML-parsing tutorial and it looks like the
actual parsing will be very easy, and I don't plan on building a JTree
so I can skip the rest of the tutorial. Instead I would use BeanShell
to interactively explore the DOM tree that has been built. But like I
said there hasn't been another day where I felt like doing such a
thing, so-far anyway.