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 8 years ago.
Improve this question
I can remember being told that having an if statement like if(10 == $myVariable) is a bad practice. However, why is this a bad practice and what is its name? (I can vaguely remember that it has a specific name).
its good practice because of accidental typos
if (10 = $myVariable)
will not compile where as
if ($myVariable = 10)
will and make the if statement true, and also set $myVariable to 10
is a fairly easy to make, and hard to spot error.
http://en.wikipedia.org/wiki/Yoda_conditions
It can also be good practice in languages where accessing null can lead to a failure, such as Java. For example, the following is safe even if myVariable is null:
if ("foo".equals(myVariable)) {
// Do something
}
Whereas the following will throw a NullPointerException:
if (myVariable.equals("foo")) {
// Do something
}
If your coding standards require that the variable comes first, your code's made more verbose by the requirement for a not null check:
if (myVariable != null && myVariable.equals("foo")) {
// Do something
}
According to a post on Jeff Atwood's blog Coding Horror, StackOverflow user zneak calls this a Yoda Condition. The description is as follows:
Using if(constant == variable) instead of if(variable == constant), like if(4 == foo). Because it's like saying "if blue is the sky" or "if tall is the man".
This refers to the Star Wars character Yoda, who always talks backwards.
As exussum mentiones in his answer, it's sometimes not regarded as a bad practice because it can avoid typo's.
Related
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 :)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm doing a C++ compiler project where I need to flag warnings at places where assignment operator can be used wrongly instead of the comparison operator. .e.g
while doing comparison in if statement , to check variable a as 10 sometimes we wrongly type if(a= 10), which will result in if statement always true whereas I wanted to be true only if a is 10. Some of the cases I can think of are :
if(var = a), logically it should be if( var==a )
while(var = a )
for(;var=a;)
do{}while(var=a)
var=a? "some XYZ": "some ABC"
Can you please help me with the more cases where this logical error can occur, where the user was supposed to use == and by mistake = was used?
A nasty one I ran across recently was assert (a=b). The reason that's so particularly nasty is because the assumption stated in the assert is that the two are already equal, so the statement is most likely harmless. But if they aren't, this sets you up for a nasty debugging session as the debug builds functionally differs from the release build.
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
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.
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
I've heard from all of my professors that this is how brackets should be used when coding in C++:
void hereIsAFunc(){
//Code
}
Yet, I don't understand why you would ever want to do this when you could do:
void hereIsAFunc()
{
//Code
}
It looks way cleaner to me, is there a specific advantage that the first has over the other? What is the reason that this is the "standard" in coding?
Your professor has a personal style for indenting and formatting code. There is no "standard", anywhere, that specifies that this is the "right" or the "wrong" way to format the code.
Unlike certain languages, that will remain nameless, that for some odd reason are sensitive to whitespace and indentation, whitespace indentation is mostly irrelevant in C++.
Your professor's preferred style is neither right, nor wrong, nor a standard of any kind. You should simply nod your head in class, submit your homework assignments using your professor's preferred formatting style, get a good grade, then simply forget you heard the whole thing after the class is over.
It's just a style preference. Except for figuring out >> vs. > > or << vs. < < , the compiler doesn't care about white space.