Rajan_sust's blog

By Rajan_sust, history, 7 years ago, In English
  • Vote: I like it
  • +12
  • Vote: I do not like it

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it
  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Why wouldn't lessThan be a < b + eps and lessThanEqual be a <= b + eps?

  • »
    »
    7 years ago, # ^ |
      Vote: I like it +13 Vote: I do not like it

    In your code (and in code in the post) there are a lot of constructions like this:

    if (A == B)
        return true;
    return false;
    

    Do you know that they should be written like this:?

    bool c = (A == B);
    if ((c == true) == true) 
        return true;
    else if ((c == true) == false)
        return false;
    else assert(c == true || c == false);
    
    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it
      if (A == B)
          return true;
      return false;
      

      And what's wrong? It's more readable than return A == B

      • »
        »
        »
        »
        7 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        It's code with excess branching, it can't be more readable. When you use if-else, it means that you have some different behaviour based on the condition. But when you need just return result of condition, like in these cases with a simple comparison, you should write as it is: return condition;.

        It's really a basic and famous example of how you can make your code dirtier with such return true/false and I don`t understand how you can say that it is more readable.

        • »
          »
          »
          »
          »
          7 years ago, # ^ |
            Vote: I like it +3 Vote: I do not like it

          It's really a basic and famous example how to write beautiful and readable code:

          if (very_long_condition_1)
            return true;
          if (very_long_condition_2)
            return true;
          return false;
          

          I bet you will write return very_long_condition_1 || very_long_condition_2 and someone will refactor it eventually.

          • »
            »
            »
            »
            »
            »
            7 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            At first, A == B is not a very_long_condition.

            At second, I will not.

            The post and code from the comment have only single-comparison conditions. So it's a very bad attempt from you to try say that I can't balance between shortness and readability in that way =)

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it +3 Vote: I do not like it

      I always like Sarcasm public.