Convention for leaving body of an if/else blank [closed]Two methods for loading jobsWhich option is better...

Important Resources for Dark Age Civilizations?

US citizen flying to France today and my passport expires in less than 2 months

Theorems that impeded progress

What is the offset in a seaplane's hull?

How to format long polynomial?

Accidentally leaked the solution to an assignment, what to do now? (I'm the prof)

Which models of the Boeing 737 are still in production?

Today is the Center

How to add double frame in tcolorbox?

How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?

How old can references or sources in a thesis be?

How can I make a cone from a cube and view the cube with different angles?

Pattern match does not work in bash script

How to know the difference between two ciphertexts without key stream in stream ciphers

Finding the repeating unit of polymerisation given two constituent molecules

Why not use SQL instead of GraphQL?

What are these boxed doors outside store fronts in New York?

Test whether all array elements are factors of a number

How can I make my BBEG immortal short of making them a Lich or Vampire?

Did Shadowfax go to Valinor?

Is every diagonalizable matrix is an exponential

Why is consensus so controversial in Britain?

Fencing style for blades that can attack from a distance

Equivalence principle before Einstein



Convention for leaving body of an if/else blank [closed]


Two methods for loading jobsWhich option is better for readability when dealing with blank strings?Summation for πCreating default parameters for a TestResultDefinitionDispatching Brainfuck commands using if-else or dictionaryBenchmark switch, binary search and if-elseConcise code to perform action on every element, when need indexWriting if..else construct without long condition checks to increase maintainabilityAge converter for some planetsGetting products for category






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







-1












$begingroup$


Disclaimer



I originally posted this in Stackoverflow and was pointed in this direction as a more suitable place to ask.



Background



I was reading this question earlier and was looking at this code snippet in one of the answers



auto z = [&](){ static auto cache_x = x; 
static auto cache_y = y;
static auto cache_result = x + y;
if (x == cache_x && y == cache_y)
return cache_result;
else
{
cache_x = x;
cache_y = y;
cache_result = x + y;
return cache_result;
}
};


Personally, I would be inclined to rewrite it as follows, with a single return statement



auto z = [&](){ static auto cache_x = x; 
static auto cache_y = y;
static auto cache_result = x + y;
if (x == cache_x && y == cache_y)
{
}
else
{
cache_x = x;
cache_y = y;
cache_result = x + y;
}
return cache_result;
};


but this leaves a blank body for the if part.



We could rewrite the if/else to just be if(!(x == cache_x && y == cache_y)) but this runs the risk of being misunderstood (and can get messy).



My Question



What is the more accepted way of writing something like this, should we




  • have multiple return statements

  • leave the body of the if blank

  • rewrite the if condition to be the negated version

  • something else




Please note, I usually code in Java whereas the sample code is in C++. I am interested in the generally accepted way of doing things, as opposed to specific C++ constructs/methods.










share|improve this question











$endgroup$



closed as off-topic by VisualMelon, Zeta, user673679, Vogel612 Mar 29 at 11:22


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – VisualMelon, Vogel612

If this question can be reworded to fit the rules in the help center, please edit the question.
















  • $begingroup$
    Welcome to Code Review; however, we require that questions concern a piece of actual code in a meaningful context - we can't answer general questions concerning the use of language constructs - so I think your question is Off-Topic here. SoftwareEngineering.SE might be a better place for you question, but check their help-centre first (this question may be of interest)
    $endgroup$
    – VisualMelon
    Mar 29 at 9:39












  • $begingroup$
    I've given the context, it's in the linked answer...
    $endgroup$
    – lioness99a
    Mar 29 at 9:42












  • $begingroup$
    That context needs to appear in the question; we can't depend on 3rd party sites (SO counts as one). If your question is about one piece of code specifically, then that is fine if you yourself wrote the code; however, my reading of your question is that you are looking for general advice concerning if else constructs, which I believe is off-topic.
    $endgroup$
    – VisualMelon
    Mar 29 at 9:46












  • $begingroup$
    Welcome to Code Review. As you can see in our help center, best practice questions are disregarded. In its current state, your question asks for best practice in general concerning if/else and returns in lambdas or other functions, not for a specific review on this code. For a specific review on the other hand, the lambda does not provide enough context, which, as VisualMelon said, must be included in the question itself. Please either edit your question to include the context if you're interested on a specific review, or try Software Engineering for generic best practices.
    $endgroup$
    – Zeta
    Mar 29 at 10:09








  • 3




    $begingroup$
    Your inverted if-condition can be simplified using the de-morgan rules to if (x != x_cache || y != y_cache), which is much easier to reason about
    $endgroup$
    – Vogel612
    Mar 29 at 10:15


















-1












$begingroup$


Disclaimer



I originally posted this in Stackoverflow and was pointed in this direction as a more suitable place to ask.



Background



I was reading this question earlier and was looking at this code snippet in one of the answers



auto z = [&](){ static auto cache_x = x; 
static auto cache_y = y;
static auto cache_result = x + y;
if (x == cache_x && y == cache_y)
return cache_result;
else
{
cache_x = x;
cache_y = y;
cache_result = x + y;
return cache_result;
}
};


Personally, I would be inclined to rewrite it as follows, with a single return statement



auto z = [&](){ static auto cache_x = x; 
static auto cache_y = y;
static auto cache_result = x + y;
if (x == cache_x && y == cache_y)
{
}
else
{
cache_x = x;
cache_y = y;
cache_result = x + y;
}
return cache_result;
};


but this leaves a blank body for the if part.



We could rewrite the if/else to just be if(!(x == cache_x && y == cache_y)) but this runs the risk of being misunderstood (and can get messy).



My Question



What is the more accepted way of writing something like this, should we




  • have multiple return statements

  • leave the body of the if blank

  • rewrite the if condition to be the negated version

  • something else




Please note, I usually code in Java whereas the sample code is in C++. I am interested in the generally accepted way of doing things, as opposed to specific C++ constructs/methods.










share|improve this question











$endgroup$



closed as off-topic by VisualMelon, Zeta, user673679, Vogel612 Mar 29 at 11:22


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – VisualMelon, Vogel612

If this question can be reworded to fit the rules in the help center, please edit the question.
















  • $begingroup$
    Welcome to Code Review; however, we require that questions concern a piece of actual code in a meaningful context - we can't answer general questions concerning the use of language constructs - so I think your question is Off-Topic here. SoftwareEngineering.SE might be a better place for you question, but check their help-centre first (this question may be of interest)
    $endgroup$
    – VisualMelon
    Mar 29 at 9:39












  • $begingroup$
    I've given the context, it's in the linked answer...
    $endgroup$
    – lioness99a
    Mar 29 at 9:42












  • $begingroup$
    That context needs to appear in the question; we can't depend on 3rd party sites (SO counts as one). If your question is about one piece of code specifically, then that is fine if you yourself wrote the code; however, my reading of your question is that you are looking for general advice concerning if else constructs, which I believe is off-topic.
    $endgroup$
    – VisualMelon
    Mar 29 at 9:46












  • $begingroup$
    Welcome to Code Review. As you can see in our help center, best practice questions are disregarded. In its current state, your question asks for best practice in general concerning if/else and returns in lambdas or other functions, not for a specific review on this code. For a specific review on the other hand, the lambda does not provide enough context, which, as VisualMelon said, must be included in the question itself. Please either edit your question to include the context if you're interested on a specific review, or try Software Engineering for generic best practices.
    $endgroup$
    – Zeta
    Mar 29 at 10:09








  • 3




    $begingroup$
    Your inverted if-condition can be simplified using the de-morgan rules to if (x != x_cache || y != y_cache), which is much easier to reason about
    $endgroup$
    – Vogel612
    Mar 29 at 10:15














-1












-1








-1





$begingroup$


Disclaimer



I originally posted this in Stackoverflow and was pointed in this direction as a more suitable place to ask.



Background



I was reading this question earlier and was looking at this code snippet in one of the answers



auto z = [&](){ static auto cache_x = x; 
static auto cache_y = y;
static auto cache_result = x + y;
if (x == cache_x && y == cache_y)
return cache_result;
else
{
cache_x = x;
cache_y = y;
cache_result = x + y;
return cache_result;
}
};


Personally, I would be inclined to rewrite it as follows, with a single return statement



auto z = [&](){ static auto cache_x = x; 
static auto cache_y = y;
static auto cache_result = x + y;
if (x == cache_x && y == cache_y)
{
}
else
{
cache_x = x;
cache_y = y;
cache_result = x + y;
}
return cache_result;
};


but this leaves a blank body for the if part.



We could rewrite the if/else to just be if(!(x == cache_x && y == cache_y)) but this runs the risk of being misunderstood (and can get messy).



My Question



What is the more accepted way of writing something like this, should we




  • have multiple return statements

  • leave the body of the if blank

  • rewrite the if condition to be the negated version

  • something else




Please note, I usually code in Java whereas the sample code is in C++. I am interested in the generally accepted way of doing things, as opposed to specific C++ constructs/methods.










share|improve this question











$endgroup$




Disclaimer



I originally posted this in Stackoverflow and was pointed in this direction as a more suitable place to ask.



Background



I was reading this question earlier and was looking at this code snippet in one of the answers



auto z = [&](){ static auto cache_x = x; 
static auto cache_y = y;
static auto cache_result = x + y;
if (x == cache_x && y == cache_y)
return cache_result;
else
{
cache_x = x;
cache_y = y;
cache_result = x + y;
return cache_result;
}
};


Personally, I would be inclined to rewrite it as follows, with a single return statement



auto z = [&](){ static auto cache_x = x; 
static auto cache_y = y;
static auto cache_result = x + y;
if (x == cache_x && y == cache_y)
{
}
else
{
cache_x = x;
cache_y = y;
cache_result = x + y;
}
return cache_result;
};


but this leaves a blank body for the if part.



We could rewrite the if/else to just be if(!(x == cache_x && y == cache_y)) but this runs the risk of being misunderstood (and can get messy).



My Question



What is the more accepted way of writing something like this, should we




  • have multiple return statements

  • leave the body of the if blank

  • rewrite the if condition to be the negated version

  • something else




Please note, I usually code in Java whereas the sample code is in C++. I am interested in the generally accepted way of doing things, as opposed to specific C++ constructs/methods.







comparative-review






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 29 at 10:45







lioness99a

















asked Mar 29 at 9:20









lioness99alioness99a

992




992




closed as off-topic by VisualMelon, Zeta, user673679, Vogel612 Mar 29 at 11:22


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – VisualMelon, Vogel612

If this question can be reworded to fit the rules in the help center, please edit the question.







closed as off-topic by VisualMelon, Zeta, user673679, Vogel612 Mar 29 at 11:22


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – VisualMelon, Vogel612

If this question can be reworded to fit the rules in the help center, please edit the question.












  • $begingroup$
    Welcome to Code Review; however, we require that questions concern a piece of actual code in a meaningful context - we can't answer general questions concerning the use of language constructs - so I think your question is Off-Topic here. SoftwareEngineering.SE might be a better place for you question, but check their help-centre first (this question may be of interest)
    $endgroup$
    – VisualMelon
    Mar 29 at 9:39












  • $begingroup$
    I've given the context, it's in the linked answer...
    $endgroup$
    – lioness99a
    Mar 29 at 9:42












  • $begingroup$
    That context needs to appear in the question; we can't depend on 3rd party sites (SO counts as one). If your question is about one piece of code specifically, then that is fine if you yourself wrote the code; however, my reading of your question is that you are looking for general advice concerning if else constructs, which I believe is off-topic.
    $endgroup$
    – VisualMelon
    Mar 29 at 9:46












  • $begingroup$
    Welcome to Code Review. As you can see in our help center, best practice questions are disregarded. In its current state, your question asks for best practice in general concerning if/else and returns in lambdas or other functions, not for a specific review on this code. For a specific review on the other hand, the lambda does not provide enough context, which, as VisualMelon said, must be included in the question itself. Please either edit your question to include the context if you're interested on a specific review, or try Software Engineering for generic best practices.
    $endgroup$
    – Zeta
    Mar 29 at 10:09








  • 3




    $begingroup$
    Your inverted if-condition can be simplified using the de-morgan rules to if (x != x_cache || y != y_cache), which is much easier to reason about
    $endgroup$
    – Vogel612
    Mar 29 at 10:15


















  • $begingroup$
    Welcome to Code Review; however, we require that questions concern a piece of actual code in a meaningful context - we can't answer general questions concerning the use of language constructs - so I think your question is Off-Topic here. SoftwareEngineering.SE might be a better place for you question, but check their help-centre first (this question may be of interest)
    $endgroup$
    – VisualMelon
    Mar 29 at 9:39












  • $begingroup$
    I've given the context, it's in the linked answer...
    $endgroup$
    – lioness99a
    Mar 29 at 9:42












  • $begingroup$
    That context needs to appear in the question; we can't depend on 3rd party sites (SO counts as one). If your question is about one piece of code specifically, then that is fine if you yourself wrote the code; however, my reading of your question is that you are looking for general advice concerning if else constructs, which I believe is off-topic.
    $endgroup$
    – VisualMelon
    Mar 29 at 9:46












  • $begingroup$
    Welcome to Code Review. As you can see in our help center, best practice questions are disregarded. In its current state, your question asks for best practice in general concerning if/else and returns in lambdas or other functions, not for a specific review on this code. For a specific review on the other hand, the lambda does not provide enough context, which, as VisualMelon said, must be included in the question itself. Please either edit your question to include the context if you're interested on a specific review, or try Software Engineering for generic best practices.
    $endgroup$
    – Zeta
    Mar 29 at 10:09








  • 3




    $begingroup$
    Your inverted if-condition can be simplified using the de-morgan rules to if (x != x_cache || y != y_cache), which is much easier to reason about
    $endgroup$
    – Vogel612
    Mar 29 at 10:15
















$begingroup$
Welcome to Code Review; however, we require that questions concern a piece of actual code in a meaningful context - we can't answer general questions concerning the use of language constructs - so I think your question is Off-Topic here. SoftwareEngineering.SE might be a better place for you question, but check their help-centre first (this question may be of interest)
$endgroup$
– VisualMelon
Mar 29 at 9:39






$begingroup$
Welcome to Code Review; however, we require that questions concern a piece of actual code in a meaningful context - we can't answer general questions concerning the use of language constructs - so I think your question is Off-Topic here. SoftwareEngineering.SE might be a better place for you question, but check their help-centre first (this question may be of interest)
$endgroup$
– VisualMelon
Mar 29 at 9:39














$begingroup$
I've given the context, it's in the linked answer...
$endgroup$
– lioness99a
Mar 29 at 9:42






$begingroup$
I've given the context, it's in the linked answer...
$endgroup$
– lioness99a
Mar 29 at 9:42














$begingroup$
That context needs to appear in the question; we can't depend on 3rd party sites (SO counts as one). If your question is about one piece of code specifically, then that is fine if you yourself wrote the code; however, my reading of your question is that you are looking for general advice concerning if else constructs, which I believe is off-topic.
$endgroup$
– VisualMelon
Mar 29 at 9:46






$begingroup$
That context needs to appear in the question; we can't depend on 3rd party sites (SO counts as one). If your question is about one piece of code specifically, then that is fine if you yourself wrote the code; however, my reading of your question is that you are looking for general advice concerning if else constructs, which I believe is off-topic.
$endgroup$
– VisualMelon
Mar 29 at 9:46














$begingroup$
Welcome to Code Review. As you can see in our help center, best practice questions are disregarded. In its current state, your question asks for best practice in general concerning if/else and returns in lambdas or other functions, not for a specific review on this code. For a specific review on the other hand, the lambda does not provide enough context, which, as VisualMelon said, must be included in the question itself. Please either edit your question to include the context if you're interested on a specific review, or try Software Engineering for generic best practices.
$endgroup$
– Zeta
Mar 29 at 10:09






$begingroup$
Welcome to Code Review. As you can see in our help center, best practice questions are disregarded. In its current state, your question asks for best practice in general concerning if/else and returns in lambdas or other functions, not for a specific review on this code. For a specific review on the other hand, the lambda does not provide enough context, which, as VisualMelon said, must be included in the question itself. Please either edit your question to include the context if you're interested on a specific review, or try Software Engineering for generic best practices.
$endgroup$
– Zeta
Mar 29 at 10:09






3




3




$begingroup$
Your inverted if-condition can be simplified using the de-morgan rules to if (x != x_cache || y != y_cache), which is much easier to reason about
$endgroup$
– Vogel612
Mar 29 at 10:15




$begingroup$
Your inverted if-condition can be simplified using the de-morgan rules to if (x != x_cache || y != y_cache), which is much easier to reason about
$endgroup$
– Vogel612
Mar 29 at 10:15










0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Fairchild Swearingen Metro Inhaltsverzeichnis Geschichte | Innenausstattung | Nutzung | Zwischenfälle...

Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...

Marineschifffahrtleitung Inhaltsverzeichnis Geschichte | Heutige Organisation der NATO | Nationale und...