Skip to main content
added 33 characters in body
Source Link
  • Echoing others, your similarValues function should be returning a boolean. Calling it areSimilarValues reads better and is more descriptive.

  • The two arguments are called smallerValue and biggerValue, yet you don't actually know that one is bigger than the other. It's equally as plausible you could call the function like this: similarValues(55, 0.1) in which case the variable names are completely wrong from their actual values. I would recommend something more generic in this case: a and b or value1 and value2.

  • The similarValues function can be reduced to one line:

      #include <cmath>
    
      bool function areSimilarValues(double a, double b) {
          return abs(a - b) < 0.01;
      }
    

    Also note that no comments are required when you give things descriptive names, and use appropriate return types.

  • Like others have said, the valueLarger and valueSmaller functions could use better names, such as getLargerValue and getSmallerValue.

  • The valueLarger and valueSmaller functions return zero when the values are equal? The other return statements return one of the numbers. Returning zero feels like a logic error. If they are equal, just return one of them. It doesn't really matter which one you return since they are equal.

      double getLargerValue(double a, double b) {
          if (a > b) {
              return a;
          }
          else if (b > a) {
              return b;
          }
          else {
              // Values are equal
              return a;
          }
      }
    

    Even this could be reduced to:

      double getLargerValue(double a, double b) {
          return b > a ? b : a;
      }
    

    If b is greater than a, return b. Otherwise, a is either larger than b or a and b are equal, so return a. And just for good measure, the getSmallerValue function:

      double getSmallerValue(double a, double b) {
          return b < a ? b : a;
      }
    
  • Now that I think about it, Mathematics has a term for getting the larger or smaller value of two numbers: Maximum and Minimum. These would be good names for valueLarger and valueSmaller:

      double maximum(double a, double b) {
      }
    
      double minimum(double a, double b) {
      }
    
  • Echoing others, your similarValues function should be returning a boolean. Calling it areSimilarValues reads better and is more descriptive.

  • The two arguments are called smallerValue and biggerValue, yet you don't actually know that one is bigger than the other. It's equally as plausible you could call the function like this: similarValues(55, 0.1) in which case the variable names are completely wrong from their actual values. I would recommend something more generic in this case: a and b or value1 and value2.

  • The similarValues function can be reduced to one line:

      bool function areSimilarValues(double a, double b) {
          return a - b < 0.01;
      }
    

    Also note that no comments are required when you give things descriptive names, and use appropriate return types.

  • Like others have said, the valueLarger and valueSmaller functions could use better names, such as getLargerValue and getSmallerValue.

  • The valueLarger and valueSmaller functions return zero when the values are equal? The other return statements return one of the numbers. Returning zero feels like a logic error. If they are equal, just return one of them. It doesn't really matter which one you return since they are equal.

      double getLargerValue(double a, double b) {
          if (a > b) {
              return a;
          }
          else if (b > a) {
              return b;
          }
          else {
              // Values are equal
              return a;
          }
      }
    

    Even this could be reduced to:

      double getLargerValue(double a, double b) {
          return b > a ? b : a;
      }
    

    If b is greater than a, return b. Otherwise, a is either larger than b or a and b are equal, so return a. And just for good measure, the getSmallerValue function:

      double getSmallerValue(double a, double b) {
          return b < a ? b : a;
      }
    
  • Now that I think about it, Mathematics has a term for getting the larger or smaller value of two numbers: Maximum and Minimum. These would be good names for valueLarger and valueSmaller:

      double maximum(double a, double b) {
      }
    
      double minimum(double a, double b) {
      }
    
  • Echoing others, your similarValues function should be returning a boolean. Calling it areSimilarValues reads better and is more descriptive.

  • The two arguments are called smallerValue and biggerValue, yet you don't actually know that one is bigger than the other. It's equally as plausible you could call the function like this: similarValues(55, 0.1) in which case the variable names are completely wrong from their actual values. I would recommend something more generic in this case: a and b or value1 and value2.

  • The similarValues function can be reduced to one line:

      #include <cmath>
    
      bool function areSimilarValues(double a, double b) {
          return abs(a - b) < 0.01;
      }
    

    Also note that no comments are required when you give things descriptive names, and use appropriate return types.

  • Like others have said, the valueLarger and valueSmaller functions could use better names, such as getLargerValue and getSmallerValue.

  • The valueLarger and valueSmaller functions return zero when the values are equal? The other return statements return one of the numbers. Returning zero feels like a logic error. If they are equal, just return one of them. It doesn't really matter which one you return since they are equal.

      double getLargerValue(double a, double b) {
          if (a > b) {
              return a;
          }
          else if (b > a) {
              return b;
          }
          else {
              // Values are equal
              return a;
          }
      }
    

    Even this could be reduced to:

      double getLargerValue(double a, double b) {
          return b > a ? b : a;
      }
    

    If b is greater than a, return b. Otherwise, a is either larger than b or a and b are equal, so return a. And just for good measure, the getSmallerValue function:

      double getSmallerValue(double a, double b) {
          return b < a ? b : a;
      }
    
  • Now that I think about it, Mathematics has a term for getting the larger or smaller value of two numbers: Maximum and Minimum. These would be good names for valueLarger and valueSmaller:

      double maximum(double a, double b) {
      }
    
      double minimum(double a, double b) {
      }
    
Source Link

  • Echoing others, your similarValues function should be returning a boolean. Calling it areSimilarValues reads better and is more descriptive.

  • The two arguments are called smallerValue and biggerValue, yet you don't actually know that one is bigger than the other. It's equally as plausible you could call the function like this: similarValues(55, 0.1) in which case the variable names are completely wrong from their actual values. I would recommend something more generic in this case: a and b or value1 and value2.

  • The similarValues function can be reduced to one line:

      bool function areSimilarValues(double a, double b) {
          return a - b < 0.01;
      }
    

    Also note that no comments are required when you give things descriptive names, and use appropriate return types.

  • Like others have said, the valueLarger and valueSmaller functions could use better names, such as getLargerValue and getSmallerValue.

  • The valueLarger and valueSmaller functions return zero when the values are equal? The other return statements return one of the numbers. Returning zero feels like a logic error. If they are equal, just return one of them. It doesn't really matter which one you return since they are equal.

      double getLargerValue(double a, double b) {
          if (a > b) {
              return a;
          }
          else if (b > a) {
              return b;
          }
          else {
              // Values are equal
              return a;
          }
      }
    

    Even this could be reduced to:

      double getLargerValue(double a, double b) {
          return b > a ? b : a;
      }
    

    If b is greater than a, return b. Otherwise, a is either larger than b or a and b are equal, so return a. And just for good measure, the getSmallerValue function:

      double getSmallerValue(double a, double b) {
          return b < a ? b : a;
      }
    
  • Now that I think about it, Mathematics has a term for getting the larger or smaller value of two numbers: Maximum and Minimum. These would be good names for valueLarger and valueSmaller:

      double maximum(double a, double b) {
      }
    
      double minimum(double a, double b) {
      }