"Always compare equality with a constant on the left, for example if (5 == maxUsers) instead of if (maxUsers == 5). If neither of the expressions being compared are constants, rewrite the condition so it is, for example if (0 == userCount - userLimit) instead of if (userCount == userLimit)."
"For consistency with the previous rule, non-equality comparisons should also be formatted using the same style. For example, use if (0 < userCount) instead of if (userCount > 0), and if (0 < userCount - userLimit) instead of if (userCount > userLimit)."
Name:
Anonymous2012-06-22 1:28
What software are those coding standards used for so I can laugh at it?
Name:
Anonymous2012-06-22 1:30
Okay, now I'm curious. What's the source of this? I mean are there some arguments pro/con to this?
It's called yoda conditions. It's a defensive programming technique to protect from accidental assignment -- since = is close to ==.
I don't see any purpose for it. I've never had accidental assignment before.
Name:
Anonymous2012-06-22 2:17
I don't think the tradeoff is worth it at all. if (0 < userCount - userLimit) is much less readable than [code]if (userCount > userLimit)[code].
Name:
Anonymous2012-06-22 2:18
I don't think the tradeoff is worth it at all. if (0 < userCount - userLimit) is much less readable than if (userCount > userLimit).
Now could someone please design a defensive technique to protect from forgetting to close your BBCode tags?
Name:
Anonymous2012-06-22 2:22
So that's considered retarded? How retarded are you to find it retarded?
Name:
Anonymous2012-06-22 2:38
>>7
It is retarded. Reading code with yoda conditionals is like trying to read a novel in which the author consistently uses a verb-object-subject structure.
>>12 $ cat foo.c
int main()
{
int x = 0;
if (x = 1) {}
return 0;
}
$ gcc -Wall -Wextra foo.c
foo.c: In function ‘main’:
foo.c:4:2: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
Name:
Anonymous2012-06-22 6:07
>>13 >>14
Well then Yoda conditions proven for retarded bullshit.
Retarded.cs(15,8): error CS0029: Cannot implicitly convert type 'int' to 'bool'
Name:
Anonymous2012-06-22 7:58
That is a different type of error, try
bool x = true;
if(x = false)
and report back
Name:
Anonymous2012-06-22 8:28
>>19
That should still fail in C# because 'x = false' is not an expression.
And, lo and behold, with Mono (gmcs), this code: using System;
namespace Scalable.Enterprise {
class Module {
public static void Main(string[] args)
{
bool ScalableEnterpriseBoolean = true;
if (ScalableEnterpriseBoolean = false)
Console.WriteLine("``faggot''");
}
}
}
gives these warnings:
gmcs -out:ScalableEnterpriseModule.exe ScalableEnterpriseModule.cs ScalableEnterpriseModule.cs(8,55): warning CS0665: Assignment in conditional expression is always constant. Did you mean to use `==' instead ?
ScalableEnterpriseModule.cs(7,30): warning CS0219: The variable `ScalableEnterpriseBoolean' is assigned but its value is never used
Compilation succeeded - 2 warning(s)
Okay, well what if userCount or userLimit aren't integers, but Strings, or Arrays, or Structs? What if userCount - userLimit doesn't actually make sense because the method - isn't defined for userCount?
>>27
Best solution for everyone:
//;--# Disclaimer: the syntax here is completely arbitrary, I'll use all comment syntaxes I know to achieve full neutrality, except VB's because it's retarded above salvation.
--//#; `let', `var', whatever to declare variables, refs instead of everything-is-mutable (optional).
let x = getX(), y = make_ref(2);
//--#; (:=) assigns to refs/mutable variables. Its return type is void or unit.
y := 3;
#//;-- Then equality is one of:
;#//-- If the language doesn't permit definition of new operators. If it's in a let, it's a binding =, else it's just an operator.
if x = y ...
#;--// the usual, x = y is a syntax error: expected expression, got a binding form.
if x == y ...
If you can remember to use yoda conditions, you'll also remember to use '==' instead of '='. If you forget to use '==' instead of '=', you'll also forget to use yoda conditions.
Making the rule worthless. QED
Name:
Anonymous2012-06-22 19:47
>>32
stop having common sense, it's offensive to feminists.
Name:
Anonymous2012-06-22 21:57
>>14
That warning needs to be made narrower, since
while(c = ...) { /* do something with c */ }
is a common idiom for processing null-terminated strings (or other things that end when they're zero). I'd suggest making it warn only if you're assign a constant or single variable, like
while(c = 5)
because that's something almost never encountered in practice. while(c = <some complex expression that returns 0 when it's time to exit the loop>) is a lot more common.
>>34
The warning is fine. It's just a warning, and if you want to avoid it, you can write while ((c = ...)) instead to indicate you're absolutely sure you know what you're doing.
Name:
Anonymous2012-06-23 7:51
>and if (0 < userCount - userLimit) instead of if (userCount > userLimit)."
Retarded
Name:
Anonymous2012-06-23 8:43
Yoda Conditions is Hungarian Notation for conditional logic.