So I spent a night tonight to read about helper classes, and there are a lot of people that advice to avoid helper classes. I loved the brutality of this guy: http://simpleprogrammer.com/2010/04/12/should-i-leave-that-helper-class/ . That made me to ask myself, what else if not helper class then?
So in my WPF application I have 3 pages. On each page I have Label and I use this method for purpose of animating it:
public static void SetUpAnimation()
{
DoubleAnimation doubleAnimation = new DoubleAnimation();
doubleAnimation.From = -nameLabel.ActualWidth;
doubleAnimation.To = nameCanvas.ActualWidth;
doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
doubleAnimation.Duration = new Duration(TimeSpan.Parse("0:0:10"));
nameLabel.BeginAnimation(Canvas.RightProperty, doubleAnimation);
}
So now I created static class LabelAnimator and put this method up, everything is ok. But what happens when I add more code that is not in connection with this animator, and I use it in other pages too. For example Timer that updates that label. Code is identical, which means I will copy paste it on each WPF page(class).
By this approach I would have to make another helper class like LabelTimer. Which means I would have 2 classes with 1 method each.. I don't know how much waste is this, but what worries me more is this ok approach at all, is there better way than doing helper classes for each method like this?
But that is just an example, don't tell me what should I do for this functionality, but global for any method that is duplicate between classes.
As I read here: https://softwareengineering.stackexchange.com/questions/107458/how-to-remove-duplicate-code-in-general the proper way to eliminate duplicate code in classes of the same hierarchy is to extract method and pull it up, which as I understand is the thing I am doing. Putting the method to helper class.
But is that good? Having 10 different helper classes(if you don't want to put all methods in one helper class, and make it very very messy) is the only option to eliminate duplicate methods on the same class hierarchy?
Back in days, when I was little kid, I always worried that programs will work, now that I am big kid I have to think about things like this. But I love coding, because of things like this ;)
Thanks for any advice.
It prevents the code from being reusable and will only help one part of your code, which in that case you don't even need a new class.
You can just put the extra code outside of the class you are calling.