115
Lightor
9y

Code: if(customer.primaryContract)

Boss: "just using a variable as a boolean isn't very readable"

Me:
if(!(!customer.primaryContract != !true).toString() == 'false')

Comments
  • 0
  • 4
    My tummy hurts
  • 10
    And you let this go to production and the next guy finds it and comes on devRant to rant about it. ;)
  • 3
    There's something to be said for readability by using well chosen names and proper indentation.

    But too often when people use the argument all I hear is "my brain can't handle complexity, please dumb it down" — leading to code which is less readable for those who are competent.
  • 2
    @nickpapoutsis
    Indeed, if only we could put a tracker on this...
  • 0
    You owe me money for therapy after seeing this :(
  • 2
    @apisarenco this is code to check if his primary contract is defined, yes. That is the convention. The you could throw a != undefined, but that's not even close to needed. As for asking questions, this is in a method called hasPrimaryContract? In the model which is giving you the ability to ask that question. Given the syntax and method name, functionality is very clear.

    Just like .contacts should be a collection of contacts and if you used it in an if statement it would tell you if there were any or if it was an empty/undefined collection. That's how values behave when treated like a Boolean. Having a Boolean stored for all your record values to see if they've been set isn't feasible, you have to be able to make these determinations.
  • 0
    😨
  • 1
    @apisarenco look in something like Angular for example, you check for null/undefined by calling the variable as a Boolean, it's very common convention.

    I get what you're saying but it's a well established convention and practice.
  • 0
    @Lightor you don't call it as a Boolean, it's evaluates to truthy or falsy by coercion and yes, it's very common code to use to check if it has a non-empty property.
Add Comment