Tuesday, June 18, 2013

A method (C#) or function (JavaScript) not idempotent should not follow the "Is" convention.

  • Good:
    • C#: public bool IsOnDice(int possibleValue)
    • JavaScript: isOnDice(possibleValue)
     
     
  • Not so good:
    • C#: public bool IsCrappedOut()
    • JavaScript: isCrappedOut()
     
     
In my imagination isOnDice just tells you if the number handed in is between 2 and 12, HOWEVER the not idempotent isCrappedOut I envision as bad:
  1. generates a sum of two separate random numbers between 1 and 6,
  2. sets state at a field for LastRoll with the value,
  3. returns false if the value is 2, 3, or 12,
  4. and true otherwise
If the logic in isCrappedOut is only called by one other method/function and it can just be tucked into that method/function, that might be better than having isCrappedOut standalone. Otherwise, it needs a better name, one that does not start with is.

No comments:

Post a Comment