Why helper classes

A recent question made me rethink that all helper classes are anti-pattern.

asawyer pointed out several links in the comments on this question: The classes helper is anti-pattern .

While these links detail how helperclasses clash with well-known principles, some things are still unclear to me.

For example, "Do not repeat yourself." How can you do this without creating your assistant? I thought you could get a specific type and provide some functions for it. But I believe that this is not practical all the time.

Let's look at the following example: please keep in mind, I tried not to use any higher language features, rather than "language specifics". So this may be ugly nested and not optimal ...

//Check if the string is full of whitepsaces bool allWhiteSpace = true; if(input == null || input.Length == 0) allWhiteSpace = false; else { foreach(char c in input) { if( c != ' ') { allWhiteSpace = false; break; } } } 

Lets create a bad helper class called StringHelper, the code gets shorter:

 bool isAllWhiteSpace = StringHelper.IsAllWhiteSpace(input); 

So, since this is not the only time we need to check this out, I think the “Don't Repeat Yourself” is filled here.

How can we achieve this without an assistant? Given that this piece of code is not associated with one class?

Do we need to inherit a string and call it BetterString?

 bool allWhiteSpace = better.IsAllWhiteSpace; 

or are we creating a class? StringChecker

 StringChecker checker = new StringChecker(); bool allWhiteSpace = checker.IsAllwhiteSpace(input); 

So how do we understand that?

Some languages ​​(like C #) allow you to use ExtensionMethods. Do they count helperclasses? I prefer them to belong to helper classes.

+6
source share
1 answer

Disclaimer: The following answer is based on my own experience and I am not doing the right and wrong approach.

IMHO, helper classes are neither good nor bad, it all depends on your business / domain logic and your software architecture. Here Why: say that we need to realize the idea of ​​the white spaces that you proposed, so first I ask myself. When will I need to check for spaces? Therefore, imagine the following scenario: a blogging system with users, posts, comments. So I would have three classes:

 Class User{} Class Post{} Class Comment{} 

each class will have a field that is a string type. Anyway, I will need to check these fields to create something like:

 Class UserValidator{} Class PostValidator{} Class CommentValidator{} 

and I would put my validation policies in these three classes. But WAIT! Do all of the above classes need checking against zero or all spaces? Ummmm .... the best solution is to take it higher in the tree and place it in the Father class called Validator:

 Class Validator{ //some code bool function is_all_whitespaces(){} } 

so if you need the is_all_whitespaces(){} function to be abstract (using class authentication also abstract) or turn it into an interface, which will be another problem, and it mostly depends on your thinking. Let's go back to the point in this case if I had classes (for example):

 Class UserValidator inherits Validator{} Class PostValidator inherits Validator{} Class CommentValidator inherits Validator{} 

In this case, I really don't need an assistant. but let's say that you have a function called multiD_array_group_by_key and you use it in different positions, but you don’t like to have it in any structured OOP place that you can have in some ArrayHelper , but by doing so you are behind the object oriented approach.

+7
source

All Articles