Author Topic: Fun little daily WTF found in my own code  (Read 2808 times)

Offline leeor_net

  • Administrator
  • Hero Member
  • *****
  • Posts: 2352
  • OPHD Lead Developer
    • LairWorks Entertainment
Fun little daily WTF found in my own code
« 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);
« Last Edit: August 11, 2016, 10:43:11 AM by leeor_net »

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4955
Re: Fun little daily WTF found in my own code
« Reply #1 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

Offline leeor_net

  • Administrator
  • Hero Member
  • *****
  • Posts: 2352
  • OPHD Lead Developer
    • LairWorks Entertainment
Re: Fun little daily WTF found in my own code
« Reply #2 on: August 09, 2016, 12:12:22 AM »
You know I thought about that?

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4955
Re: Fun little daily WTF found in my own code
« Reply #3 on: August 11, 2016, 09:20:11 AM »
That's because you're evil.

How are the cookies on the dark side?

Offline leeor_net

  • Administrator
  • Hero Member
  • *****
  • Posts: 2352
  • OPHD Lead Developer
    • LairWorks Entertainment
Re: Fun little daily WTF found in my own code
« Reply #4 on: August 11, 2016, 10:44:00 AM »
Better than on the light side.