WPF If helper classes are bad, how do you properly eliminate duplicate code?

960 views Asked by At

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.

2

There are 2 answers

2
ismellike On

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.

3
sa_ddam213 On

"pull it up" does not always mean create a static helper class, regarding you example above you may create a base class fro your 3 pages that contain the same animation (but really this belongs in Xaml only)

public LabelPage : Page
{
    public virtual 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);
    }
}

And if you need to add/change the animation you can override in your class

public Page1 : LabelPage
{
    public override void SetUpAnimation()
    {
       base.SetUpAnimation();

       // more stuff to do;
    }
}

IMO there is nothing wrong with helper classes as long as they are not abused, personally I find the over use of Extension Methods to do helper stuff is way more of a problem then solid classes