Advertisement

Dumbest coding mistakes ever

Started by February 07, 2012 01:19 AM
53 comments, last by Bacterius 12 years, 8 months ago

I don't remember exactly where I did this, but I did manage to pull this one off once:
int& derp = derp;

It annoys me that this is even allowed in the language.

These are my "dumbest" moments, in C++ at least:

struct Foo
{
Foo(float u, float v) : u(u), v(u)
{
}

float u;
float v;
};


A related version:


struct Bar
{
Bar(float someVariableX, float someVairableY)
:
someVariableX(someVariableX),
someVariableY(someVariableY)
{
}

float someVariableX;
float someVariableY;
};



lotsAndLotsAndLotsOfCode();
for(int i = 0 ; i < N ; +i) {
lotsAndLotsAndLotsOfCode();
frobnicate(i);
lotsAndLotsAndLotsOfCode();
}
lotsAndLotsAndLotsOfCode();

The common problem was dismissing the problematic code as being too trivial to contain a bug, and spending lots of time debugging the calling/called code looking for errors. One gets too used to reading what one expects to be the case, rather than reading what is actually there.
I do this every once in a while. Luckily I've gotten pretty quick at catching it in the debugger, but that's only because I've gotten practice:

for (int i = someStartValue; i > 0; ++i)
{
// code
}


I know I make plenty of others, but I'll have to try and remember them...
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Advertisement
I guess I am just a bizarre abberation. I write all my code with every line on the left edge

thing=0;
otherthing=10;
while(thing<otherthing) {
stuff;
stuff;
thing++;
}
more;
code;
all;
on;
the;
left;


spacing things out generally confuses the hell out of me too.

I guess I am just a bizarre abberation. I write all my code with every line on the left edge

thing=0;
otherthing=10;
while(thing<otherthing) {
stuff;
stuff;
thing++;
}
more;
code;
all;
on;
the;
left;


spacing things out generally confuses the hell out of me too.

Yup, that's pretty weird. But there's no mistakes in teh codes....
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
I was writing a Windows service and needed to change the privileges of the process token.

SetPrivileges("SE_RESTORE_NAME\0SE_BACKUP_NAME\0SE_TCB_NAME\0\0");

The function would split up the string and LookupPrivilegeValue() for each privilege, building up the structure to pass to AdjustTokenPrivileges().
This worked in the previous version of Windows I was using it (I don't remember if it was XP or Vista), but in Windows 7 it didn't; LookupPrivilegeValue() was failing.

Couldn't figure it out until I looked at WinNT.h:
#define SE_RESTORE_NAME TEXT("SeRestorePrivilege")

The fix, obviously, is

SetPrivileges(SE_RESTORE_NAME "\0" SE_BACKUP_NAME "\0" SE_TCB_NAME "\0\0");

What I still don't fully understand is why it worked in older Windows 7.
"But who prays for Satan? Who, in eighteen centuries, has had the common humanity to pray for the one sinner that needed it most?" --Mark Twain

~~~~~~~~~~~~~~~Looking for a high-performance, easy to use, and lightweight math library? http://www.cmldev.net/ (note: I'm not associated with that project; just a user)

while( someCondition );
{
}
Advertisement
I often flip the direction of boolean tests. Like it should be

if (foo) ...

and I'll write

if (!foo) ...

These can be hard things to catch, because you can stare at the bad comparison but your brain just kind of assumes that the call knows what it is doing. :)

[quote name='speciesUnknown' timestamp='1328628754' post='4910526']
This is why I usually have a policy of not using control structures without a block, but sometimes I relapse. It would be good to have a plugin or something that could detect this.

I think the standard we're working with now allows single line if statements like if(a<b) a=b, but all other control structures require blocks.

Personally, I always use blocks, so that's why I'm not positive about the above in our coding standard. I only ever don't use them with the ternary operator, and I only use the ternary operator when I'm conditionally initializing stuff because it's harder to visually parse and some people have no idea what it is.
[/quote]

Having your own standards is a partial solution; but it breaks down when you have to use some random persons code, and you are back to wishing the language enforced a standard.
Trying to abuse special constructors instead of delegating the work to functions .


class Map2D
{
Map2D() { makeGenericMap(); }
Map2D( int width, int height, int cell_size, int layers )
{
makeMap(width, height, cell_size, layers);
}
...
}
...
class SpecificMap : public Map2D
{
SpecificMap() : Map2D()
{
MapParser parser("filename");

if( parser->isGood() )
{
Map2D::Map2D(parser->getWidth(), parser->getHeight(), ... ); // Nothing would ever happen

....

// More stuff

}
}
}



I thought it'd be the same as calling any other super-function at the time. Don't know what in the world I was thinking. Spent a good bit of time tracing it down because "I thought it'd just work like that."
I'm that imaginary number in the parabola of life.

I often flip the direction of boolean tests. Like it should be

if (foo) ...

and I'll write

if (!foo) ...

These can be hard things to catch, because you can stare at the bad comparison but your brain just kind of assumes that the call knows what it is doing. smile.png


The worst part is when the boolean variable name adds to the logic.

if (notRunning) {
notRunning = isRunning();
}

This topic is closed to new replies.

Advertisement