Is a single return statement cleaner? [closed] - c++

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Today, me and my colleague have a disagreement on a subject :
Which of the following is "cleaner" and why ? :
bool check(){
if(!test to do){
return false;
}
if(!other test to do){
return false;
}
return true;
}
Or:
bool check(){
bool result = true;
if(!test to do){
result = false;
}
if(!other test to do){
result = false;
}
return result;
}
In my opinion, it doesn't matter because of compilation process.

It almost doesn't matter functionally, because of the compilation process.
Some older texts recommend a single return, i.e. your second example, as they say the logic of the function is easier to trace.
Personally I dislike clutter and find the first example easier to read. You still have to think about and reason about the function in either case, just the first doesn't involve an additional variable and potentially more if/else flow clutter.
The first one doesn't always evaluate the second condition, though, which may be of interest to you if it has performance penalties, or side effects.
Furthermore, individual return statements are easier for the compiler to elide (not that it matters here).
But otherwise it's completely subjective.

Related

std::string as out parameter or return value [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Which is the correct way to get an std::string value from a function. The assumption here is that I am writing the function and the caller.
std::string foo()
{
std::string str = "ABC";
return str;
}
OR
void foo(std::string &str)
{
str = "ABC";
}
I understand that in the first method compiler optimizations will come into the picture and return by value will not be a big overhead so this method should be just fine.
The second method guarantees that there is no copy involved so it's going to be always efficient.
Which method would be your choice?
As said in the comments, using a return by value seems to be preferred nowadays. I find my code to be more readable that way, in that you can see that there is actually a change.
With the out parameter foo(str) it's not obvious that str is being changed.
I believe all mainstream compilers are clever enough to optimize away any unnecessary copying.
So, in essence, it's more about readability and clarity than anything else.

Why else condition is needed in c++ for good coding practice even if we have nothing to write in there? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I have seen else condition even if there is nothing inside it in many standard code documents. Why is that? Why should we use else even there is nothing to write in there as a good practice?
int a=1,b=2;
if(a)
{
a=a+b;
}
else
{
// Why we need this even there is nothing to write
}
Code standards are not global, these vary with the group, organization or community. Some people think it is more readable and maintainable this way, some think otherwise.
In my opinion, it is one of those rules made up by people who have too much free time on their hands.
I mean just look at it
if(condition) { do something }
else { }
How is that even more readable and maintainable. It is just adding some extra trash code.
I think one should avoid such documents for the sake of code.
However, if you work with people who follow the same standards either convince them to drop this out of standards or just walk away slowly :)

alternative of if else-if else statement c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to expand my knowledge of conditional statements in C++
if (condition){
return 0;
}
if else (condition A && condition B) {
//
}
else {
//
}
in this multiple conditonal state, what would be a good alternative?
Obviously because of condition A && condition B on if else statement, I can't use switch statement?
what would be a good alternative?
Shouldn't matter if no good alternative is better than what you already have.
(Ignoring the apparent if else error) Your shown control flow appears to be quite minimal (and therefore good) representation. There is no code duplication, and no repetitive structure that could be further exploited.

if-else or early return [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Sometimes I like to use early return statements to prevent nesting if statement, which I find makes for less readable code.
I am wondering if there is any objective or overwhelming general consensus as two which of the following patterns is better practice? I don't think this is a subjective question, since what I am really asking is there a near objective preference.
void func() {
if (a) {
do b
}
else {
do c
}
}
or
void func() {
if (a) {
do b
return;
}
do c
}
Frankly, I recommend the second one.
The second is more clear to understand
When some else modify the code more easy to understand is the first place.
Maybe the first is more clear in math but not in human being.
I would opt for the first version. I was actually given a lengthy explanation several years ago regarding this.
The two examples, as your wrote them now, are functionally identical. If the a condition be true, then the logic in the first if condition will execute, and the function will return. However, have a closer look at the second scenario:
void func() {
if (a) {
do b
return;
}
do c
}
Right now, should the first if fire, the function would return, otherwise c would execute. However, consider that at some point down the line a programmer decides to refactor the method for some reason. If he were to take out the return statement, then the logic for c would also execute if a were true. This may seem far-fetched, but it could happen more easily than you might think. On the other hand, if you use a full if-else, then even a refactor of the if condition would never result in the c logic evaluating at the same time.
The first is better. Simply put,it helps another developer to understand that c compiles because the condition is false. It also prevents other people from making damaging changes to your code. That said,they are both correct and would both work just fine

Is it common practice to "abuse" loops as goto [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
If in code there are many if-clauses and execution of code is not sensible on a previous error, is it OK to use a one-cycles loop for this? Simply for being able to exit the block with a break?
Like this:
do {
//..code
if (error1) break;
//..code
if (errorN) break;
//do finally something when no errors before
} while (false);
Yes, this is idiomatic, even if, perhaps, it was not the intended use for a do while loop. The source code for the linux kernel exploits this.
There's nothing unclear about it: while(false) does exactly what is says on the tin.
Yes it's a common technique to avoid deep nesting, and actually preferable to goto;.
From point of readability its way better than goto statements. The scope and code flow of the loop is well defined, and you don't need to lookup the corresponding labels of the goto statements, which not necessarily appear below.