• (nodebb)

    You have no business being a programmer if you don't understand mathematics beyond frist class.

  • Your Name (unregistered)

    A negative discount isn't. It's a surcharge. What's so hard about groking the term “discount”?

  • akozakie (unregistered)

    Running out of puns already? I don't even think you've ever used ternip, not to mention a ternado... Come on, you can do better!

  • Brian Boorman (google)

    The problem with the "abs multiplied by negative one" method is that it doesn't differentiate between a positive zero and a negative zero.

  • John (unregistered)

    I think you also missed the fact that PromotionDiscount itself is a string. I hope it's only used for display purposes.

  • Frank Wilhoit (google)

    This reminds me of the compound discount problem. The application was AR and CRM for a niche industry vertical. It was originally written in Visual Basic 3. The UI was based on very large forms, typically 30-60 fields, each of which was backed by a database record that literally represented the contents of the form. All of the business logic was in LostFocus event handlers, which were stateless, even though the business logic itself silently assumed that they were stateful and depended upon tab order through the forms.

    Discounts were attributes of customers (not the application vendor's customers, but THEIR customers) -- this customer gets, say, 10% off orders over $5000.

    The emergency arose when it was discovered that the customers' customers were sharing, among themselves, competitors though they nominally were, a little hack that went like so:

    Customer's customer (CC) calls customer (C) and says, ever so innocent like, I have a question about this invoice. The question would be phrased in such a way as to lead C's AR person to bring the invoice up on the screen and tab through it in such a way as to apply CC's discount again (and again, and again, ...) even though it had already been applied.

    This was surprisingly difficult to solve. In fact, the options came down to two: either migrate the app to VB6, or define the problem as a training issue. Guess.

  • minus1 (unregistered)


  • Scott (unregistered)

    A quibble: If this code is from the display side (not the administrative side), it's valid to allow for non-negative values. One shouldn't rely on some other tool being a) correct in validation and b) the only entry point for data.

    Now how they allow for non-negative values seems both clumsy and wrong. I'd prefer either an exception or setting to zero, but presumably they were given this business rule to implement.

  • 🤷 (unregistered)

    We can do better than that...

        PromotionDiscount = "";
    if((n.PromotionAmount.ToString()[0] != '-') == false)
        PromotionDiscount = "-" + (-(n.PromotionAmount)*(-1)).ToString();
        PromotionDiscount = PromotionDiscount.Substring(1, PromotionDiscount.Length - 1); //turns "--" into "-"
            //force a div zero exception, in case n.PromotionAmount == 0, else set it to the previous amount
            n.PromotionAmount= n.PromotionAmount / n.PromotionAmount * n.PromotionAmount;
            PromotionDiscount = "-" + (-(n.PromotionAmount)*(-1)).ToString();
            PromotionDiscount = n.PromotionAmount.ToString();
  • Jinks (unregistered)

    Umm... TRWTF is that the number shouldn't even be negative, it should be positive.

    A negative discount is an increase in price.

  • Best Of 2021 (unregistered)

    There's absolutely nothing wrong with a ternary like that and you've deliberately put the line breaks in the wrong place to make it look bad.

    If you lay it out thusly:

    PromotionDiscount = n.PromotionAmount.ToString()[0] == '-' ? n.PromotionAmount.ToString()
    : n.PromotionAmount.ToString() == "0" ? n.PromotionAmount.ToString()
    : "-" + n.PromotionAmount.ToString()

    ... then it reads exactly as easily as the corresponding if ... else block.

    Using string manipulation to do numeric checks is, of course, a WTF. Btu a construction like

    discountRate = x > 0 ? (positive case) :
    x == 0 ? (zero case) :
    (negative case)

    is absolutely fine.

  • Anon (unregistered)

    This ternary would be much more fun if written in PHP.

  • Fnord (unregistered) in reply to Best Of 2021

    As long as you're OK with NaN being lumped in with the negative case. Or should that be DISCOUNT_NOT_FOUND?

  • Best Of 2021 (unregistered) in reply to Fnord

    As long as you're OK with NaN being lumped in with the negative case

    Fair, if that's a floating point type, but that mistake would undoubtedly be in the if/else version too. Honestly almost all number handling code I've ever seen assumes that it isn't given a NaN.

    It would be kind of nice to be able to throw a flag somewhere to make any floating point operation that generates a NaN throw an exception immediately. For most business logic it's a sign that something has already gone wrong, no need to let it propagate 10 method calls away before something gets upset and throws.

  • Is That Really A Crosswalk? (unregistered)

    It's not unreasonable to assume that separate departments are responsible for the code behind the entry screen and the calculation.

  • ZZartin (unregistered)

    Or just AbsoluteValue(DiscountAmount) * -1.0

  • tbo (unregistered) in reply to Anon

    This is one of those language idiosyncrasies that's more problematic in the theoretical than the practical.

    Most PHP people I know don't use nested ternaries, just if/else. I would say if/else is easier to read in any language anyway.

  • (nodebb) in reply to tbo

    Plus the new version of PHP requires nested ternaries to be parenthesised.

  • Simon (unregistered) in reply to Jinks

    you get this more often than it seems. Because of the Advertising Standards Authority, the British ads sail very close to the wind on "negative discounts" and things of t hat kind. They also survey very few people - Procter and Gamble has one at the moment where 34% of 86 women agreed it made their hair look shinier. Now, doing the sums that means 29 women and a quarter agreed to their question, but leaving the quarter of a woman aside, it hardly matches the statistical standards of Family Fortunes.

    You get this all the time. The adman writes the prose, and cons. One that seems to have disappeared is "in real terms". We used to have that when we had high inflation. "In real terms" a potato is so much money. As opposed to being entirely imaginary or delusional terms. "It does not mean"' as Wilson said "That the pound in your pocket or wallet or bank has been devalued". ÖH YES IT DOES.

  • Simon (unregistered) in reply to tbo

    Never understood the hatred against ?:. I presume people prefer verbosity. The nice thing about?: actually is that it is an expression not a statement so has a value . that being said working out the type of the value is a bloody pain in the bum if the two sides are not the same type-expression, c++ does define what it will be, but you can spend all day working it out as it will probably be promoted then have an implicit cast or whatnot so good luck working out what type the result is actually going to be. But it is far more compact than four our five lines of if-else, I don't see what your problem is with that. If you can't read code, you shouldn't be writing it.

  • PedanticRobot (unregistered)

    Pretty much everything with that ternary is wrong.

    PromotionDiscount = n.PromotionAmount.ToString()[0] == '-' ? n.PromotionAmount.ToString() : n.PromotionAmount.ToString() == "0" ? n.PromotionAmount.ToString() : "-" + n.PromotionAmount.ToString();

    1. Causes unneeded garbage collection by instancing the same string two or three time instead of only doing it once.

    string value = n.PromotionAmount.ToString(); PromotionDiscount = value[0] == '-' ? value : value == "0" ? value : "-" + value;

    1. Nesting is pointless, the '-' and "0" checks should be a single || condition for a single ternary.

    string value = n.PromotionAmount.ToString(); PromotionDiscount = value[0] == '-' || value == "0" ? value : "-" + value;

    1. Most importantly, this isn't an ternary operation, ie do a thing or do an other thing, it's a binary operation, ie do a thing or do nothing.

    string value = n.PromotionAmount.ToString(); if (value[0] != '-' && value != "0") { value = "-" + value; } PromotionDiscount = value;

    There was no reason to use a ternary in the first place and, ignoring that, practically every choice made in using one was wrong.

  • Simon (unregistered)

    It is not as stupid as it looks as you have to consider the rules for EAR / APR to do the annual interest rate. This is where the payday loans companies got in trouble,. a bit unfairly really as their APR was some ridiculously high amount like 1259% or wgatever but nobody uses a payday loan company for a year so it was meaningless but were forced to advertise that way. I used payday loans companies not because I was short of money but it was cheaper than withdrawing from my long-term savings account and so on. If you compound the interest they charge yes you will come out with a ridiculous APR / EAR. Think of it a different way, You borrow a tenner off your mate and pay him back on Friday at the pub, and you buy him a pint to say thank you. The pint costs you 3 pounds. On a tenner borrowed for a week. The interest rate on that pint is, if you extrapolate it, absolutely ridiculous, you would owe him a kilderkin by Christmas. Short-term loans just do not fit into that model. I have a mortgage that is at an incredibly low rate of interest, but I don't put my house in hock every time I find myself short of cash. Same with credit cards. They tend to have quite a high rate of interest, 20-30%. But (if you're sensible) you don't put long-term debt on your credit card. They are a rainy day fund. So it is really a bit silly to talk about compounding interest here, where it shouldn't be compounded by any sensible borrower. Anyway, the equation for compounding is essentially a simple quadratic equation as any fule kno so I don't see what the trouble is here.

  • Sr dev (unregistered) in reply to Best Of 2021

    No no NO! That double ternary makes my brain melt every time I look at it? Which colon goes with which question mark? WTF is this thing doing? What happens first?

    If you put that thing in my code base I WOULD FIRE YOU ... by singing telegram!!!

Leave a comment on “A Terned Around Discount”

Log In or post as a guest

Replying to comment #:

« Return to Article