- 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
I saw something similar to this in a customers code that was done intentionally. They were afraid a critical call would error out, so executed it 3 times in 10 minutes (Starting at 1AM) just incase. Unfortunately the call invoked a process that took 4.5 hours to complete, and they had 'assumed' that if it completed the first time that the system was smart enough not to execute the second and third instances without any instructions or error handling.......The only reason I was brough in was because the server was locked up every morning when they came in as the instances ran serially....DOH! [8-|]
Admin
And even worse that they then edit generated code. This way lies misery....
Admin
As far as I can tell, FieldMap.Organization, FieldMap.Tag and FieldMap.Slate are all enumerations which contain the mapping of column "names" to indices. Thus "dr[FieldMap.Tag.is_active]" is getting the "is_active" field from a DataRow, which has been populated from the Tag table.
The cast to bool can be explained too. This definitely looks like C#, in which case the operator [int] returns the value of the specified column. It's most likely a bit field (SQL Server) or a yes/no field (Access MDB). As the value's returned as an object, the cast to bool would be valid.
But yes, the WTF here is the triple check. It obviously won't avoid any kind of race condition, because:
(1) The data can still change between the last check and when addBlahRow() is called.
(2) The data is almost certainly cached client-side, making each check return the same value.
(3) The compiler/linker might optimize it all down to 1 check anyway.
Admin
Ah, but if he was going to refactor the chek out to a function then surely you want to be certain...
Admin
Yeah.
Bad geek! Bad, BAD geek!! No pizza!!
Admin
This is a good example of test-driven development.
:)
Admin
Irish?
Admin
First of all, this does appear to be C#; not because of the syntax itself, but that it is also using a DataRow which is one of the ADO.NET classes.
"FieldMap.Organization.is_active" is either a static or a const 'int' or 'string' value; it cannot be discerned which since the indexer (or 'Item' property) for the DataRow is overloaded. It cannot, however, be an enumeration in C#, because enumerated values are not implicitly 'int' types and would have required an explicit cast to an 'int' in order to compile. (Of course, I'm making the assumption that this code snippet did compile without error.)
Because the indexer for DataRow returns an 'object', it is either a reference to some other class type or a boxed value type (which is the data contained in the referenced column for that row) and would be required an explicit cast to unbox the 'bool' in order to use it in a logical expression (i.e. the 'if' statement). The "is_active" somewhat reinforces that the referenced column in that data row may very well contain boolean data.
"organizations", "tags", et al. are DataRowCollection objects which are most likely assigned from the 'Rows' property for the given table they are iterating through to examine.
This could possibly be the result of code that was spit out by some type of code generator; perhaps the original author tested the generated code, found that it worked and the resulting code that was generated henceforth was never questioned by anyone that used it -- out of sight, out of mind. That is, of course, until a new developer comes along that needs to either make changes to the generator or examines the generated code to determine how things work.
Admin
"its always a good idea to check everythign a few times before you process any data.... *gag*"
Ah, like you checked your spelling! [:P]
Admin
What database could give different results for the first, second and/or third call of is_active, even considering concurrency and stuff?
Admin
To debunk all the theories.....
2) It wasn't generated by a code-generator, it was hand written.
3) I wasn't drunk when i was first reading the code. I wanted to get drunk immediately afterward, but lunch wasn't for another hour.
4) The explanation i received was that the value in the DB might have changed after the first if statement so they checked it again... twice. Why only twice? I don't remember if i asked that or not. The funny part about this is the values are already pulled from the db and are in the DataRow, and will not change unless the DataRow is repopulated, so there is absolutely no reason to check it three times.
I believe it was due to copy/paste, but will probably never know.
Of course, this code is from the same guy who chose to implement the entire project without using form designer because he believed the .res files that the form designer generated created too much overhead. This was the main reason for the file being 28,600 lines (yes, exactly 28,600.)
Admin
RE: the Lewis Carrol school of programming.
You, Sir, are one of the rare programmers educated in programming history.
I believe the Bellman was modelled on Charles Babbage, so the triple-if is clearly the only way to evaluate boolean propositions.
Admin
Almost, but you forgot the mandatory IsTrue() call.
if (IsTrue((bool)dr[FieldMap.Organization.is_active])){
if (IsTrue((bool)dr[FieldMap.Organization.is_active])){
if (IsTrue((bool)dr[FieldMap.Organization.is_active])){...
;)
Admin
No it would not, or at least I hope not. The compiler looks at code and checks it for logical/structural errors( if command without then) and internal reserved words/commands (do = do +1).
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>
The reason why I think this was done is to avoid the ‘wait (X)’ seconds command to see if the changes occurred. Why would he avoide it? I don’t know.
Admin
Well I heard that if you stand in front of a mirror and refererence FieldMap.Organization.is_active three times in a row that it comes alive and kills you.
Admin
Actually he is in compliance with the law and we are not.
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>
It is now illegal for US citizens to annoy people without providing your true identity.
<o:p> </o:p>
Read this article: on theinquirer.net. Unfortunately it is not a joke but is truly WTF!
http://www.theinquirer.net/?article=28971
Admin
hahahaha, this code is posted up right outside my cube.... I have written across the paper, "WTF?" in nice software dev. chicken scratch.
a nice copy n paste technique by one of our collegues (actually, a really smart guy, although I still can't figure out why he tried to justify it to me). The biggest WTF is 28,600 lines long tho, and trust me, the repeated if-statements are just the tip of the iceberg.
When ppl are under pressure, when specs are changed after development is done, hell, when there are no specs and a looming deadline sits in front of a hobbled project, ppl will do whatever it takes to spit it out as fast as possible, which usually is (as you can tell) a BAD idea, but one not necessariliy brought upon the project by the dev team. You know what really cracks me up about the previous project was the old management. We had another guy who was contracting for us, good friend and really smart guy BTW (he works on 3D modeling systems now), but didn't know diddly about SQL, and was pretty much asked to finish our whole reporting system single-handedly.
The result for one of our reports was a 36,000 line sproc. I'd love to share it sometime. :D
Sometimes the real WTF is why nobody was listening on either side of the fence when something sounded horribly wrong. The real WTF was a breakdown of the communication lines, and a breakdown of good management, and finally, a breakdown of the dev. team.
It's nice to still work for this company, because they have really turned around. Code separation is nice, and there no longer will be 10,000 (10,000!) lines of migration code in the interface layer because we have a boss that truly believes in practicing outstanding OO development OF a layered architecture, someone with 15 years of experience who will make sure he doesn't see the crap hit the code.
PS: no auto-generating. but trust me, we are laughing/crying with you and are just as mind-boggled as well, I'm sure. DataRows are in C#, pulled from DataTables, DataSets, etc etc etc (I suggest DataReaders for speedy access). They get tied into the Business Objects in a pretty funky way, if you are really that curious... here is a note from the previous developer (DB specialist), who, I might add once again, was a very smart guy, and really knew his stuff (DB expert). It's just that when smart ppl are under pressure without a strong leader at the helm... you get this lurking deep in the base class for the business logic.
//TODO explain this monstrosity better
return (DataRow) DbGateway[(int)((ArrayList)DbGateway.KeyIds[baseTable])[index], key_Name];
Unfortunately, no better explanation was left. And this was true for most of the code, for anyone, anywhere. OO concepts were used, sometimes a few comments were injected in to the spaghetti, and some developers who left the company were kind enough to try to make some sense of the mayhem for the newcomers and let them know the kind of fallouts they experienced. Sometimes you make due with what you got, be glad your company stood by you through it after telling them not to continue the project because it was a POS (P := 'pile'), and be very happy that they made the right decisions to dump the project and to get highly qualified people to lead the team with a passion and a desire to work on something that should be more like a Piece Of Art than a POS.
Admin
My bad, I forgot to put quotes around the word "used" when talking about OO concepts. :D
Admin
<o:p>The language need not have reserved words. FORTRAN is an example of this. I believe PL/I also is.
</o:p>
<o:p>A compiler can do far more than that limited checking. How much it can get away with depends on the language.
Sincerely,</o:p>
Gene Wirchenko
Admin
A good example of get-tough data interrogation techniques. Obviously the data is reluctant and will only participate when pushed.
Admin
He's de-bouncing the keyboard or something similar...
Admin
Hes taking into account an unfair bias on the True condition
Admin
Please see the following snippet from the MSDN documentation on enumerations in .NET:
Unless I'm reading this wrong, it's quite possible for an enumerated value to be implicitly cast to an integer (assuming that the underlying type is an integer or smaller). It's definitely true though that you cannot convert the other way (integer -> enum value) without a typecast.
Admin
Very interesting. I didn't notice it before - but you bring it to light. The side effect of the routine... is the added overhead when the first test resolves TRUE. The delay behavior is amplified based on the probability of TRUE in the data.
Admin
It's not an array - it's a DataRow. FieldMap.Organization.is_active is most likely a const string identifying the column name (prolly "IsActive"). Since this call to dr[] returns an object, it has to be cast to a bool to be of any use... Essentially this code is like saying: "if the organization is active, add the row dr..."
(God I need to leave work!).
Admin
Even scarier: What is the .is_active actually isn't a property but a method? What if is actually does something and maybe needs to be executed a few times before it returns true? Or what if it handles errors with a messagebox and gives the users 3 tries when failing?
*shiver*
Admin
I was not aware that anyone had this level of brillance!
Admin
This is the PERFECT algorithm for politics. Ask the same question three times, or offer three similar soultions. You could run for office with this logic.
Admin
Bingo
Admin
Good for you, Michael Smith
Admin
Was that Michael Valentine Smith?
Admin
assuming we are dealing with strongly typed datasets (since there is no implementation of the IEnumerator in the generic datatable),
the WTF is actually in the checking of the field value, albeit thrice! Even were it once, it would still constitute as a WTF. Imagine having 50,000 records with just 50 records meeting the criteria. To loop through that is unforgivable.
The appropriate way is to first understand what ADO.NET can do for you and then use it. In this case, we use DataViews.
<font size="2">// the view is disposed once it goes out of scope
// we deal only with the DataRows that match the required criteria
using (DataView view = new DataView(tags, FieldMap.Tag.is_active + " = true", string.Empty, DataViewRowState.CurrentRecords)
{
foreach(DataRowView rowView in view)
{
addTagRow(rowView.Row);
}
}</font>
Admin
My thoughts exactly, but perhaps that's because I'm related to an OCD sufferer.
Admin
It's brillant. Wonder why I didnt think of that.
Oh wait.... i wasn't paid by line of code. Phew.
-Paula.
Admin
maybe is_active is a property that checks state of something? and the requirement is at least 3 times? not sure if property works that way in non-vb.net languages
Admin
Where did you get that thought from ? Boolean ? We don't know the type at all. It is perfectly possible that is_active is of type System.Animals.Types.PinkElephant.
dr is a DataRow. The indexer probably is a class full of constants, is_active is the name of their database table field, which probably contains 1 and 0 as there are some databases ( Oracle *hint* *hint* ) that don't have boolean field types. So is_active is probably an integer or string constant to connect the array to the database field. Not as bad as placing a magic "5" in there so that everyone maintaining it has nightmares.
The triple checking really is a WTF, the rest is not.
*prays that quoting will work*
Admin
Admin
Admin
I used to work with a programmer who routinely did something very similar. His while loops were always of the form
while(condition) {
if(condition) {
...
}
}
in single-threaded code too, so no concurrency issues. His reason? "Just in case."
Admin
Ha, ha. Great fun :)
Admin
perhaps some "lock" may be usefull if this is done for concurrency.
anyway, if the value remains unchanged, no need to be thread safe
Admin
Irish coder! To be sure, to be sure, to be sure.
Admin
Is it possible that the DataRow dr contains 3 columns and that each FieldMap.Slate, etc is returning the current Slate before iterating to the next? He's therefore checking each Slate in the DataRow before adding it. But yeah, that's still pretty WTF'ed up.
Admin
You are not reading it wrong, you are just interpreting it wrong. The C# language, while it can create enumerations of different underlying types (of integral values, specifically), does not implicitly cast enumeration values to that underlying type when assigning or passing as parameters. From the MSDN documentation on C# enumerations:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcrefTheEnumerationTypes.asp
Note the section under 'Remarks' which reads: "The underlying type specifies how much storage is allocated for each enumerator. However, an explicit cast is needed to convert from enum type to an integral type."
Admin
It would have to be...
But isn't it interesting that we have a Heinlein side-note in a thread all about the notion that 'I tell you 3 times it's true' - a notion that Heinlein himself was quite inordinately fond of?
The world is truly a coincidental place - or is it?
Maybe it's all run by infinite groups of non-real alien beings :)
Admin
good security, check three times just in case theres a virus modifying random bits of memory
Admin
Therein lies the difference between an ignoramus like your good self and those that take some time to look under the hood. It is faster to use a DataView then to do the loop.
Admin
My thoughts exactly.
Cheers.
Admin
it doesnt use the == operator though!
Admin
it doesnt use the == operator though!