On 26/06/2009, at 2:27 AM, David FOXWELL wrote:
New procedure: validateClientName( name )
calls validateName( name : CLIENT_CONSTANT )
New procedure: validateCompanyName( name : CLIENT_CONSTANT )
calls validateName( name : CLIENT_CONSTANT )
Hmm, I see I cloned code and did not properly edit. The
validateCompanyName() procedure should have used the COMPANY_CONSTANT
but I presume you understood that.
I'd then have something like
New procedure: validateAddressStreet( street : ADDRESS_CONSTANT )
calls validateAdress( Address : ADRESS_CONSTANT )
Wouldn't I end up with another function validateAddress identical to
validateName?
If the only thing you check is the characters contained in the address
then yes, the functions will likely be identical. However, you should
be considering the purpose rather than the code. An address is a
different sort of thing from a company or a client. Even if the only
validation you perform currently is to limit certain characters you
will probably expand that in future (or you should allow for it in
your design).
validateAddressStreet() is too limited. An address consists of
multiple components and should be treated as a single entity.
Therefore validateAddress( anAddressStruct ) would be better. This
function can then check whether the postal code is appropriate for the
city or suburb in the address. It could use a web service or external
function to determine whether the address is in fact real. There are
many things that could be done to an address that would be
inappropriate for a company or client name. That's difference number 1.
Charles Wilt pointed out difference number 2. The ease of maintenance
caused by properly chosen names. It becomes much easier for a later
developer to see where a particular change belongs in the code and it
becomes easier to make that change with less effort and less disruption.
Jon Voris mentioned reason number 3. Self-documenting code. That's a
good thing and far outweighs even well-commented code.
I think the major difference and main benefit is that this approach
causes you to change how you think about things. You begin to think in
terms of objects, object relationships, and responsibilities. That
will not only improve your RPG but will help you with other languages.
Good design skills transcend language skills. Some things are easier
to implement in one language vs. another but good design is everything.
In fact, isn't validateName just encapsulating the %CHECK BIF?
In this very limited example that's true but it is reasonable to
expect that validating something will likely include more than just
checking for certain characters. In that case using a procedure to
encapsulate the simple check now will allow you to easily add
additional checks later. Charles pointed out the costs of maintenance
far exceed the cost of initial development. Imagine the pain caused by
either adding an additional check to all the code that uses %CHECK
directly or converting all that code to use a new procedure (that
could have been implemented during initial coding).
Why not therefore, instead of
validatClientName ( name ); which is self documenting,
// Validate client's name.
%CHECK ( ClientName : CLIENT_NAME_CHARACTERS ); with a comment.
Reason 1: self-documenting code vs. code that requires a comment to
explain
Reason 2: encapsulating the function (WHY is this code here) vs. the
code itself (WHAT is this code doing)
Reason 3: easier maintenance in future. Even if you think this
function will NEVER change you have made that possible with very
little extra cost by using a procedure. If you stick with the embedded
code solution you have saved a little time and money now but have
exposed a potentially large cost later.
Reason 4: Cleaner coding style that makes it obvious WHY something is
being done.
I hope I'm not missing the plot.
A bit ... but you're making progress ... and you're at least
interested in this which puts you several steps ahead of many other
RPG developers.
Regards,
Simon Coulter.
--------------------------------------------------------------------
FlyByNight Software OS/400, i5/OS Technical Specialists
http://www.flybynight.com.au/
Phone: +61 2 6657 8251 Mobile: +61 0411 091 400 /"\
Fax: +61 2 6657 8251 \ /
X
ASCII Ribbon campaign against HTML E-Mail / \
--------------------------------------------------------------------
As an Amazon Associate we earn from qualifying purchases.