Thread: Reducing code duplication from common code calling common class

  1. #1
    Registered User
    Join Date
    Apr 2014

    Reducing code duplication from common code calling common class

    I have a class 'A' which is almost perfect for my needs. Class 'B' uses class 'A'
    I've now designed Class 'C' and Class 'D' and noticed that there is a good chunk of code in class 'B', 'C' and 'D' for using Class 'A' is duplicated. I've separated out this code in specific, standalone functions in each of the classes.
    Now I'm wondering where this code should go. At the moment, the functions are duplicated in the three calling classes (B, C and D).
    Placing the functions into class 'A' would break the single responsibility principle. Inheritance to add functionality would likely break both SRP and LSP.
    The one that seems that it may work is composition.

    However, Is designing a complete class just for a few functions over kill?

    Would it be valid for classes 'B', 'C' and 'D' to access both the new class 'E' (which would depend on A) and the old class 'A' (which would have to be the same instance as the instance in the new class 'E'), or should the new class 'E' provide sufficient functionality so that Classes B, C and D don't need to access Class A directly? It would seem that its then an incomplete interface of the original object with additional functionality (ie, incompatible)

    Or should I do it a completely different way?

    (and why does software design require so much thinking?)
    Last edited by erty; 04-13-2014 at 02:37 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Does the code that uses class A require access to the internals of classes B, C and D? If not, then one approach is to move the code that uses class A to a non-member non-friend function template. Another approach is to define an abstract base class for B, C and D to inherit from such that you can write a non-member non-friend function that uses the interface provided by the abstract base class to implement the code that uses class A. Maybe these functions can form an interface provided by your hypothetical class E (or maybe they should remain separate, it depends).

    If the code that uses class A does require access to the internals of classes B, C and D, then it could mean that there is a deficiency in the interface provided by these classes, or maybe they are doing too much and part of them should be moved to your hypothetical class E.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    If the class A is constantly pressed into service in a particular way, i.e. there is a "magic recipe" for how an A object is used, then you have an indicator that the interface of A is not what it needs to be for the users of A. Providing a sane and usable interface is not a "second responsibility," it is just part of the design of the class.

    You'd have to give a pretty good explanation of why the interface of A isn't broken even though every user of A has to jump through the same hoops while using it.
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);

  4. #4
    Registered User
    Join Date
    Apr 2006
    Just make the common code as methods in class A. Or if they have other state, then yeah class E could be the way to go, as you describe.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008

    Can you give us a legitimate, if incomplete, example of the "hoop jumpery"?

    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code for getting the GCD (greatest common divisor)
    By Noobpoint in forum C Programming
    Replies: 1
    Last Post: 03-07-2012, 07:13 AM
  2. How to handle multiple cases which use common code?
    By tmaxx in forum C Programming
    Replies: 3
    Last Post: 10-03-2008, 07:42 AM
  3. Reducing my code
    By limitmaster in forum C++ Programming
    Replies: 7
    Last Post: 01-09-2008, 07:05 AM
  4. Some (common?) code errors...
    By FlyingIsFun1217 in forum C++ Programming
    Replies: 10
    Last Post: 05-14-2007, 05:12 AM
  5. reducing a fraction/greatest common factor
    By dbaryl in forum C Programming
    Replies: 2
    Last Post: 07-21-2002, 03:05 PM