Ad
  • Custom User Avatar

    Well, that's what I've been saying above: the code itself should be as concise and elegant as possible, while the intent or explanation of the code should go in the comments. Dumbifying the content of the code itself just to make less competent people able to follow, at the expense of clean and concise code, is the real wtf and unacceptable thing.

    (By the way, the explanations have been provided down the comments below.)

    But then, I'm not surprised that lots of newbies went down the path of "why you no spoonfeed me noob code". The fact that OP get something like 31 score is mind-boggling yet anticipated. It's really something that's even more basic and fundamental than "coding fundamentals"...

    p.s If anything I want to bash this OP more for his blatant and shameless "pick-a-fight-and-run" tactic. After all, you've picked a fight yourself, and then you're just going all "you misunderstood me, I do not wish to discuss any further" and try to get away with it like nothing happened? That's not going to happen, mate. There are consequences of picking up a wrong fight, and you need to face it.

  • Default User Avatar

    Good point.But you post at the wrong place.

    This code is simple and just doing one thing.It's not unreadable.

    If you don't know what the code is doing.You should learn.

    It's faster than the normal code.If you have an cost-efficient issue,it will help you.

    For honestly,you should have learned if you are studying Computer Science(At least in our country).But we just don't know where to use it.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    Uhm, git gud then?

  • Default User Avatar
  • Default User Avatar

    Voile
    "you're accusing the wrong one-liner here. This one-liner is so simple what it does is obvious from first sight"

    No it is not simple and no it is not obvious what it does at the first sight PERIOD

  • Custom User Avatar
  • Default User Avatar

    Wow, that's a really nice answer! Thanks bro!
    That really clears my mind!

  • Default User Avatar

    A good heuristic for deciding between the ternary operator and if/else is whether you are choosing between side effects or between calculations. For example:

    if (employee.isBoss()) {
        status = employee.notifyDirectReports();
    } else {
        status = employee.notifyBoss();
    }
    

    Here it should be clear that the choice is between two actions. Contrast with:

    pay = employee.bonusActive() ? BONUS_RATE * employee.hours() + employee.bonusAmount()
                                 : BASE_RATE * employee.hours();
    

    In this case we're choosing one of two calculations. You could rewrite the first example to use the ternary operator, but then it's easier to miss the side effects.

    There is a strong tendency for beginning programmers to want to use language constructs to make code terser and denser than it should be, without considering the factor of readability. It comes down to the question of whether you have the experience and/or discipline to use the ternary operator when it's appropriate and not just because you want to see if you can fit something on one line. If you're not disciplined enough to consider the pros and cons each time you use it, then it's best to err on the side of verbosity. The same argument holds for the ++ and -- operators and other shortcuts.

    That's not to say that you personally aren't good enough or experienced enough, but if you think about a policy that has to cover a whole company or a whole open source project, you need rules that will prevent the worst programmer in the group from writing bad code, and so you end up with more always / never prescriptions.

    One last thing: Different languages vary widely on how verbose coders tend to be. Things like the ternary operator and using short-circuiting boolean operators for side effects is much more tolerated in JavaScript, for example. The best advice is to try to match what most people are doing for your given language and platform, since it will make it easier to adapt to any given code base in that language, and easier for others to give feedback on or contribute to your code.

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution