Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

21
  • 153
    When brevity trumps readability. Commented Jan 5, 2017 at 16:23
  • 28
    Looking at your specific example: a cast is either (1) a place where the developer knows more than the compiler and needs to tell the compiler a fact that cannot be deduced, or (2) where some data is being stored in the "wrong" type for the kinds of operations we need to perform on it. Both are strong indicators that something could be refactored. The best solution here is to find a way to write the code with no casts. Commented Jan 5, 2017 at 18:24
  • 29
    In particular, it seems bizarre that it is necessary to cast CostIn to decimal to compare it to zero, but not CostOut; why is that? What is on earth is the type of CostIn that it can only be compared to zero by casting it to decimal? And why is CostOut not of the same type as CostIn? Commented Jan 5, 2017 at 18:26
  • 12
    Moreover, the logic here may actually be wrong. Suppose CostOut is equal to Double.Epsilon, and therefore is greater than zero. But (decimal)CostOut is in that case zero, and we have a division by zero error. The first step should be to get the code correct, which I think it is not. Get it correct, make test cases, and then make it elegant. Elegant code and brief code have a lot in common, but sometimes brevity is not the soul of elegance. Commented Jan 5, 2017 at 18:29
  • 6
    Brevity is always a virtue. But our objective function combines brevity with other virtues. If one can be briefer without harming other virtues, one always should. Commented Jan 5, 2017 at 18:52