Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OOP design of little helper classes

Lets say I have a class C that is doing some job.

For that I need a little very simple helper class H (e.g. representation of a pair or a 3-tuple). H is only needed in C.

I would put H inside of C.

class C
{
   void foo ()
   {
     // ... use H to do the job more easy ...
   }

   class H
   {
      // very simple and contained stuff
   }
}

Is putting H inside of C a good idea? I do it to have it contained and at the place I think it should be and nowhere else. But I am not sure it is good design.

like image 857
chris01 Avatar asked May 15 '19 06:05

chris01


People also ask

What is a helper class in programming?

In object-oriented programming, a helper class is used to assist in providing some functionality, which isn't the main goal of the application or class in which it is used. An instance of a helper class is called a helper object (for example, in the delegation pattern).

Are helper classes anti pattern?

asawyer pointed out a few links in the comments to that question: Helper classes is an anti-pattern. While those links go into detail how helperclasses collide with the well known principles of oop some things are still unclear to me. For example "Do not repeat yourself".

What is helper method?

A helper method is a small utility function that can be used to extract logic from views and controllers, keeping them lean. Views should never have logic because they're meant to display HTML.

What is helper CSS?

What is a CSS Helper Class? CSS helper classes, otherwise known as utility classes, are a CSS methodology that allows us to write less repetitive, modular code. This is done by creating a set of abstract classes that are responsible for doing one thing and one thing only.

What are helper class standards?

The Helper Class standards will aid to a better understanding of the intent of the underlying Software Entity. By focusing the code implementation, within the Code Block on the Single Responsibility, and not the Details of how that Responsibility is being carried out for the calling Software Entity, Helper Methods support Open/Closed Principle.

What are the design principles of OOP?

According to tho this OOP design principle, “Classes, methods or functions should be Open for extension (new functionality) and Closed for modification”. This is another beautiful SOLID design principle, coined by Uncle Bob on his classic Clean Code book, which prevents someone from changing already tried and tested code.

What is the base class/helper class methodology?

My company used to use the Base class / Helper class methodology where each object would have two classes. You would have a Person class that contained all of the class properties and definitions and a PersonHelper class that contained all of the methods, SQL statements and Logic that manipulated the Person class.

What are public helper classes in Java?

Public Helper Classes are a critical part of one of the Pillars of Object Oriented Programming (OOP): Encapsulation. By creating these Software Entity Types we are also complying with the “O” in S.O.L.I.D. Design Principles: The Open / Closed Principle.


Video Answer


3 Answers

That is a very good design. The visibility of the classes, variables or methods should always be limited to the only required scope, never more.
If later this class needs to be used outside the inner class, just move it somewhere else.

like image 147
davidxxx Avatar answered Oct 31 '22 06:10

davidxxx


More of an addendum:

First: if H objects are supposed to exist without an owning C instance, then H should be static. You might even want to make it private.

Then, keep in mind: you have an inner class here, so H's full name is actually something like whatever.package.com.C.H. In other words: you really tied H to C.

What if you decide at some point that H has enough other aspects that make it worth to be "its own" thing? Then you might have to touch some more code, to turn your whatever.package.com.C.H into whatever.package.com.H.

Thus you could consider to make H a toplevel class, but to not make it public. So you keep it in C.java, but not as inner class!

( but of course, that really depends on "style", I think that the Google java coding standard tells you to only have one top level class per .java file)

Final words: if there is a way to turn your H class into something that could be used in other places, then I would go forward and right away make it a public class of its own. If re-using seems reasonable, write your code so it can be easily re-used! And a 3-value tuple doesn't sound like it is very specific to anything, so it sounds like a candidate for re-use.

like image 20
GhostCat Avatar answered Oct 31 '22 06:10

GhostCat


Yes it is good design. What you're trying to do is called inner class.you can also make it private. If it doesn't need exclusive access to the members of the outer class, make it a static nested class , it will require less memory.

like image 29
Srinivasan Sekar Avatar answered Oct 31 '22 06:10

Srinivasan Sekar