HiveBrain v1.2.0
Get Started
← Back to all entries
principlecppModerate

One program using three functions to compare two double-precision numbers

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
threenumbersprecisionprogramonetwodoubleusingfunctionscompare

Problem

I am currently learning C++ though Bjarne Stroustrup book Programming Principles and Practices using C++.

Going through the drills, I am trying to purposefully write efficient and readable code (good coding practices. However, I think I may be overdoing it with my use of functions for tasks within my program.

//Function to check if the entered numbers are similar in value
double similarValues(double valueBigger, double valueSmaller)
{
    if ((valueBigger - valueSmaller)  value02) {
        return value01;
    }
    else if (value01  value02) {
        return value02;
    }
    else if (value01 > value01 >> value02) {

        // Checks if the functions valueSmaller & valueLarger have not retuned 0
        if (valueSmaller(value01, value02) != 0 || valueLarger(value01, value02) != 0) {
            cout << "The smaller value is: " << valueSmaller(value01, value02) << " , the larger value is: " << valueLarger(value01, value02) << "\n";

            //Checks if the values are at least similar, returns 1 if true
            if (similarValues(valueLarger(value01, value02), valueSmaller(value01, value02)) == 1) {
                cout<<"The numbers are almost equal. \n";
            }
        }
        // If functions valueSmaller & valueLarger have returned 0 then do the below instuction
        else if (value01 == value02) {
            cout << "Both of these numbers are equal!" << "\n";
        }
        // If the values entered cannot be processed then output an error message
        else {
            cout << "Not a value I know! Incorrect input" << "\n";
        }

    }
}


Please critically (I do not mind if it is harsh) state what programming practices I am not following or following correctly. Also, whether my coding is on par with what you would deem as following great coding conventions.

  • Am I overdoing the functions?



  • Am I using return values differently then they should be used?



  • Is my code readable?



  • Are my comments accurate and to the point

Solution

Your functions and their use are very curious:

  • A function to determine if two values are similar should be symmetric in its arguments, and return a bool.



  • A function determining the truth of a property should be called is@. similarValue is a bit of a puzzler, which is actually good as the functions behavior is very surprising.



  • A conditional expression returns a bool, which can be implicitly converted to an integer of value 0 (false) or 1 (true).



  • I'm not sure what I should do with valueSmaller and valueLarger, but I am sure that I don't want to see anything like them ever again.



Other points:

  • Seems you also don't know the conditional-operator (expr ? value-if-true : value-if-false). It can be used to write things better, but beware of getting carried away with it.



  • You are using using namespace std;.



That's a bad idea, read "Why is “using namespace std;” considered bad practice?".

  • When you output string- and char-literals consecutively, just concatenate them together. Also, if you have a length-1-string-literal, a char-literal can serve the same purpose more efficiently.



  • Your comments are, I'm sorry to say, useless, as they just rephrase some of the more obvious code.



When you use descriptive names, most other comments for describing what code does also become superfluous (exception: doc-comments which will be extracted for bare-bones documentation).

Comments are for giving non-obvious reasons for doing things you do, reasons for doing them the way you do them, or describing how you do something.

See this good answer by rolfl about how to properly comment.

Context

StackExchange Code Review Q#113257, answer score: 15

Revisions (0)

No revisions yet.