Outpost Universe Forums

Projects & Development => Projects => OutpostHD => Topic started by: leeor_net on August 07, 2016, 11:23:43 AM

Title: Fun little daily WTF found in my own code
Post by: leeor_net on August 07, 2016, 11:23:43 AM
For the developers out there, here's a fun little snippet I found while coming up with a better solution to create formatted strings:

Code: [Select]
str_scratch.str("");
str_scratch << TileIndexTranslation[mTile->index()];
r.drawText(font(), str_scratch.str(), rect().x() + 5 + mBold.width("Terrain: "), rect().y() + 75, 0, 0, 0);
r.drawText(mBold, "Terrain:", rect().x() + 5, rect().y() + 75, 0, 0, 0);

str_scratch is a std::stringstream.

TileIndexTranslation is a std::map that uses an int as a key and returns a std::string as a value (std::map<int, std::string>).

Yeah. It's truly that bad.

I used a stringstream to create a formatted string using a string.  :-X  :-\ :-[ :-[ :-[

Genuinely belongs on TheDailyWTF.

Corrected code looks like this:

Code: [Select]
r.drawText(mBold, "Terrain:", rect().x() + 5, rect().y() + 75, 0, 0, 0);
r.drawText(font(), TileIndexTranslation[mTile->index()], rect().x() + 5 + mBold.width("Terrain: "), rect().y() + 75, 0, 0, 0);
Title: Re: Fun little daily WTF found in my own code
Post by: Hooman on August 08, 2016, 03:16:43 PM
Rather than fix it, why not throw a loop around it, build the same string 1000+ times, and see how long it takes for someone else to find it? :P
Title: Re: Fun little daily WTF found in my own code
Post by: leeor_net on August 09, 2016, 12:12:22 AM
You know I thought about that?
Title: Re: Fun little daily WTF found in my own code
Post by: Hooman on August 11, 2016, 09:20:11 AM
That's because you're evil.

How are the cookies on the dark side?
Title: Re: Fun little daily WTF found in my own code
Post by: leeor_net on August 11, 2016, 10:44:00 AM
Better than on the light side.