Jump to content



Dumbest coding mistakes ever

  • You cannot reply to this topic
54 replies to this topic

#41 HappyCoder   Members   -  Reputation: 146

Posted 24 February 2012 - 09:07 AM


for (i = 0; i < foo; ++i)
{
     for (j = 0; j < bar; ++i)
     {

     }
}

Luckly I have done this enough that I have gotten pretty good at finding it.

Ad:

#42 Sik_the_hedgehog   Members   -  Reputation: 235

Posted 26 February 2012 - 12:40 AM

Just wrote this. Luckily it got caught as a warning, otherwise it'd have been really annoying. Just so you know, name is a const char*.
for (unsigned len = 0; *name; len++, *name++) {

Why is ++ allowed on a const value? I'd like to say that maybe it's because the return value is modified, but it's a post-increment (so the original value would be returned) and even then there are other easy ways to approach that (e.g. the + operator). Take into account this is C, not C++, so there isn't any wacky overloading going on (and even then, it's a char...). Moreover, it's being compiled with -std=c99, so extensions are disabled.

(and before you ask, no, I wasn't reinventing strlen, the loop code does more than just counting characters)
Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

#43 MaulingMonkey   Members   -  Reputation: 1454

Posted 26 February 2012 - 04:28 AM

*name++ is *(name++), not (*name)++

#44 Sik_the_hedgehog   Members   -  Reputation: 235

Posted 26 February 2012 - 05:59 AM

View PostMaulingMonkey, on 26 February 2012 - 04:28 AM, said:

*name++ is *(name++), not (*name)++
Derp, and the most ironic is that I had remembered that syntax specifically (since it matches one of 68000's addressing modes). I wonder what was I thinking when I wrote that post.
Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

#45 JavierMC   Members   -  Reputation: 164

Posted 28 February 2012 - 04:09 AM

uint16 numVerts = 0;
for(size_t i = 0; i<70000; i++)
	numVerts++;

Twice last week Posted Image

#46 MaulingMonkey   Members   -  Reputation: 1454

Posted 28 February 2012 - 10:43 AM

Here's another really fun one:

for ( size_t i = 0 ; i < v.size() - 1 ; ++i )
{
	...
}


#47 Telastyn   Members   -  Reputation: 1858

Posted 28 February 2012 - 11:20 AM

C#: I was hunting down a bug, so went in and set a conditional breakpoint deep in the bowels of this data access code
entity.Location = "http://....somebiglongstring.html"

And then went to lunch. Came back and bug was still there, but the breakpoint wasn't hit. Rebuilt, redeployed and still a bug. Nobody else got the bug though, and it worked fine in release mode. Step throught the code... data model entity goes into the translator, view model comes out with the wrong location...

Took me 2 days to find the conditional breakpoint which will do the assignment, and will never actually trigger since assignment isn't "true".

#48 Alpha_ProgDes   GDNet+   -  Reputation: 1299

Posted 28 February 2012 - 12:23 PM

View PostJavierMC, on 28 February 2012 - 04:09 AM, said:

uint16 numVerts = 0;
for(size_t i = 0; i<70000; i++)
	numVerts++;

Twice last week Posted Image

View PostMaulingMonkey, on 28 February 2012 - 10:43 AM, said:

Here's another really fun one:

for ( size_t i = 0 ; i < v.size() - 1 ; ++i )
{
	...
}

I acknowledge I'm a bad C++ programmer, but what exactly was the problem with those pieces of code. They both will compile and run. Won't they?
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts :)

Spoiler

#49 swiftcoder   Senior Moderators   -  Reputation: 1617

Posted 28 February 2012 - 12:28 PM

View PostAlpha_ProgDes, on 28 February 2012 - 12:23 PM, said:

I acknowledge I'm a bad C++ programmer, but what exactly was the problem with those pieces of code. They both will compile and run. Won't they?
The first one is just a waste of time - he might as well have set numVerts directly.

The second will fail horribly if v.size() == 0.
Tristam MacDonald - swiftcoding [new blog post: bidding a freelance contract]

#50 way2lazy2care   Members   -  Reputation: 257

Posted 28 February 2012 - 12:38 PM

View Postswiftcoder, on 28 February 2012 - 12:28 PM, said:

The second will fail horribly if v.size() == 0.
Why would it fail? Wouldn't it just do nothing? Or is it because v.size()-1 presumably is unsigned since it's being compared against a size_t so it would run MAX_INT times?

#51 swiftcoder   Senior Moderators   -  Reputation: 1617

Posted 28 February 2012 - 12:47 PM

View Postway2lazy2care, on 28 February 2012 - 12:38 PM, said:

Why would it fail? Wouldn't it just do nothing? Or is it because v.size()-1 presumably is unsigned since it's being compared against a size_t so it would run MAX_INT times?
Because size() returns a size_t, which is unsigned. And (unsigned)-1 is a very, very large number...
Tristam MacDonald - swiftcoding [new blog post: bidding a freelance contract]

#52 alnite   Members   -  Reputation: 709

Posted 28 February 2012 - 01:18 PM

The first one is an uint16, which presumably, a 16-bit integer whose maximum value is 65536. That loop iterates 70000 times.

The second one is the conditional
i < size()-1
. The last element won't get counted for. And of course, if size() returns 0, it will evaluate to a very large number.

#53 Luckless   Members   -  Reputation: 620

Posted 28 February 2012 - 02:21 PM

How is this for a brain dead mostly asleep error?

Trying to work on something while way past too tired to program, but I needed to get something done that I could actually show someone.

started with
for x in a:
    for y in a:
	   # Do something with x[0] ... y[1]
#A short time later...
for x in a:
    #Do something else x[0] .... y[1]

Also managed to screw myself over trying to hunt down a bug over position 1 vs 0 of a list. Translating some notes written by someone else into code can be dangerous. The issue was so hard to track because for most of the data set had the same value in the first two elements of the list, except for a few rare cases.
Old Username: Talroth
If your signature on a web forum takes up more space than your average post, then you are doing things wrong.

#54 markadrake   Members   -  Reputation: 104

Posted 28 February 2012 - 02:54 PM

if(x % 1 === 0)

I did this a few nights back, I was up really late playign around with JavaScript and the Canvas HTML Element and for some reason needed to test if something was odd or even...

This is obviously not how you do it.
Mark A. Drake
OnSlaught Games
Mark Drake

#55 Bacterius   Members   -  Reputation: 253

Posted 29 February 2012 - 07:39 PM

function Vector(const X, Y, Z: Double): TVector;
begin
 Result := Vector(X, Y, Z);
end;

It just seemed logical at the time I wrote this Posted Image






We are working on generating results for this topic
PARTNERS