- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Just because they didn't die in the crash doesn't make them immortal, after all.
Admin
Admin
Admin
I guess that's the whole point of the negated condition check, although personally I think it is not well implemented.
Suppose the code is written on day1 with this intention
if (condition1) { doSomething(); } else if (!condition1) { doSomethingElse(); }
It could be that the doSomethingElse() only work if !condition1, and it could be castrophic if doSomethingElse() is executed if condition1 is true.
e.g. if (alive) { doMonitorHeartBeat(); } else { injectFormalin(); }
Now consider a feature request to introduce a new switch to monitor heart beat, now an inexperienced developer could write code like.
if (alive && switch == "On") { doMonitorHeartBeat(); } else { doInjectFormalin(); }
Now he kills everyone when the switch is off!Now you see the value of insisting the else block check? Consider the doMonitorHeartBeat() is expanded into something like 300 - 400 lines - it is really easy for one to miss that.
I feel like the right solution is Assert [Assuming you turn on assert during testing, and there are enough test cases]
if (alive) { doMonitorHeartBeat(); } else { Debug.Assert(!alive); doInjectFormalin(); }
or to be extra safe - to ensure the code doSomethingElse, despite a careless programmer comes in - can be executed.
if (alive) { doMonitorHeartBeat(); } else if (!alive) { Debug.Assert(!alive); doInjectFormalin(); } else { Debug.Assert("Should never reach here"); }
Admin
Look - the subject of the article applies!
Anything can happens! That "Anything" also include someone can change your code. Having robust code is nothing wrong.
Admin
Too bad you proceeded to fuck it up:
You need the redundant test inside doInjectFormalin(), not here in the if statement.And you need the redundant test in release builds. You DON'T need this:
} else if (!alive) { #if debug alert("fatal bug"); abort(); #else // kill(patient) in 3 ... 2 ... 1 ... #endif doInjectFormalin(); } else {
Admin
I always assumed it rhymed with "plague". But http://dictionary.reference.com/browse/ague tells me it is "ay-gyoo".
Admin
Hey, there are languages where Doug's pattern makes sense. For example in (System) Verilog:
Admin
FTFY: "That seems like good practice for any software."
BTW, MISRA also enforces that.
Admin
I see this had a happy ending: no patients being fried with 4 times the radiation they needed!
It's better many times to get a developer that is green that can be trained than the 'experienced' developer.
Admin
if (a) { doSomething(); }
or else { youHaventHeardTheLastOfThis(); }
Admin
yeah, bury that poor fucker alive :-D
Admin
This code reminds me of a guy named Bob I used to work with - he also had an interesting conditional pattern used everywhere throughout his code:
with the same (sometimes not quite right) duplication of the conditional check as Doug's code. Also sometimes the conditional had side effects, which got applied twice...
Apparently all this was necessary because "you never know - it might change between the 'while' and the 'if'"....
Admin
The infamous negation operation may indeed cause interesting side-effects. Long time ago as an intern I had to bang my head against wall in a certain VB6 (sigh) project. A function in a certain COM component returned strange, ill-behaving "Boolean" values, which were not quite defined in a standard way.
#define TRUE 1 #define FALSE 0 #define FILE_NOT_FOUND !TRUE
fileExists=FILE_NOT_FOUND;
if(sfileExists) { copyFile(); } else if (!foo) { showError("File not found!"); }
Admin
Of course I ment to write the code as following.
Admin
Admin
Admin
We've got a "what if something happens" person on our team. She knows literally fuck all about SQL or ORMs but can claim that "remote paging and sorting is really inefficient because you keep hitting the database. it would be much more efficient to select all the rows from the table, keep them in memory and do the paging and sorting from there".
Despite the fact that there's this thing called "Structured Query Language", databases must only rarely be queried, because it will impact negatively on performance..
When I asked her about concurrency in case records are added, deleted or updated she went all quiet, then countered with "opening DB connections is an expensive operation, you don't want to do that on every request". Then I asked her if she knew anything about connection pooling..
Putting the F in WTF.
Admin
What about that:
Not that I would endorse this kind of programming, because of it being pretty unpredictable, but still. Also, C++ operator overloading could actually yield different results for (bool)(a) and (bool)(!a). Not that I would endorse that kind of programming either, but the following would actually be able to execute all three code paths, depending on operator overloading:
Admin
If a is true doSomethingElse(); would not be executed, because it doesn't check for the second if.
This instead would execute both:
Admin
Obviously he was just trying to account for the tri-state boolean.
Admin
Let's face it. It's another victim of Visual Basic, the only language where a boolean may be something else than just true or false. It gives a whole new meaning to the "else {" structure.
Admin
Seriously Irfanview, WTF?
Admin
Admin
I would suggest asking Bob "what happens if it changes between the 'if' and the 'do something'? Better put another 'if' in there."
Admin
Which is where the person's 'decades of experience' kicks in, he's had to deal with 'compiler idiosyncrasies' before, and wanted to guard against that.
Image your C++ example was built into the compiler itself, and then you went and changed your Boolean logic to fit a coding standard...
I've actually had experience with a compiler that would allow certain types of Boolean operations in an 'if' conditional and not others.
Also, if you are getting in complex Boolean logic, then I would recommend an online Boolean simplifier to make sure that you're getting the inversion logic correct. And, to make the code easier to read, I would recommend factoring out just the Boolean conditional to a separate function with an easy to understand name.
Admin
I can't count how many times I've seen:
Admin
Your example is wrong on so many levels, you know.
I guess the point you are trying to make is: In a single bit variable, there could be more states than zero or one. That is true.
Your "initial" happens at sim-time zero. Who is defining the state of 'x'? Also at sim-time zero? You got a race condition on your hand!
Your variable 'x' is local scope to "top". Maybe there is someone accessing 'x' via 'top.x'. However "top" suggests to be the toplevel of your design/simulation (whatever you are trying to do here).
What are you passing into $display()? Is this even valid syntax? It takes a string, no? All 8bit ASCII characters? Then your 1, 2 or 3 are non-printable values. Right?
... might make some more sense.
Yes, tripple = if you compare includes 'Z's or 'X's! (An unitialized reg - the datatype of "x" - will be X, as in state unknown. If it is an undriven wire, you'll see a Z.)
Or you could simply do this:
I am replying to a troll, ain't I?
Admin
Hmm, you seem to be confusing a C/C++ null pointer with a SQL null. In SQL, "null" is defined to mean "unknown" or "inapplicable". (Some insist that it should not be used to mean "inapplicable", but that's another story.) In C/C++, a null pointer is invalid, and exactly what that means is up to your program. You may interpret it to mean "unknown" -- I often do. Or you may interpret it to mean "invalid" or "Thursday" or whatever else you want it to mean. Or you may interpret it to mean "oops I forgot to set a value and now we're about to blow". Anyway, if you try to think of SQL nulls in terms of C null pointers, you're just going to confuse yourself. They're not the same concept.
Admin
Just to be pedantic, in SQL "null!=null" is not a true staement. Comparisons in SQL have three possible results: true, false, and file-not-found ... I mean true, false, and null. Null in this case meaning "unknown". So "null=null" gives null, unknown. "null!=null" also gives null.
Actually this is not entirely pedantic. Consider:
What is the output? Neither query returns any records.
If it were true that null!=null, then the second query should return two records, because 42!=null and null!=null. But in fact it returns no records, because null!=null is not true, it is null.
The technically correct statement would be that "null=null" is not true.
Admin
The real catch to code like this is, suppose you came along months later and found:
Now you're left wondering: Does this code INTENTIONALLY do nothing when a==30? Or is that a mistake? If I saw such a statement in a program that did not have the pattern of negating all the IFs, I'd probably assume it was deliberate. But with all the negated IFs ... it's hard to say. You'd have to study the code and figure it out.
Admin
Doug was probably told "Do not repeat yourself".
So he repeated the opposite.
Brilant.
Admin
[* censored because I'm watching my language. The F at the beginning of that language doesn't stand for fuck.]
Admin
Admin
In C++, a null pointer can be safely compared to a pointer for equality:
You can assert( NULL == NULL ); and you can assert ( NULL != p ) if p is not NULL.
(p < NULL) and (NULL < p) are technically "undefined behaviour". They shouldn't crash / core-dump you but it's undefined whether they will return true or false (or file not found..)
Admin
Someone said that the complete name of Doug is "Doug DeMorgan"!
Admin
ay gyoo
Admin
Um... as an engineer that often writes code, um... no. Your 'Doug' programs because he wasn't a solid engineer either. I also notice plenty of 'Dougs' from a CS background as well, inflexible, superstitious, socially inept, keepers of the cargo cult.
Admin
Great, lets mince words then. We are talking VERILOG here, right? How do you define a single bit variable? You either use 'reg' or 'wire'. And here you have (at least) the 4 states you mention. In VERILOG, is there any other way to declare a single bit variable? Apart from 'reg' or 'wire'?
Look, in my initial statement, I am not disagreeing with you about the sentiment: "More than 2 states may exist and one might need to test for each of them." All I object to, is your choice of example. I have fallen victim of people deploying non-real-world (accademic) examples as a decoy before. That is why the "biting-reflex" engaged so readily.
Yes, the initial value of reg 'x' will be indeed 1'bx, as it has no assignment to it.
I am a bit wary about people who are quick in citing the "STANDARD". Many of them - in my personal experience, YMMV - do not really know what they were talking about.
I will give you the benefit of the doubt.
OK, fair enough, I apologize for calling you a troll. These days, no one knows who is real and who is faking it!
I may be replying to a grad-student, a dedicated lecturer from a university or a veteran of many years of digital design - however the last I believe is unlikely. I do not know - the blessings of the internet.
I have run the code you posted. (I did not even need to use any System-Verilog capable simulator to do that. Why the mention of System-Verilog in the first place? This is just plain VERILOG 101.)
Well, I'll be damned:
It outputs '3' and hence took the branch wich is neither true or false. So maybe this example really tells us something?
And OK, $display also takes an integer as argument. I did not know that. So this I learned - but I certainly won't apply that knowledge in the future. I'd like more meaningfull messages in my simulations, such as:
$display("state 'x' detected");
I might have done it this way:
Which has a more verbose output:
But then try to explain the finer points of HDL to the audience. So maybe your post had a point after all.
Admin
Actually, I've sen that too at my own eyes, so it is no exaggeration at all. When one very senior developer has explaining some things to me (I was a newbie), he used exactly the same method (copying by mouse through menu). When I saw that, I really wanted to take the keyboard off him and do it by myself (but I didn't, obviously). Needed to say, that he grew up on mainframes, was very good on that and didn't care much about some strange windowed systems :). But I must admit, that the code he wrote was a mess - he basically wrote in C++ like if it was assembler.
Admin
I'm just glad that Doug hadn't heard of ?:
Admin
...
No, dude... Just... No. That's horribly offensive.