Ad
  • Custom User Avatar

    Can't help, but hear the money counting machine sound when looking at these.. xD

  • Default User Avatar

    Hmmm...interesting.

  • Default User Avatar

    it makes me sad to see how easy you guys solve these katas

  • Default User Avatar

    simple, but i like it

  • Default User Avatar

    Thank you for the explanation Sir. +1

  • Custom User Avatar

    tl;dr Yes, this is shorthand for declaring two variables with the same value. But this answer is poorly written, as explained below.

    = is the assignment operator, which evaluates from right to left. You can imagine that line as

    var costA = (n = 0)

    the assignment operator also evaluates to the value of whatever was assigned, so n = 0 evaluates to 0. So after assigning to the n variable, the line then becomes

    var costA = 0

    which of course assigns the value 0 to costA. Note that if later the value of n changes, costA will not match the new value for n. costA will have its own value.

    Note that this answer would be considered BAD code in 2022. By both declaring and instantiating the variable n without the var keyword, n defaults to global scope. This means that as soon as the movie() function is run once, the value of n will be visible outside of the function scope, which can lead to silent bugs. Thus, multiple assignment (=) in one line should be done ONLY after the variables being assigned to the right of the first assignment have already been declared. For example:

    (within the function)
    var n;
    var costA = n = 0,
    costB = card;

    It's still considered poor form to mix and match some declaration and some assignment, as it makes the code harder to glean for bugs. I would instead prefer to declare local variables at the top of the function, because technically that's what the JavaScript interpreter is doing anyways for var declarations (all locally-scoped variables are "hoisted" to the beginnig of the enclosing scope).

    (within the function)
    var costA, costB, n;
    costA = n = 0;
    costB = card;

    To avoid some of these oddities with var and accidental globals, I instead declare variables with let or const exclusively.

  • Default User Avatar

    I'm a little confused with what's happening with the var costA line. Are you declaring two variables as 0 at once?

    I tried googling it a bit but I wasn't sure what to Google and couldn't find an answer.

    Any nudges in the correct direction would be appreciated. =D

  • Custom User Avatar

    First define Clean code in a small context.

    The context is all.

    Is a test? A MVP? An architecture? or merelly a beginner kata?

    A pragmatic programmer who applies Clean code also balances:

    • designing/coding spent time
    • readability
    • available time and money cost
  • Custom User Avatar
  • Custom User Avatar

    It is just "Distributive property". So p0 * (1 + percent / 100) is mathematically equivalent to p0 + p0*(percent / 100).

  • Custom User Avatar

    In what language would assignment to p0 mutate anything in a way noticeable for a caller? No matter if p0 holds a reference to an object, or a value, reassignment of p0 parameter won't mutate anything on the outside. Or will it?

    I can point one language where reassignment of an argument can mutate values seen to the outside (without changing the syntax of a call), I wonder if there's more and what they are.

  • Custom User Avatar

    Whilst in JS, assuming p0 is a number, then mutation isn't going to happen, for people coming in from other languages it's uncomfortable to see. Generally it's a bad habit to 'mutate' arguments as you're increasing the cognitive load on future coders to know whether the argument is a reference or not. For example, if the code is refactored and now p0 is given as an object or an array - if the coder niavely updates the code, they could easily start actually mutating the argument. Of course, good testing will guard against this, but it is a bad habit unless you want mutation outside of the function.

  • Custom User Avatar

    I agree that this isn't best practice, though it is a decent solution. For instance, I wouldn't want the arguments to be changed in the function body as it leads questions about accidental mutation (particularly if you come from another language). Of course, the argument names are bad, but as they are given by the Kata I can understand keeping them the same. However y to stand as number of years is a poor choice. With modern IDEs, there's very little reason to not use descriptive variable names. So if this was a professional codebase, I would want some changes before I would accept this.

    But definitely a good solution in terms of solving the problem

  • Default User Avatar

    I am also confused on how p0 = p0 * (1 + pecent/100) + aug works.
    does the 1+ replace the need for p0 += p0 * percent/100 + aug?

  • Custom User Avatar

    10 + (10 * 0.05) === 10 * 1.05

  • Loading more items...