|
This might be even better. After all, it's the way I do it in my head.
bool IsNumberEven(int number) {
string sNumber = number.ToString();
string sLastDigit = sNumber[sNumber.Length - 1];
if(sLastDigit == '0' || sLastDigit == '2' || sLastDigit == '4' || sLastDigit == '6' || sLastDigit == '8' ) {
return true;
}
else {
return false;
}
}
|
|
|
|
|
It's ok e.g. in C#, but in C/C++ this habit can lead to nasty bugs.
If you work with multiple libraries, you probably have to deal with bool, BOOL and VARIANT_BOOL . While bool is fine (unless abused in the assignment), BOOL is usually used as "any nonnegative value is true", and the respective TRUE constant is defined sometimes as 1, sometimes a (!FALSE) which would expand to -1. VARIANT_BOOL explicitely defines -1 as VARIANT_TRUE.
I know that some people prefer
if (someridiculouslylongexpression==false)
over
if (!someridiculouslylongexpression)
since the not can get lost quickly for the quick reader. However, I discourage this habit, as it may fail with the "fak" boolean types.
|
|
|
|
|
dighn wrote: slightly more redundant
It is a way bit explicit right? Do you mean to say it affects any of the coding guidelines? C# actually advocates and prefers explicit typecasting too. And similarly an explicit mention should be a welcome though a bit strange. What do you way?
|
|
|
|
|
Actually I do not know why that was even posted, there was absolutely nothing wrong with that code.
Now if there was something in the “do something…” sections, that showed a problem – then it might have made since as a problem – otherwise it is great and legitimate code.
Tinko101 – can just ignore my comment, because I have only been writing code for about 19 years or so, but I agree with you.
INTP
"Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
|
|
|
|
|
The only thing I can see wrong is that the boolean value isn't initialized. Now that is ban practice as in C/C++ values only get a default value in Debug mode. In Release mode it could be anything
|
|
|
|
|
I assumed the posted code was just missing an elipsis. Presumably there was something to set the Boolean value.
|
|
|
|
|
Tell the compiler that
|
|
|
|
|
mmm...not sure !
I already see worse !
You write a first test
if (boolValue == true)
during debugging, it appaers that you have to change the test
if (boolValue != true)
after a second debug you wish to change it again
if (boolValue != false)
As summary, do you think that this code is really readable, even the performance is the same... :
<big>if (boolValue != !false)
|
|
|
|
|
It makes no difference, just like
if (((true == myFlagVariable) == true) != false)
is in fact equivalent to
if (myFlagVariable)
It's just that one version shows that the programmer understands that a boolean variable is in fact a boolean expression. So while it doesn't do much damage, it does show a lack of understanding -- excepting the occasional brainfart, which can surely happen to anyone.
I've also noted the habit of C programmers to write
if (null == var) ...
which to me just doesn't read naturally! I realise that they do this to avoid an unintensional assignment - a common and often subtle bug in C code:
if (var = null) ...
But in C# an assignment statement is not legal in an if condition, so it is a pointless practice. But I won't deny that habits are powerful and it may not be so easy for an old dog (C programmer) to change his ways. (I wish it was "her" ways more often, but for some reason girls are few and far between in our community.)
|
|
|
|
|
I'll rush to correct myself...
"an assignment statement is not legal in an if condition"
isn't strictly true. What is true is that an if condition in C# requires a boolean expression. For the present purpose it doesn't greatly matter, since the accidental assignment cannot happen in C#, but in general, an assignment can indeed be part of the boolean expression, even if there is little reason to make our code harder to read in this way.
if ((list.Capacity = count) > 1024) { ... }
This is perfectly legal, but not as readable as
list.Capacity = count;
if (count > 1024) { ... }
which is equivalent (i.e. both versions produce the same IL code).
|
|
|
|
|
that's the classic "the Product guys won't make up their minds, so i'll do it both ways" design pattern.
|
|
|
|
|
While I don't write boolValue == true, I do write boolValue == false since it stands out more then !boolValue.
I can imagine the sinking feeling one would have after ordering my book,
only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon
|
|
|
|
|
It stands out more?? How clear do you need it to be?
IMO, (b == true) "stands out" more than (b), so if this was a guiding principle we ought to write it all the time.
Perhaps we should also write
int i, count, n;
...
i = i.Add(count.Multiply(n));
instead of
i += count * n;
Admittedly the last example is a stretch. And the issue is surely quite academical - there are a few other bad coding habits I can think of, and methodology (or lack thereof), and much else, that matters rather a lot more in the end. But for the sake of academic nitpicking (which I think is rather fun) I'd still say "less is usually better". The only exception is when less code makes something obscure or unreadable. In my judgement (!b) clearly does NOT qualify; it is as clear as it can be.
|
|
|
|
|
I'm going to be pedantic here and state that the do something condition will never be executed in this example. BTW - to answer your question, the compiler recognises that if (a == true) is functionally equivalent to if (a) because it is clever enough to know that all parts of the if condition must be boolean conditions.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
In some languages TRUE is actually an int, mostly 1. But BASIC languages (VB for one), define TRUE as NOT FALSE (where not is the 2's complement), so TRUE is actually -1. If you are interacting with different languages, you have to take this into account -- for example, a Windows API return value in Visual Basic.
|
|
|
|
|
Not with C# compiler right? The compiler 'barks' and strongly recommends an explicit casting. Isn't it?
|
|
|
|
|
Yep, in C# you can't compare a bool to 1 or an int to true without explicit casting.
|
|
|
|
|
I dont think theres anything wrong with that, the compiler is going to optimize it anyways, the reason I like it is because is simpler to read than the alternative and simpler is always better in code. The only argument you could make that one shouldnt do that is if you mistype and put if(someBoolValue=true) and do an assignment instead of a comparision. The workaround to that is to either put constants on the left hand side if(true==someBoolValue), which you should already be doing so the compiler catches other similar mistakes (like null checks), or set the compiler warning level to 4, which I would also recommend you do to let the compiler detect other bugs.
|
|
|
|
|
Why, when you can write it like this:
column.DataType = System.Type.GetType("System.String");
(obviously, it's C#)
[ My Blog] "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|
|
Because it's faster... much faster.
I always hope the typeof() gets done at compile time, but I think that's not the case.
However, I just whipped up a little program to do one million of each, and the GetType took 00:00:17.1290068, whereas the typeof took a mere 00:00:00.0245439.
|
|
|
|
|
PIEBALDconsult wrote: ecause it's faster... much faster.
PIEBALDconsult wrote: GetType took 00:00:17.1290068
PIEBALDconsult wrote: typeof took a mere 00:00:00.0245439.
Either you missed my sarcasm, or I missed something else. So not only typeof shows intention much better (and is shorter), it's also faster. Excellent.
[ My Blog] "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|
|
|
PIEBALDconsult wrote: It's a win/win/win
And those are very nice situations to be in
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
|
|
|
|
|
dnh wrote: typeof shows intention much better (and is shorter), it's also faster. Excellent.
Hey, what if I want potential runtime errors? You just need to write a unit test to make sure column.DataType == typeof(string).
I can imagine the sinking feeling one would have after ordering my book,
only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon
|
|
|
|
|
I believe the typeof(T) operator is resolved at compile-time. Thus when you speak of how long "typeof took" you are really only measuring the time spent to loop, increment and check loop variable, and perform the assignment.
The MSDN documentation does make a distinction between types in general and the type objects that represent types (and which are, of course, themselves types). For example, the reference for the typeof() operator mentions:
Used to obtain the System.Type object for a type. A typeof expression takes the following form:
System.Type type = typeof(int);
It's clear that the System.Type is the "metatype", the type that is created for us but describes a type we created.
It also says to use GetType() to obtain the run-time type of an expression (although not the GetType overload discussed here, taking a string and finding a corresponding type in any loaded assembly in the appdomain!), and I take this as evidence in support of the "typeof() is resolved at compile-time" hypothesis.
|
|
|
|