- 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
Um, let's look at the code:
And now let's look at your comment:
Seeing as this is C#, not C++, you're wrong on all counts.
How can you possibly state categorically that this is C# code, from the information provided? You can't, because SqlCommand can easily be a class, and the rest is easily C++/STL code. So, you're not exactly "right on all counts" yourself.
Admin
What you don't get is the WTF has nothing to do with the specific piece of code -- it is an EXAMPLE! did you read the original post? Did you read the comments where we also reiterated this fact a few times?
Nothing is sadder than people who attempt to demonstrate their superior intelligence when they have no clue what is actually going on ... (though it can be entertaining sometimes, I guess)
Also -- try logging on or at least signing your posts so we can at least know which posts belong to who. That's the most frustrating thing about this site: "I am going to prove how smart I am to the world -- I just won't let them know who I am in case I happen to be wrong!" Weak.
Admin
And it's instantiation isn't valid C++. If it were:[code language="c++"]SqlCommand *cmd = new SqlCommand();[/code] then you'd be correct. But it's not, so it cannot be. The other correct C++ instantiation would be:[code language="c++"]SqlCommand cmd;[/code]
Admin
Admin
Thou shalt be careful with words such as "cannot" et al [;)]
Admin
I've met this kind of approach in some code I've inherited. Only in PHP.. something like
[code language="php"]
$rs = mysql_query("SELECT * FROM some_table");
while ( ($rc = mysql_fetch_row($rs)) !== false ) { $count++; }
[/code]
I have the former developer suspected for not knowing exactly what a database is.. I've found this somewhat related code:
[code language="php"]
$rs = mysql_query("SELECT * FROM users ORDER BY username ASC");
while ( ($rc = mysql_fetch_array($rs, MYSQL_ASSOC)) !== false )
{
extract($rc);
if ( $username == $search ) { break; }
}
[/code]
Sad, isn't it..?
Admin
Finix:
Yes, but seeing as it would be leaking memory all over the place in the given context (in C++), it's not very interesting.
Admin
Oh, I see. So because it's an example, I'm not allowed to point out the errors in it. Wow, excuse me. Surely the point of this IS to point out the errors in the example - after all, that's what everyone else has done with the SQL part of this snippet. And as this is an "EXAMPLE" (sic), how can you state it's C#? The argument you're using against my comment works against yours, too. There is nothing in the code to prove, definitively, what language it is.
Signing my posts so you can know who I am? What does that have to do with anything? You know the same person is responding. Anyway, thanks Jeff, now that you've posted with your name I know, uh, well, nothing about you. Irrelevant point, but feel free to continue attacking off-topic issues such as whether or not I'm logged in. It's easier than discussing the issue at hand.Badly written code that, oh, I don't know, leaks all over the place. Wow, someone should start a site that posts those pieces of code for people to chuckle at. They could look at it and respond, "WTF!". Someone could update the site each day with new things to laugh at. You could probably even call the site "thedailywtf.com", but looks like that FQDN is taken. Never mind.
Also LordHunter, your previous comment about the code being C# due to the use of <font color="#0000ff">string </font>is not necessarily correct. As I said before, <font color="#0000ff">string </font>is also an STL type.
I apologise for interrupting your clique with valid comments that you didn't take the time to fully consider.
Admin
<font face="Georgia">I propose a new rule: commenters may only throw idiot hissy fits if they're logged in. Granted it could seriously reduce the number of comments, but I'm sure the benefits outweigh the costs.
<font face="Georgia">... which is obviously much easier to read!ObWTF: they should have done it in Scheme. Using macros. And completions. With a suitable set of definitions, the entire original posting could be rewritten as:
</font>
</font>
Admin
>>I apologise for interrupting your clique with valid comments that you didn't take the time to fully consider.
Wow ... you simply just don't get it. [:(]
If someone asks you your name, you interpret that as an attempt to exclude you from their "clique" ? Come on, grow up!
Please stick around and contribute, and if you want to post multiple times and have an intelligent discussion, don't you think it would help if you somehow mark your posts so we can distinguish yours from the other anon posts? Do you understand that? Look at the anonymous posts in this thread -- how can you tell who is responding to who? We don't want your friggin social security number, we don't need your real name.
Admin
> There is nothing in the code to prove, definitively, what language it is.
Perhaps not, but I find it hard to believe anyone familiar with both languages could really suggest that its not C#. The use of names originating from the System.Data namespace alone is enough to convince me.
Admin
Yes, you're right. Albeit it doesn't necessarily leak anything, it isn't very interesting. But, then again, so is the whole function in the given context.
Just put that constructor there to prove your statements ("no valid C++", "cannot be") wrong.
Admin
I'm with that other anonymous guy. I don't like being harassed to "log in" with a frickin' screen name.
What is this...big brother? Why do you want to track us so bad?
I'm never logging in. And that's just to prove a point to Jeff S. and all you other forum militants.
Admin
What's with the (sic) ?!?! Or do you not know what (sic) means?
Admin
<FONT style="BACKGROUND-COLOR: #efefef">This piece of code has the potential to do a lot more than just count. </FONT>
<FONT style="BACKGROUND-COLOR: #efefef">It's prime for a SQL injection attack.</FONT>
<FONT style="BACKGROUND-COLOR: #efefef"></FONT>
Admin
Yes, I see.
Perhaps the original authors are way ahead of everybody else, and decided to put a few backdoors into these systems.
Admin
By the way... C# does have pointers. You'll have to use an unsafe block, or mark the whole assembly. It'll obviously need full permission to run too.
Admin
Aside: if you're going to edit a direct quote, please do so according the rules of grammar. I usually don't care much on an online forum, but misquoting of direct quotes and misediting as well tends to upset me. It's disrespectful to someone else's words.
I showed my justifications as to why it must be C#. While I cannot conclusively say it's C#, of the languages in common use on this forum, it's most likely to be.
But, seeing as in C++, in order to copy an object from a pointer to an object, you must explictly make a constructor to do so, it's reasonable to assume the code author would have been aware of, and dealt with, the memory issue. You can't just copy construct from an object pointer in C++. Seeing as the construct shown is identical to the only way to initalize an object in C# and Java, and it's not a "normal" C++ construct, it's highly unlikely it's C++.
Nevermind that even though you can do it in C++, it's a rather nonsensical and dangerous construct anyway, for the very reason that code points out.
You'll also note I never said it could not be C++ because of the use of the word "string". I said it couldn't be Java. I said it wasn't C++ due to the way the SqlCommand object was constructed.
And I apologies for interrupting your childish, anonymous, highly inaccurate and simply wrong tirade against the posters of this site.
Fact: Your first post pointing out the errors in the code contained several errors, even if the code was C++. Notably:
The object in the code isn't declared as a pointer in any language we've considered. You falsly assumed based on the use of the "new" keyword and the fact the construct is very much like freestore allocation in C++, that the declared object must be a pointer. The only way it could be is if "SqlCommand" were a typedef, but the code would fail to compile anyway, because it's allocation would fail.
The fact of the matter is that the lines are perfectly valid C# and/or Java, as that's how you declare, allocate, initialze, and allocate objects in both languages. As I pointed out, it's close to being valid C++, and it could be if and only if there is a constructor for "SqlCommand" that constructs from a pointer to a "SqlCommand" object. And if that were true (and it was valid C++), the line you point out as wrong would still be valid anyway, because the object being used isn't a pointer at all, but a live stack-based object. Meaning you use the dot operator, not the arrow.
So, you're wrong on point number 1.
I'll ignore the memory allocation issues since you're contesting the language so hotly [8-)]
You're wrong on your last point about the meaning of 'static' w.r.t to a function. In C and C++, for a non-member function, static affects the linkage of the function and makes it local to the file it was declared in. In all relevant languages, a static member function (meaning the member of a class) means that it's data is shared between all instances of an object; it does not recieve a pointer/reference to the object it's being called from. It also means it can be called without any instances of the object being alive. It does not mean the function needs to worry about allocated resources or checking to see if they're NULL. I'm not sure where you came up with that, but I think you were thinking of static local variables, which retain their value between calls to a function.
I'd like to point out as an aside: you're quibiling about the language this code could be, but you can't even point out what's wrong with in one language, let alone more than one. Leading me to wonder how you can possibly contest it's language. It's not like the code's a trick question or anything.
Yes, I know, but they're not frequently used, so I "omitted" them ;)
Admin
I think the best clue to this example being C# is that Alex wrote it. And if I remember the previous items on this forum correctly Alexprefers to use a .Net language.
This doesn't prove anything, but I just wanted to point it out. (And I could be wrong about Alex).
Drak
Admin
Huh? You can tell them they are idiots.
In ASP one typically uses ADODB.RecordSet, and that has a property called .RecordCount that (indeed ! Who'ld have thought?) has a count of the records. In some (!! exceptional ) circumstances, it may be necessary to move the cursor to the last record before the recordcount is available though. But moving to the last item is still not a scripted loop with a counter.
Admin
You know... you're all pretty lame. And what's the fun in "making up" WTF's like Alex did?
Admin
Here's another great forum falling apart because of the trolling... WTF, people, we were supposed to have fun together!
Admin
It's not the trolls themselves who spoil the fun on this forum. It's the people who respond to these trolls who are making things worse. Give a troll some attention and he will continue to troll around some more. Ignore the comments the trolls make and they soon get bored...
Admin
Admin
One was in C#, another PHP, and the third in VB (either Script or 6). The PHP function had the table name as a param (like this: "select * from $tableName").
You are correct, I do prefer .NET. But it really came down to the fact that I have a Syntax Hilighter that easily does C#. It is kinda fun though to see the "what language" discussion, though :-D.
As for the trolls, when I roll out the new version of the software, anonymous posting will still be permitted; there is too small a percentage or readers/registeredUsers. But the posting will be more .Text style ... you enter your name and URL, and it remembers you via cookie. I think that will encourage better discussion ...
Admin
And you know why? You're a coward. You're afraid that if you post enough BS people will see your name, assume BS and ignore you. Or worse, tell you to STFU.
Admin
Typically we have always used SELECT COUNT(Id) FROM Table . Since Id Is the Primary Key and cannot be null.. After a bit of testing this is faster by a tiny bit. (1.193 vs 1.86 sec) Your table does have a primary key right?
Admin
I've seen it too, I think because count on it's own will not return a column name. The way around it was to:
select count(*) as RecordCount from MyTable
Admin
That long to return a count? My goodness!! All RDMS I've worked with (even oracle) will do a full index scan on the PK index if * is selected. What DB are you using?!?
Admin
<FONT color=#0000ff>"In ASP one typically uses ADODB.RecordSet, and that has a property called .RecordCount that (indeed ! Who'ld have thought?) has a count of the records. In some (!! exceptional ) circumstances, it may be necessary to move the cursor to the last record before the recordcount is available though. But moving to the last item is still not a scripted loop with a counter."</FONT>
<FONT color=#000000>You're right, RecordCount is not a scripted loop, but it still may end up being a loop that pulls all the records to the client side to count them:</FONT>
<FONT color=#006400>"If the Recordset object does not support approximate positioning, this property may be a significant drain on resources because all records will have to be retrieved and counted to return an accurate RecordCount value." </FONT><FONT color=#0000ff>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdprorecordcount.asp</FONT>
<FONT color=#000000>The default and most efficient cursor for most uses (adOpenForwardOnly) does not support approximate positioning. So if you do not RTFM and use RecordCount blindly you may be asking ADO to run the same silly WTF loop, but hidden from your view of course. It's not a problem if your result sets tend to be a dozen or so records that you are going to process anyway, but it's a massive cluster-WTF if all you wanted to do was count records.</FONT>
Admin
I have seen books recommend using COUNT(1) instead of COUNT(*) because of bugs in previous versions of Oracle and I have seen the difference in performance.
Admin
OH DEAR GOD! WHAT A SERIOUS MEMORY LEAK!! ...
You're FIRED!
Admin
you forgot the <font face="Courier New">(requires 'srfi-666) <font face="Times New Roman">part. [:P]</font></font>
Admin
Admin
Hey that's MY line! [:P]
Admin
ow man, are you trying to get me killed ... [:'(]
Admin
This is C# and
Table.Rows.Count would have taken care of this. Enough said.
Admin
Sarcasm? A "devil's advocate" approach? Honest opinion of the best approach? Typical anonymous troll?
Might be just me, but how can you tell? I will assume, due to the lack of icons and/or excessive punctuation, that the poster is serious.
And, therefore, deserves a coveted "WTF Comment" award. And a response:
Did you read any of the comments? The database layer should return 1 single value, 4 bytes, accross the network -- SELECT COUNT(*) -- and THAT'S IT. To use Table.Rows.Count or something similiar, the DB must return ALL ROWS from the table to the application. And that is exactly what the "WTF" of this example is doing, and that is WHY it is a "WTF".
Whether you loop manualy or use a method on the client that does the same, you should never do this.
Admin
<FONT size=2> </FONT>
<FONT size=1>This is C# and </FONT>
<FONT size=1>Table.Rows.Count would have taken care of this. Enough said</FONT>
<FONT size=1>This is a lesser WTF and really only excusable if you've already asked for the full dataset that you want to count for some other god forsaken reason, like filling a dropdown list or something, and have already fetched all the records locally. At that point you may as well just get the record count by asking the memory object for its Rows.Count. </FONT>
<FONT size=1>In all other cases 1) you don't need all the data from every row locally or 2) you haven't bothered to fetch all the data across the network and don't really expect to. It is faster and easier to 'Select Count(*) as RowCount from <TableName>' (feel free to replace * with <PrimaryKey> if you like). Aliasing the column will work around the crappy drivers problem if that is a problem for you. </FONT>
Admin
From every test I have ever seen of this in Oracle, there is no difference in performance between Count(1) and Count(*). For a fairly detailed test case, check this link out: http://www.speakeasy.org/~jwilton/oracle/count-star.html
Admin
No. select count(*) allows the server to choose the most efficient method of getting the count from the specified table. Select count( blah ) might force the server to use a column that has no index and probably end up running something similar to the WTF code that started this travesty.
Admin
Yeah, nice, and then you still have to stream all that junk across the network to your client. What good did it do you to utilize the cache, again? :)
Admin
[:$] de que hablan ? no es necesario hacer ningun procedimiento para contar los registros si existe la funcion count
Select *, count(id) as num from table1; [H]
Admin
Sorry [:$]
Correect...
Select *, count(id) as num from table1 group by name;
Admin
That's not nice that you made fun of whoever wrote the loop thing but you never said what's the right way of doing it!!
So...
myCmd.CommandText = "SELECT COUNT(*) FROM the_table WHERE id = 5"
myReader = myCmd.ExecuteReader()
Dim amount As Integer
If myReader.Read() Then
amount = myReader.GetValue(0)
End If
Admin
Admin
I refuse to be slandered. I do not write code like that, I write right code. But Jeff would disagree because he always says I should write LEFT code.
[:P]
He look, a martini...[D]wish Bill would put one of those up...
Admin
You're still here? Having trouble finding other websites to browse?
Admin
Brilliant! Of course you're right: returning the ENTIRE table to the middle-tier is much faster than asking the db for a count...
Admin
Get a brain moran !
count(*) multiplies the count by the number of columns !