• Gibbon1 (unregistered) in reply to savar
    savar:
    Clever is a strong word. This is still very stupid code. (Hint: each field is hard coded, as is the constant 0.09.) But I agree its not exactly a WTF either. It's bad code that kinda works for what it was supposed to do.

    I'm not sure an extra level of abstraction is going to improve things. At least here the wtf is contained

  • Some Damn Yank (unregistered) in reply to Anonymous
    Anonymous:
    gallier2:
    Transforming 100*ct/9.0 to ct/0.09 should be obvious to anyone
    Possibly obvious, but also wrong: 0.09 can't be exactly represented in IEEE floating-point, so you may get a fractionally different result. It happens to work for 9 items, but not in general e.g.
    $ python -c 'import math; print math.ceil(57/0.57)'
    101
    
    That's because you're using IEEE math instead of ASCII math.
  • Jimm (unregistered)

    If this is supposed to get percentage complete, shouldn't it be 100 * ct/9, or 11.11 * ct?

  • Senji (unregistered) in reply to Tod

    In JS you could probably do:

    elementslist = [ "nickname", "websiteUrl", "linkedin",  "locationString", "title", "bio", "imageUrl", "resume"]
    
    ..... if typeof( u[element] ) ....
    
  • anonymous (unregistered)

    I found the following WTFs:

    • "proFormComplete" means... "procent form complete"?
    • there's an anonymous function for no apparent reason*
    • "user" is a global variable instead of an argument
    • "user" has a getter method that returns... itself?
    • two variable definition lines in a row, both with "var"**
    • "0 +" does absolutely nothing that the next "+" wouldn't do
    • typeof is not a function; the parentheses do nothing
    • why the need to check the types... they might not be strings?
    • the only thing equal to "string" is "string"; exact comparison is unnecessary
    • string.length > 0 instead of string > "" (when we know it's a String)
    • "value && true" instead of "!!value" (or just "value", if it's Boolean)
    • variable that holds a stupid intermediate value**
    • no comments whatsoever

    *it's not an event; the only other reason it would need to be done this way is if it's nested inside another function - in which case, that's another WTF. **this should be done when it helps improve readability; it doesn't, so why bother?

    Dividing by 0.09 is not a WTF. Yes, you could multiply by 11.1 instead. No, it wouldn't make any substantial difference.

    Without making it much less "clever", this is how the code should probably look:

    function percentFormComplete(u) {
    // Sums the number of values that aren't blank
    // and returns an integer between 0 and 100
    return Math.round(
    (
    (typeof u.nickname == "string" && u.nickname > "") +
    (typeof u.websiteUrl == "string" && u.websiteUrl > "") +
    (typeof u.linkedin == "string" && u.linkedin > "") +
    (typeof u.locationString == "string" && u.locationString > "") +
    (typeof u.title == "string" && u.title > "") +
    (typeof u.bio == "string" && u.bio > "") +
    (typeof u.imageUrl == "string" && u.imageUrl > "") +
    (typeof u.resume == "string" && u.resume > "") +
    !!u.employerSharing
    ) / 0.09);
    }

Leave a comment on “Divide and Validate”

Log In or post as a guest

Replying to comment #:

« Return to Article