Re: OOP style



So far, in the app I'm working on, most of the time we're dealing with
singletons. There is little need for code reuse in the code I write.
Most of the reusable code is drawn from 3rd party libraries of
components. So it's largely aesthetics for me, as well as making it
easier to understand the code when I go to debug or add a feature.

I'm not sure if my approach reflects inexperience, or simply a
different paradigm. I'm learning more about OOP and Agile approaches
as I go, but it's hard to work out the details, especially with very
few people around I can talk to. I glad there's newsgroups!

Example of what I'm working on right now. (Rewriting someone else's
code):

A form which is basically a settings screen. When it's time to save,
several things have to happen:
validate the fields (make sure required fields aren't empty, make sure
the database server is alive at the drive letter the user indicated),
if this is a new user create the database record, create their working
databse tables, copy some files, write to an ini file. If this is
modifying an existing user, post the data, and see if they have the
current version of the database tables; set some global variables.
Handle errors, make sure the state of things is always valid.

Generally, for each thing that I can enumerate, I write a method for.
So I have methods called: requiredFieldsValid, DBConnectOK,
createNewUser, createNewDataDir, copyFiles, DeleteDataDir,
needsDBUpdate, getCurrentDataVersion, setGlobalVariables, and so forth.
These methods probably average 10-15 lines each. I also put a sentence
or two as a comment defining what the method is supposed to accomplish.

Then the Save button handler is simply method calls, such as:

if not RequiredFieldsValid then exit;
if newUser then
if not createNewUser then exit;

etc.

It originally was one line after another -- when I copied it into Word
to print out, it was 2.5 pages long (8 pt Courier font).

Programming seems similar to writing an essay -- get it all out, and
then revise and streamline it.

-Corinna

.


Quantcast