c# How to optimize repeated codes on inline if statement

I use a model class to create an object. My code works well but it has code repeat.

imagine this code below:

ModelA objectA = new ModelA
{
    x = A-B <= 0 ? 0 : A - B,
    y = C - D > 0 ? C-D : int.MinValue
   
    
};

This code below creates object inline. The problem is i must correct values when A-B < 0. Else it returns negative values. Consider all operations are type int.

What can we do for optimize this code?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

A possibility is to add a parametrized constructor to your class not having x and y as parameters but A, B, C and D.

public class ModelA
{
    public ModelA(int A, int B, int C, int D)
    {
        X = A-B <= 0 ? 0 : A - B;
        Y = C - D > 0 ? C-D : int.MinValue;
    }

    public int X { get; set; }
    public int Y { get; set; }
}

Now you can create new objects with
ModelA objectA = new ModelA(A1, B1, C1, D1);
ModelA objectB = new ModelA(A2, B2, C2, D2);

Optionally you can add constructor overloads.

// Default constructor. Allows you to use an object initializer.
public ModelA()
{
}

// Or pass x and y as constructor arguments instead of using an object initializer.
public ModelA(int x, int y)
{
    X = x <= 0 ? 0 : x;
    Y = y > 0 ? y : int.MinValue;
}

The last variant allows you to create an object like this, while having the constructor do all the range checks
ModelA objectA = new ModelA(A - B, C - D);

And of course you can do these checks in the property setters instead:

private int _x;
public int X
{
    get {
        return _x;
    }
    set {
        X = value <= 0 ? 0 : value;
    }
}

private int _y;
public int Y
{
    get {
        return _y;
    }
    set {
        Y = value > 0 ? value : int.MinValue;
    }
}

If you need mutable properties, this is safest version, as the checks are always performed, no matter whether you are using a constructor an object initializer or whether you mutate the property later in the object life cycle.

As always, you have a lot of options. Which one is the best, depends on your needs.

Method 2

Is your complaint that e.g. A-B<=0 ? 0 : A-B performs the A-B minus operation twice?

If so, why not:

ModelA objectA = new ModelA
{
    x = A<B ? 0 : A-B,
    y = C>D ? C-D : int.MinValue
};

e.g. “If B is greater than A then A-B would surely be negative and we want 0 not negatives, so put 0, otherwise do the math”

So you don’t have to do the math and look at the result in these cases, you can work out whether the result is unacceptable without doing the math


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x