Re:Which one will execute faster, if(flag==0) or if(0==flag)?
by MartinCron on Friday January 07, 2011 @11:06PM
(#2082149)
Code like Yoda, you must not.
[ Parent
| on HN ]
Re:Which one will execute faster, if(flag==0) or if(0==flag)?
by larrik on Friday January 07, 2011 @11:12PM
(#2082165)
The if(0==flag) is perfectly valid for avoiding if(flag=0) bugs.
Of course, I'd use if(!flag)
Re:Which one will execute faster, if(flag==0) or if(0==flag)?
by kenjackson on Friday January 07, 2011 @11:27PM
(#2082202)
Any modern should not allow 'if(flag=0)'.
Any non-modern language should flag a warning on that. And this warning should be set to be an error by any reasonable developer.
Re:Which one will execute faster, if(flag==0) or if(0==flag)?
by Jach on Friday January 07, 2011 @11:49PM
(#2082264)
Personally I'm more against "=" being the assignment operator, which should be ":=". But I have no problems with the actual syntax so long as it's clearly defined what (x = y) returns. (Does it return y? x? true if assignment was successful? Does it call a operator=() function that blows up the moon?) In languages where the line between statement and expression is nil it would be especially odd to flag this as illegal.
Flash Builder's Actionscript compiler has saved me from that a couple times in memory, though, so I agree it should be a warning if you don't wrap it in extra parens. gcc also will complain.
Re:Which one will execute faster, if(flag==0) or if(0==flag)?
by chromatic on Saturday January 08, 2011 @12:07AM
(#2082311)
Personally I'm more against "=" being the assignment operator, which should be ":=".
The problem with := is that you run the risk of the Pascal trap, in that no language which uses := for assignment has ever become more popular than Pascal.
Re:Which one will execute faster, if(flag==0) or if(0==flag)?
by redthrowaway on Saturday January 08, 2011 @12:29AM
(#2082356)
In general, I just don't like hitting shift, and so am opposed to :=.
Re:Which one will execute faster, if(flag==0) or if(0==flag)?
by Jach on Saturday January 08, 2011 @02:11AM
(#2082531)
Let's compromise and make it ;=.
Re:Which one will execute faster, if(flag==0) or if(0==flag)?
by redthrowaway on Saturday January 08, 2011 @06:31AM
(#2082871)
I'd rather if the compiler simply determined whether or not it was used as an assignment or comparison and just go with a single =.
Re:Which one will execute faster, if(flag==0) or if(0==flag)?
by rcfox on Friday January 07, 2011 @11:34PM
(#2082219)
What about while? while(flag = 0) looks like a similar error, but while(foo = read()) is a common idiom.
Re:Which one will execute faster, if(flag==0) or if(0==flag)?
by brownleej on Friday January 07, 2011 @11:40PM
(#2082236)
I think some compilers give a warning on while(foo = read()) but not on while((foo = read())).
Re:Which one will execute faster, if(flag==0) or if(0==flag)?
by CamperBob on Saturday January 08, 2011 @12:17AM
(#2082335)
That would be a broken compiler.
Re:Which one will execute faster, if(flag==0) or if(0==flag)?
by tbrownaw on Saturday January 08, 2011 @12:24AM
(#2082350)
No, I'm pretty sure that's intentional specifically to permit that idiom.
Re:Which one will execute faster, if(flag==0) or if(0==flag)?
by __david__ on Saturday January 08, 2011 @03:01AM
(#2082614)
It's usually a warning that you have to turn on explicitly, or one that you can at least turn off (thank heavens).
Re:Which one will execute faster, if(flag==0) or if(0==flag)?
by CamperBob on Saturday January 08, 2011 @12:17AM
(#2082334)
It's not the choice of keyword that (should) trigger the warning. It's the simple fact that an expression that evaluates to boolean contains an '='. This potential error is trivial for any compiler to flag.
Re:Which one will execute faster, if(flag==0) or if(0==flag)?
by bradly on Saturday January 08, 2011 @02:31AM
(#2082566)
> Any modern should not allow 'if(flag=0)'.
This is how you set default values for optional argument in Ruby and is very common.
Re:Which one will execute faster, if(flag==0) or if(0==flag)?
by chc on Saturday January 08, 2011 @04:39AM
(#2082738)
I have never seen "if foo = 0" used to set a value for an optional argument in Ruby. It would either be "def bar(foo=0)" or "foo ||= 0". Can you give any example of what you're talking about?
Re:Which one will execute faster, if(flag==0) or if(0==flag)?
by bradly on Saturday January 08, 2011 @04:16PM
(#2083980)
My mistake. I didn't read parent carefully enough. Although it is common to see something like:
if(@post = Post.find(params[:id]))
render @post
else
render 'Post not found'
end
Re:Which one will execute faster, if(flag==0) or if(0==flag)?
by xhuang on Saturday January 08, 2011 @05:53AM
(#2082826)
I think PHP allow assignment in 'if'
Re:Which one will execute faster, if(flag==0) or if(0==flag)?
by tjsnyder on Friday January 07, 2011 @11:21PM
(#2082183)
Honestly, this is like arguing tabs vs spaces.
Re:Which one will execute faster, if(flag==0) or if(0==flag)?
by lukeschlather on Saturday January 08, 2011 @02:11AM
(#2082529)
Tabs vs. spaces is basically an implementation detail of the editor. The point is that the code should always look the same.
This is a semantic issue that doesn't even go away in pseudocode.
Re:Which one will execute faster, if(flag==0) or if(0==flag)?
by zokier on Friday January 07, 2011 @11:44PM
(#2082248)
In C++ flag==0 and !flag are not always the same, but depending on the type of flag, and if that type(or any its parents) has either or both of == or ! operators overloaded.
Re:Which one will execute faster, if(flag==0) or if(0==flag)?
by InclinedPlane on Saturday January 08, 2011 @05:57AM
(#2082833)
Pray tell which compiler you are using that won't throw a warning on variable assignment in a condition?
Re:Which one will execute faster, if(flag==0) or if(0==flag)?
by andrewf on Saturday January 08, 2011 @05:41AM
(#2082811)
If one side of the expression is longer, putting the shorter one first can improve readability. (Some would argue that you should assign the longer expression to a variable first).
I think that
if(S_OK == QueryInterface(IID_IUnknown, &p)) {
is clearer than if(QueryInterface(IID_IUnknown, &p) == S_OK) {