2

Could someone help me with the c# code. I want to calculate new a value- the value is calculated in the way: a=a-2*b, than to see if the result is less than zero and if it is in the range (0,a). I am usually doing that in few steps, but I have found a code on the internet which looks much better than mine, and the explanation of the problem the code solves is like mine, but I am not sure if the code is written in the proper way or not, because it doesn't give me the correct result. Also, there is no reported error in code.

 a = a - 2 * b < 0 ? 0 : a;

Is the code ok for the thing I need, or not?

9
  • Is it (a - 2) * b or a - (2 * b) you're trying to calculate? Because default operator precedence will cause the multiplication to occur first, then the subtraction. Also, what does your code look like? Commented Jun 25, 2015 at 12:32
  • I want first to multiple 2*b Commented Jun 25, 2015 at 12:33
  • That code sets a=0, if a - 2 * b is less than 0. if not, it sets it to a's value Commented Jun 25, 2015 at 12:35
  • The origine code which I found on internet was angle=angle-2*angle1<90?0:45, and it was written that the code is checking if the new angle is less that 90 degree and if it is in the range 0-45. So I just put the variables I use in my calculation. Commented Jun 25, 2015 at 12:36
  • 1
    Note that a = Math.Max(0, a - 2*b); is IMO much more clear. Commented Jun 25, 2015 at 12:41

4 Answers 4

4

The code you posted could be written like this, maybe this helps clear things up:

        if (a - 2*b < 0)
        {
            a = 0;
        }
        else
        {
            //this assignment is not needed it is just here for clarification
            a = a;
        }

And btw i want to mention, it is not realy important how compact code is, it is most important how easy it is to read for you and others. So if you can read if else statements better then use them.

Oh well 2 slow ;)

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for explanation. I understand now where the problem was.
I disagree with you about 'it is not really important how compact code is' - it's true only in case when code is lumped together. But if you can do something in 1 line of code and this code is fairly easy to read - it's always better than if you do it in 5 lines. And it's very important. An example of bad code is nested 'if' statements when you just need one 'if' or series of LINQ queries like .Where(condition).Where(condition2)... when you can just do .Where(condition && condition2). Keep your code nice and clean!
Well the important thing is that it is easy to read, not just for you but for others. Often if we do code in a more compact way like we have here in this topic, it is not easy to read for everyone espacally for beginners. So why should a beginner use it? If all people in your team are familiar with that its fine unless someone else is coming who is not familiar.
3

Your code is this:

int a;

if((a - 2 * b) < 0)
{
    a = 0;
}
else
{
    a = a;
}

Which doesn't make sense, because you set a = a. What i think you want is this:

a = (a - 2 * b) < 0 ? 0 : (a - 2 * b);

1 Comment

Thank you so much, now I understand why it doesn't give me good result :)
1
 a = a - 2 * b < 0 ? 0 : a;

That is a shortcut for:

if (a - 2  * b < 0) {
  a = 0;
} else {
  // a = a is a no-op.
}

which does not seem to match your explanation.

I suspect you want:

var oldA = a;
a = a - 2*b;
if (a < 0) {
  // do something
} else if (a < oldA) {
  // do something else.
}

Comments

1

You may also want to use switch statement:

int newA = a - 2*b;
byte option = newA < 0 ? 0 : newA < A? 1 : 2;

switch(option)
{
  case 0 : // your code for NewA < 0  break;
  case 1 : // your code for NewA < A  break;
  case 2 : // your code for NewA > A  break;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.