users
[Top] [All Lists]

RE: [cinjug-users] Constants imply DP (was: Question on last Monday's me

To: "'Mark Windholtz'" <windholtz@xxxxxxxxx>, <users@xxxxxxxxxx>
Subject: RE: [cinjug-users] Constants imply DP (was: Question on last Monday's meeting)
From: "Jim Cook" <jimcook@xxxxxxxxxxxxx>
Date: Fri, 25 Feb 2005 12:17:46 -0500
Delivered-to: mailing list users@cinjug.org
In-reply-to: <3a4860b4050225083956af8634@mail.gmail.com>
Mailing-list: contact users-help@cinjug.org; run by ezmlm
Thread-index: AcUbWJ3kas65ZjHEQ9Sesh9kwPya0AAAR/CQ
Mark, I am still kicking myself from not attending your talk.  <ouch>   From
what I am gathering from these notes, I think you have made some very
profound observations.  


The use of constants in code just hit me recently with a defect in some code
I looked a recently. The code has a unique number used in two places, but
was only changed in one place when the unique number changed.  The way I
fixed it is using a constant and I am looking for a suggestion on how to
properly fix it.  For sake of simplicity, I will give a hypothetical
scenario:

Let's say we have an application that computes the total cost of a car
repair.  We have an html page that has two input fields (1) hours of labor,
and (2) cost of parts.   What our application does is takes the hours of
labor and multiplies it by a rate (The Constant In Question) and adds the
cost of the parts and stores the total in a relational database as a total
cost. This computation and what not are done on the server using Java.
Simple enough and everything is fine.  Assume the application is a
wonderfully designed OO application using MVC design pattern.

Now, the customer of the application wants a change.  They want it as soon
as a person enters the hours, on the web form, they want the cost to be
displayed.  This is implemented in Javascript and the Javascript needs to
know the rate as well in order to display the rate immediately on the form.
I solved this by using a constant and the view code that generates the html
(and Javascript) refers to the constant in the model code (e.g. Model.RATE).
Obviously the logic to produce the total is duplicated (Once in Java and the
other in Javascript) but I cannot see a way around that. It seems like the
use of a constant is a good thing in this case. I suppose I could have used
a Model.getRate() as opposed to Model.RATE, but it doesn't seem to me that
in this case one outweighs the other. 

Any suggestions on how I could solve this without using constants? 

  
















 



<Prev in Thread] Current Thread [Next in Thread>