-3

So I setup a code that finds the magnitude (absolute value) of the difference between the user's input and 51. If the user's input is greater than 51 the result is tripled. Not complicated. In an effort to minimize the code itself I came up with this.

// Compare and determine the correct output based on program's 
// paramters:
//
// - find absolute value of input - 51
// - if input > 51 then multiply result by 3
//-----------------------------------------------------------

int calcDiff(int x) {
      const int base = 51;
      int result     = x - base;    
      return x > base ? 3*result : (result < 0 ? ~result + 1 : result);
 }

So the question is:

Is this appropriate? I know it works but i'm more curious whether it's bad practice and can some how bite me in the rear in a big way some day. Is this just opinion or is it a big no no like using scanf or gets? Is there an emoji for beating a dead horse? Because i'd like to use it.

  • 3
    `return x > base ? 3 * (x - base) : (base - x);` seems simpler (ans is shorter). – Jarod42 Jul 03 '19 at 21:06
  • 5
    When you have 50 such a returns in your program and something will not work and you will need to debug it - you will understand why it is better to write several lines more and not doing those pseudo "smart" things – 0___________ Jul 03 '19 at 21:06
  • 3
    I'd complain if a co-worker showed this to me. You understand it today, but a year from now it will be way harder to understand than it should be. I don't consider it maintainable. Let the compiler do the optimization. That said, please read [ask] to see what it says about questions asking for opinions. – Dave S Jul 03 '19 at 21:07
  • Seems like you're trying to find the most complicated way to do the job. `"I'd like to use it"` - Is there any real reason to why you want to use this construct? – ProXicT Jul 03 '19 at 21:11
  • Why obfuscate `-result` as `~result + 1` (which is only equivalent if the implementation uses two's complement)? A straightforward translation of your specification gives `return result > 0 ? 3 * result : -result`. – molbdnilo Jul 03 '19 at 21:11
  • Oops, what to ask isn't linked from how to ask for some reason, see this - https://stackoverflow.com/help/on-topic – Dave S Jul 03 '19 at 21:13
  • Mostly just new to using the operator. Like a person learning a new word, you want to use it. But it got me to thinking. Just because it works and I feel neat for implementing it, does that make it right? Judging by the responses so far, I would say it's more than JUST opinion, it really would be a no no/dick move. – Alan Jurisich Jul 03 '19 at 21:13
  • @molbdnilo, honestly? Because until this post is didn't know I could use the - operator that way. – Alan Jurisich Jul 03 '19 at 21:37

1 Answers1

3
  1. There is nothing wrong about nesting conditionals in a return statement, per se.

  2. ~result + 1 is bad. You are attempting to negate result. The proper way to do this is simply -result. ~result + 1 relies on two’s complement representation, which is ubiquitous, but this expression is weirder and unnecessary.

  3. You do not need (result < 0 ? ~result + 1 : result). This expression is evaluated only if x > base is false, in which case result must be less than or equal to zero, and you want to return -result, as that handles both the less than zero case (return -result) and the equal to zero case (return 0, which is the same as -result when result is zero).

So the return statement can be written:

return x > base ? 3*result : -result;

or:

return result > 0 ? 3*result : -result;
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312