Question:

OOP Overloading: How to best reduce duplicate code

by  |  earlier

0 LIKES UnLike

Hi, I'm trying to convince the "lead developer" not to duplicate code when overloading methods but corroborating "best practices" are hard to find.

When he overloads methods, he has the guts of the method duplicated across every overload. That methodology is *much* harder to manage IMO. I prefer having all of the main logic exist in one place (such as in a protected method) with all of the public overloads calling the primary method.

Does anyone have a link to confirm my premise with my lead?

If anyone has a better solution those are welcome too!

 Tags:

   Report

1 ANSWERS


  1. 20 years of encapsulation and modular development theory backs up what you are saying. Do not repeat yourself is a pretty solid mantra in the "best practices" circles.

    http://en.wikipedia.org/wiki/Don't_repea...

    --Note the "Once and Only Once" part which is more in line with what you are dealing with.

    Having overloaded methods call the same code I would contend is the standard, but not without exception (as is any development rule, eh?).

    If DoSomething(A) DoSomething(A, B) DoSomething(C) all call a private DoItAll(AMaybe, BMaybe, CMaybe) then odds are DoItAll will be a poorly written hack that tries to do too much.

    In my experience (10+ years for what its worth) more often then not the overloaded routines share more than one routine. This might be what you were suggesting anyway.

    DoSomething(A)

    --Code1

    --Code2

    --Code3

    --Code4

    DoSomething(B)

    --Code1

    --Code2

    --Code5

    --Code6

    Becomes

    DoSomething(A)

    -CallRoutineThatDoes(Code1 and 2) <--shared code pulled out

    --Code3

    --Code4

    DoSomething(B)

    -CallRoutineThatDoes(Code1 and 2)<--shared code pulled out

    --Code5

    --Code6

    Hope I could help...I feel your pain.

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.