Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8458979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:13:26+00:00 2026-06-10T13:13:26+00:00

In the example I’m thinking of I have about 4 lines of code that

  • 0

In the example I’m thinking of I have about 4 lines of code that could be encapsulated by a function, and this function will surely be used in other classes in the same hierarchy.

I have the following options for reusing that code:

  1. Copy paste the function around to the classes that need it.
  2. Make a base class for the classes that need the function and put it there.
  3. Make a class that contains the function which gets passed into the classes that need it through DI or is just a member of the class. (seems like major overkill)
  4. Make a static utility class and put that method in it.

I definitely wouldn’t do 1 or 4. I would have done 2 in the past but I’m trying to keep to the composition over inheritance principle so I’m leaning towards 4 however it seems like a lot for something that will most likely never be used outside the hierarchy and is only 4 lines. I know this is very nitpicky but I want to figure out the right way to do it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:13:27+00:00Added an answer on June 10, 2026 at 1:13 pm

    Inheritance was created for a reason. The fact that it has been overused doesn’t mean that it doesn’t have legitimate uses. The key is that whether you use it should not depend on whether you can get easy reuse out of it, but whether it makes sense for it to belong to the base class, based on what your base class represents.

    Without better understanding what your classes are, and what the method is that you’re trying to reuse, I can’t give specific advice in your particular case. But think of it this way: When you say it will “most likely never be used outside the hierarchy,” is that because it purely just doesn’t make sense outside of that hierarchy? Or is it just that you don’t think somebody’s going to build something that happens to use this feature, even though it could conceivably make sense outside of the hierarchy?

    If this method would make any sense outside of the specific hierarchy you’re talking about, I would suggest approach #3.

    And of course, all of this assumes that your class hierarchy is really a hierarchy in the first place. Another common abuse of inheritance is when people force a hierarchy on objects that don’t need to be hierarchical in the context of their application.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Example: var sessions = require('sessions'); function load_session(req) { sessions.load(sid); } Now that the session
Example class code: <?php class example { public function forExample() { return true; }
Example I have this array: @packages = [2, [4, 2, 1], 4, [4,2], [5,
Example: I have this block, and I want to update a label on the
Example 1 : function a(){ return ok; } function b(){ this.c = a; }
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Example: print max(chain_length(i) for i in xrange(1,10001)) This returns the maximum/biggest chain_length (an arbitrary
Example CSS #wrap{margin:20px} Code prettify wraps the whole line in .com <span class=com>#wrap{margin:20px}</span> Somebody
Example, I have billions of short phrases, and I want to clusters of them

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.