Friday, April 20, 2007

Revision Comments

Where I work we have a standard header comment that is a revision block.
In this block we record dates, tracking numbers, person responsible for the revision and a comment.

We even have an entry for the creation of a file.

The other day I ran across this entry.


//---------------------------------------------------
// Revision Author
// ------------ -------------------------------------
// 01/01/2005 Nessie our underwater ally - Creation


And some more...

// Chuck Norris's Mom(also created the roundhouse kick) - Creation

// Knight Rider (and Kitt helped of course) - Creation

// TDe... - like Creation and stuff...

// Beltashazar the Amalekite - Creation

// "Doc Oc" - Creation

// TDe... - Creation - this one goes out to the one i love...FIRE!!

// Voltron - Creation

// Jabba the Hutt - Creation

// Chuck Norris - Creation

// Lloyd Christmas - Creation

// Zoltar and the Galactic Whosamawhatzit - Creation

// Space Ghost - Creation

// Bildad the Shoehite - Creation

// Ross Perot - Creation

// Lion King - Creation

// Napoleon Dynamite - Creation

// Rocky Balboa - Creation

// My Mom - Creation

// My Wife - Creation [this programmer is not married]

// Jalfrabby Chicken - Creation

// The Flying Nun - Creation

// Mighty Mouse - Creation

// Condoliza Rice - Creation

// Krohn the Stalagtite - Creation

// Papa Bear - Creation

// Captain Stooby - Creation

// Green Lantern - Creation

// Loose Cannon - Creation

// Simon the Sorcerer - Creation

// [name withheld] - Creation - I am an artist and I write pretty code...

// Caleb's remote control tarantula - Creation

// Starbuck from the Quasar System - Creation

// Stardate 10 Captain Kirk - Creation

// Tom and Terry from "The Great Race" - Creation

// Mr. Klondike Bar - Creation

// Your Mom? - Creation

// Matza Cracker - Creation

// Hogan's Hero's - Creation

// Space Alien? - Creation

// Marvin the Martian - Creation

// Swygard Burrito - Creation

// Johnny Quest - Creation

// [another programmer's] mother in law - Creation

// Sargent Bilko (and his sidekick [another programmer]) - Creation

// My Mom on Tuesdays - Creation

// CAPTAIN Jack Sparrow - Creation

// Mr. Gibbs - Creation

// Miss Elizabeth - Creation

// Mikael Gorbechev - Creation


The funny thing is that almost none of these are duplicated. The several that are: the mom ones, the Jalfrabby chicken ('cause it's so tasty) and Chuck Norris (because if we didn't, he'd come round-house kick us).

Thursday, October 26, 2006

Today I am the perp. Today the mighty have fallen.

Or at least I have fallen - I'm not mighty, I'm wee.

So I'm trying to figure out why, in my C++ program, I'm getting an access violation.

I mean, c'mon! I know I write, if not perfect, then at least pretty darn good code, and I've really been paying attention to this particular unit and I know there are no mistakes.

Yet, there's still a pesky A/V. BAD!

Step through the code. Ok, now we're getting some place.
Well, that's weird, that's where I thought I would get an error.
Hmm... keep going.
Uh huh, right, ok, yeah...
DOH!
TDateObject* __fastcall UserData::FindDate( TDate _Date )
{
TDateObject* Result = 0;

for ( int ix = 0; ix < Count; ix++ )
{
if ( Items[ix]->Date == _Date )
{
Result = Items[ix];
break;
}
}

if ( Result == 0 )
{
Result = new TDateObject();
TObjectList::Add( Result );
}
}

Even though the moral of today's story should obviously be obvious, it obviously wasn't obvious enough for me to obviously catch it whilst obviously programming it.

Moral:
If you're going to use a function to create an object (or any memory for that matter), you'd better make darn tootin' sure you're actually returning something.

I'm a moron.
That is all.

Monday, September 25, 2006

Double Negatives

This beauty was submitted by the amazing and fast rEx.

The culprit was none other than fellow CSOD hunter-gatherer* J-Ra.
if ( IsBadSSN() == false )
{
Result = stBadPersonSSN;
}

Without knowing what IsBadSSN does, one would assume (based on what it sounds like it would do) that this is an error any person with a basic understanding of the English language should be able to catch.

Moral of the story?

Double Negatives in if statements are hard to read.

This would have been better written
if ( ValidSSN() == false )
{
Result = stBadPersonSSN;
}

* hunting and gathering CSOD's for your enjoyment.

Friday, September 22, 2006

And I, I Took the Return Path Less Traveled By

And that has made all the difference.

When you get into the mind-set that multiple return-paths are ok you run risks.

Pre-Conditions are usually ok, and not a horrible idea, unless implemented part-way through the function.

When you get into the mind-set that multiple return-paths are ok, you run the risk of writing (and possibly maintaining) code that does the following. And if you're especially unlucky, (like the original programmer) your code is being used in a Windows service where people expect things to run for months between restarts.

bool TSomeClass::SomeFunction()
{
long RECORD_ID = GetRecordID();

if( RECORD_ID )
{
THandlerClass *Handler = new THandlerClass();

Handler->LoadRecord( RECORD_ID );

if( Handler->SomeBoolFlag == false )
{
ReportError( "Some Bool Flag is false - can't continue" );
return false;
}

Handler->Process();

delete Handler;

return true;
}
else
{
ReportError( "Record ID is not set - unable to process." );
}

return false;
}



Moral of the story?

Pre-Conditions should be before any memory allocation.

Also, if possible, create the memory in the constructor of a class and destroy it in the destructor - this way you don't have to worry about leaking memory inside functions, and unless you have a situation I ran into the other day where the destructor wasn't being called (the subject of a future posting) the memory will be released when the host object is destroyed.

Thursday, September 21, 2006

Comments are cool, Comments are fun...

Comments make their food from the rays of the sun.

Coding is more fun with people who are willing to leave comments in the code like this one:
// to all evildoers, this SQL statement
// must accurately reflect whether or
// not there is a DB connection.
// if it is ever trying to access a table
// that doesn't exist, it will always
// return false. do not change unless
// you know what you are doing.


This was in a function where I was getting an "access in invalid memory" error from Borland's CodeGuard.

This was due to the fact that this base dll is used in applications that don't create the base database object because they don't operate on the database at all.

A simple
if ( bool( Object ) )
{
// logic here to determine if the database is connected
}
else
{
// logic here to handle the lack of database functionality
}

would have helped.

Moral of the story?
Never assume you have a valid instance of a global variable... especially in a statically linked dll that relies on the global variable being created in the main application.

Wednesday, September 20, 2006

I love virtual functions

Pure Virtual functions are even better - allowing you to force a child class to implement a particular function.

Imagine my surprise when I found this brand new type of pure virtual function:
long TSomeClass::SomeFunction()
{
// This is pure virtual
return 0;
}

The header of this class was even correct.
    virtual long SomeFunction() = 0;

Now, you may laugh at me - at my nerdish geekiness, but when I saw this, I started laughing out loud and had to print it off and show the other programmers in the office.

In fact, it was this snippet that caused me to start this blog.

So, there you go... let's laugh at this code, but most of all, let's laugh at the original programmer... you know who you are.