Simple logical code vs logical simplicity

This is a simple code

1
2
3
4
if(a==b)
	return true;
else
	return false;


I found out that this can also be written as

return a==b;

Although the second one is one-liner and more simpler but not many people understand it in a go....

The first one is pretty simple and can be understood at first attempt

So the question arises that which should be used?

Should we prefer code readability or smart codes?

PS: I could not guess in which forum should I put this so I selected general, If you want I'll move it to lounge :)
Last edited on
I reckon it is better for one to learn the idioms of the language, rather than have extra verbosity.
I'd say use the one you like better. This seems more of a stylistic thing, and it doesn't really matter unless you're sharing your code with other people. In any case I think the "smart code" is just as readable as the first snippet you've provided.
Last edited on
TheIdeasMan wrote:
I reckon it is better for one to learn the idioms of the language, rather than have extra verbosity.


My thoughts exactly

xismn wrote:
I think the "smart code" is just as readable as the first snippet you've provided.


I also thought the same but when my friend saw the code he was stumped and asked me to add a comment or at least simplify it for future use

Last edited on
can you please explain it for me? I also could not understand this code
can you please explain it for me?


The expression a==b is evaluated. Then that value is returned.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

bool truth1(int a, int b)
{
    if(a == b)
       return true;
    else
       return false;
}

bool truth2(int a, int b)
{
    return a == b;
}

int main()
{
  std::cout << truth1(1,2) << ' ' << truth2(1,2) << '\n';
  std::cout << truth1(2,2) << ' ' << truth2(2,2) << '\n';
  
  return 0;
}


0 0
1 1


It's crunch time ... take your pick ... the truth will always out.

@vabi

if a and b are equal then the function will return true (as they are equal)

otherwise it will return false
there is a third way:
1
2
3
4
5
6
7
8
9
bool truth3(int a, int b)
{
    bool is_ok = false;

    if(a == b)
       is_ok = true;

    return is_ok;
}
It makes sense when the calculation is more complex.
Last edited on
Consider the first example in the tutorial on functions:
1
2
3
4
5
6
int addition (int a, int b)
{
  int r;
  r=a+b;
  return r;
}

copied from this page: http://www.cplusplus.com/doc/tutorial/functions/

Now that's ok for an introduction to C++, but the same function could be written more concisely as:
1
2
3
4
int addition (int a, int b)
{
  return a+b;
}

The reduced verbosity makes the code easier to read and understand, I'd say.

The code in the OP uses a similar style. Instead of return a+b; it has return a==b;
If there's any difficulty in understanding the latter form, it is possibly because of unfamiliarity with the == operator, and boolean expressions in general. Though these things turn up all the time in programs, perhaps they are sometimes seen more as a magical formula, rather than an expression with two values and an operator, which gives a result of type bool.
Topic archived. No new replies allowed.