Monday, March 06, 2006

Objects make your code expandable

thats pretty much it. I am a big fan of oop and am working on a project that is not. It now takes me so much longer to do anything and it has a huge impact on the code base and its consumers.

I have one method like this:

CreateCashout(clerkId as integer, officeId as integer, cashTotal as double, creditCardTotal as double, debitTotal as double, PaymentIds() as integer)

This is then validated and passed to the same method signature in the data layer.

So for one I have a long method signature. Some of these items will be zero (i.e. debit totals for some consumers) so why make them put it in at all.

Also I would like to see what payments were cash, credit card, and debit but right now I just know all the payments by the paymentId array.

I would create an object like this:

1 Public Class Cashout


3 Private mClerkId As Integer

4 Private mOfficeId As Integer

5 Private mCashTotal As Double

6 Private mCreditCardTotal As Double

7 Private mDebitTotal As Double


9 Private mCashPaymentIds() As Integer

10 Private mCreditCardPaymentIds() As Integer

11 Private mDebitPaymentIds() As Integer


13 'properties for accessing ClerkId, OfficeId, CashTotal, CreditCardTotal, and DebitTotal


15 Public ReadOnly Property PaymentIds() As Integer()

16 Get

17 'just an example. I have not tested this

18 Dim allIds(mCashPaymentIds.Length + mCreditCardPaymentIds.Length + mDebitPaymentIds.Length) As Integer

19 mCashPaymentIds.CopyTo(allIds, 0)

20 mCreditCardPaymentIds.CopyTo(allIds, mCashPaymentIds.Length)

21 mDebitPaymentIds.CopyTo(allIds, mCashPaymentIds.Length + mCreditCardPaymentIds.Length)

22 Return allIds

23 End Get

24 End Property


26 Public Sub New(ByVal clerkId As Integer, ByVal officeId As Integer, ByVal cashTotal As Double, ByVal creditCardTotal As Double, ByVal debitTotal As Double)

27 Me.ClerkId = clerkId

28 Me.OfficeId = officeId

29 Me.CashTotal = cashTotal

30 Me.CreditCardTotal = creditCardTotal

31 Me.DebitTotal = debitTotal

32 End Sub


34 Public Sub New(ByVal clerkId As Integer, ByVal officeId As Integer)

35 Me.ClerkId = clerkId

36 Me.OfficeId = officeId

37 End Sub

38 End Class

And the implementation for just creating a cash cashout would now look like this:

1 Dim cashout As New Cashout(clerkId, officeId)

2 cashout.cashTotal = 30.00

3 cashout.CashPaymentIds = cashPaymentIds

4 CreateCashout(cashout)

Notice how I now enforced that at least the clerkId and OfficeId are mandatory by having it in both New constructors.

Also note that in the new constructor that I assign the parameters to properties and not to the fields. i.e. I assigne cashTotals to me.CashTotals instead of mCashTotals

The reason for this is that there is now a central point to validate all data if need by. i.e. lets say that CashTotals can not be negative we would put the check in the property and be done with it. If we directly assigned it to mCashoutId in the new constructor we would have to check once in the new constructor and once in the property as there are now two ways to set the value of mCashTotals.


Post a Comment

<< Home