Open In App

Clarity and Simplicity of Expressions

Last Updated : 23 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The concept of programming lies between solving complex expressions, to ease human efforts. The computer is super fast and can do a lot of calculations, but if the expression you provided is not clear enough, then it is going to be confusing, or the human who will later be willing to understand or improve the code won’t understand the clarity of the expression. Any piece of code which solves any problem must be clear and simple to the user. In this article, we are going to understand how to adopt Simplicity and Clarity of Expression. Developing good programs is a skill and good programs have a major feature i.e., humans can understand it easily. Writing good and understandable codes is a good habit, which could be eventually developed by following the principle of Clarity and Simplicity of Expressions. Let’s understand it in a detail.

Expressions

An expression is a combination of operators, variables, and names. These expressions are used in the computation of any value. An expression contains, not necessarily but two or more operands, and one or more variables to produce some output. Examples of Expression are a+b, a, 5-1, 5-a, etc. Here, a, b are variables, 5, 1 are constants, a+b sum of two variables, and 5-a difference of a constant and a variable

Operators

These are the building blocks of expressions, which are combined using operators. Operators are used for combining any two variables, or constants to perform some action on them. More than one operator can combine to create a complex expression. Some of the basic operators include:-

Operator

Function

+

Used for Addition 

Used for Subtraction

*

Used for Multiplication

/

Used for Division 

%

Used for finding Mod

Clarity of Expression

In programming languages, if we are not using a correct variable, then it becomes very tough for anyone to understand the code. Thus, any modification if required, will take a lot of effort, or sometimes rewriting the complete code again, which is a painful and time-taking experience. The use of long statements, or improper indentation makes it hard to understand the code. Indentation is very important for loops, and conditional expressions. In some programming languages like Python, Matlab, etc., improper indentation would cause an error. There are some general points which we can keep in mind for making our expressions more clear and simple. These steps are as mentioned below.

  1. Don’t write too long statements, try to complete them in a single line.
  2. Use proper variables, which specify the context and the meaning of the variables.
  3. Use proper indentation when using selection or loops.
  4. Use simple conditional tests, use break conditions and avoid unnecessary else conditions.
  5. Use parenthesis for clarifying the expressions.
  6. Use proper spacing, to improve readability.
  7. Avoid nesting in loops and conditional operators, not more than three layers.
  8. To make programs powerful, use the library function. It will also improve readability.

Let’s understand it more by some examples:-

C++




// C++ program to find the maximum of two numbers
#include <iostream>
using namespace std;
  
int main()
{
    int a = 13, b = 18;
    int minimum = max(a, b);
    cout << minimum << endl;
    return 0;
}


Output:

18

In this example, we are finding the maximum of two numbers, but the variable which we have used is minimum, if this is used in a long program, then it will surely confuse the user. A clear expression would be:

C++




// C++ program to find the maximum of two numbers
#include <iostream>
using namespace std;
  
int main()
{
    int a = 13, b = 18;
    int maximum = max(a, b);
    cout << maximum << endl;
    return 0;
}


Output:

18

Let’s make it more clear with the next example.

C++




// C++ program to find the difference between 
// the maximum and minimum of three number
#include <iostream>
using namespace std;
  
int main()
{
    int a = 12, b = 23, c = 4;
    int minimum = max(a, max(b, c));
    int maximum = min(a, min(b, c));
    int submission = minimum - maximum;
    cout << "The difference is " << submission << endl;
    return 0;
}


Output:

The difference is 19

Here, the user will surely get confused as the variable minimum is storing the maximum of the numbers, maximum is storing the maximum of the numbers and submission is storing the difference. The clear program would look like this:

C++




// C++ program to find the difference between 
// the maximum and minimum of three number
#include <iostream>
using namespace std;
  
int main()
{
    int a = 12, b = 23, c = 4;
    int maximum = max(a, max(b, c));
    int minimum = min(a, min(b, c));
    int difference = maximum - minimum;
    cout << "The difference is " << difference << endl;
    return 0;
}


Output:

The difference is 19

Simplicity of Expression

The expression is required to be simple and easily understandable not only to the computer but also to the users. For simplicity of the expressions, statements must be short, must be under proper indentation and proper use of parentheses is required. Let’s understand it by looking into some examples.

C++




// C++ program to perform division, multiplication,
// and addition respectively.
#include <iostream>
using namespace std;
  
int main()
{
    int a = 24, b = 6, c = 12, d = 11;
    cout << a / b * c + d << endl;
    return 0;
}


Output:

59

The expression used here could be quite confusing in this context. Alternatively, we could have used this expression for more clarity.

((a / b) * c) + d

Such confusing expressions may lead to wrong answers too, in some contexts. For example, take a case where you have to perform the previous task but in the opposite order. Here, if we would use the expression like c + d * b /a, then our answer would be incorrect. Let’s code it for better findings. 

C++




// C++ program to perform division, multiplication,
// and addition respectively.
#include <iostream>
using namespace std;
  
int main()
{
    int a = 24, b = 6, c = 12, d = 11;
    cout << c + d * b / a << endl;
    return 0;
}


Output:

14

But, if we would use proper parentheses here, then correct output and the desired answer could be obtained.

C++




// C++ program to perform division, multiplication,
// and addition respectively.
#include <iostream>
using namespace std;
  
int main()
{
    int a = 24, b = 6, c = 12, d = 11;
    cout << (((c + d) * b) / a) << endl;
    return 0;
}


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads