|
As I stated in my original post "OK, its harmless enough", maybe you didn't read that far.
I'd hope the compiler would eliminate this, I'd be surprised if it didn't. However, you can say the same for a myriad of things, such as unused local variables. Should I stop removing such things from my code - after all the compiler will sort out the mess for me, never mind the poor developer's who have to try and understand my code.
I'm sure the original writer is doing OK - he's writing code for Microsoft, so presumably is up to the task, but my point was that you'd hope tutorial code would illustrate good coding practices.
|
|
|
|
|
You should get resharper to find and remove them from your code for you.
Did you ever see history portrayed as an old man with a wise brow and pulseless heart, waging all things in the balance of reason?
Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful?
--Zachris Topelius
Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies.
-- Sarah Hoyt
|
|
|
|
|
CodeRush from DevExpress does that too.
|
|
|
|
|
In this case it's probably not worth getting angry over. Look what the compiler made out of this little test with a false condition:
test = (1 == 0) ? true : false;
00000037 xor edx,edx
00000039 mov dword ptr [ebp-40h],edx
It even pulled the old XOR trick out of the hat to set EDX to 0 It effectively realized that the condition will always be false and simply copied 0 into the variable.
Seriously, coding practices (good or bad, old or new) may become problematic when they are used because somebody said so or because they have always been used. Good practices and rules are not a good substitute for knowing (or being able to verify) what the compiler will make of your code. I had the dubious pleasure of working in a 'best practices' team recently and do not think very much about their religious belief in their rules and that nothing can go wrong as long as they firmly hold onto them. The gray mass in their heads is not just there to keep their ears apart
I'm invincible, I can't be vinced
|
|
|
|
|
I won't dispute that this is harmless for the generated code, its just redundancy in source code, and in high-level languages (such as C#), that should often be a higher priority.
|
|
|
|
|
Code is written for people, not computers.
|
|
|
|
|
May I just point out that C++ is a different language? Why would a C++ veteran be given a pass on something like this? If I started doing things in C# because that's the way we had to do them in VB6 or JavaScript, I would get laughed out of this forum.
I don't care if the compiler optimizes this away, it's the fact that it's there to begin with that is the problem.
|
|
|
|
|
I have seen such stuff many, many times. They always claim that such approach makes it "easier to read". But the question arises: "For whom?" Don't want to answer, because it will be abusive for some. But writing such sort of a code is the same (yes, it is) as putting the caption "car" on a car (you can make up more examples if you wish). Thanks, I noticed
|
|
|
|
|
This particular redundancy is a bit crude, but I've seen others that, while technically just as redundant, struck me as quite forgivable. The dividing line for me is whether the redundancy aids legibility or maintainability. If it contributes to either of those virtues, I'm inclined to shrug.
Of course, "cut-and-paste" redundancies, which replicate code merely to avoid having to think about generality and modularization, are not forgivable, as they degrade maintainability. But that's grammar-school stuff; a programmer who doesn't grasp it should consider hanging it up and going to work in a coal mine.
(This message is programming you in ways you cannot detect. Be afraid.)
|
|
|
|
|
Redundancy in code redundancy
modified 20-Oct-19 21:02pm.
|
|
|
|
|
The value of true may change.
Psychosis at 10
Film at 11
Those who do not remember the past, are doomed to repeat it.
Those who do not remember the past, cannot build upon it.
|
|
|
|
|
That way, ternary operator sucks a bit less.
|
|
|
|
|
This could have been written because there is an annoying compiler warning with Visual Studio about coercing an integer to a boolean type.
Quite a lot of useless code has been written to silence compiler warnings.
|
|
|
|
|
It wouldn't have to coerce an integer to a boolean...
|
|
|
|
|
At least it wasn't:
popToSelectedButton.IsEnabled = !(historyListBox.SelectedItems.Count > 0) ? false : true;
That kind of thing doesn't not happen all the time!
- Life in the fast lane is only fun if you live in a country with no speed limits.
- Of all the things I have lost, it is my mind that I miss the most.
- I vaguely remember having a good memory...
|
|
|
|
|
Forogar wrote: That kind of thing doesn't not happen all the time!
So true! I don't often see the use of double negatives used "properly" in English, so I automatically corrected it in my head to what you "really" meant. That didn't make sense. Ah, you intentionally mangled the English to get an expression as valid as your code example.
Your mangled code is superior to the one I came up with. ...== 0) ? false : true;
|
|
|
|
|
5 stars... that made me laugh.
|
|
|
|
|
I do this when converting bool to BOOL or back, for two reasons.
1. Code clarity.
2. Eliminate warnings.
And as someone else pointed out, conditional expressions didn't always return a bool type. They used to return integers. So this could just be habit.
|
|
|
|
|
Not in C#, I hope!
(This is from MSDN's WinPhone SDK, so obviously not C/C++)
|
|
|
|
|
Obvious to folks developing in that arena I'm sure.
|
|
|
|
|
Rob Grainger wrote: OK, its harmless enough..
True, but its obfuscating redundancy, and its begging to be abused or typoed into something like:
popToSelectedButton.IsEnabled = (historyListBox.SelectedItems.Count > 0) ? false : true;
and that would be harmful.
We can program with only 1's, but if all you've got are zeros, you've got nothing.
|
|
|
|
|
It seems to be a characteristic of Microsoft programmers, that they want to prove they are clever by producing less readable code.
At times, the attempts at "clever" also makes the code a bug. It took me a week to convince the manager this was a bug:
if ((CurrentThreadCount - StepThreadCount) <= MaxThreadCount) getMoreThreads();
And that was clear-cut code, not even close to "clever". Three to five seconds after I first read it, I saw the bug correction needed:
if ((CurrentThreadCount + StepThreadCount) <= MaxThreadCount) getMoreThreads();
(It did take me about 40 seconds to verify in my mind my first thought for a correction was correct and about 15 minutes to go through the code and verify the names and method did what was suggested.)
What I did like about it was that the variable and method names were so clearly doing what the names suggested was being done.
|
|
|
|
|
Filed under [Clock Cycle Fetish]
|
|
|
|
|
It might not be so bad depending on the context. If this code was ported into C then it might avoid a problem in interpreting what true and false actually are. Then again might have been C code originally and simply copied into place ...
|
|
|
|
|
If you're writing code in which the meaning of true and false are in question, this would be the least of your problems at a guess. I've never experienced such issues, and I code widely in C, C#, C++, JavaScript, occassionally D and Java. I'm assured no such issues are common moving to PHP either by my learned friend.
|
|
|
|