Author Topic: Horrifying lines of code  (Read 13257 times)

0 Members and 1 Guest are viewing this topic.

Offline karajorma

  • King Louie - Jungle VIP
  • Administrator
  • 214
    • Karajorma's Freespace FAQ
Re: Horrifying lines of code
Does that mean I've got permission to start adding the stuff from the patch in the Diaspora internal to trunk then?
Karajorma's Freespace FAQ. It's almost like asking me yourself.

[ Diaspora ] - [ Seeds Of Rebellion ] - [ Mind Games ]

 

Offline WMCoolmon

  • Purveyor of space crack
  • 213
Re: Horrifying lines of code
Check your PMs. :)
-C

 

Offline Flipside

  • əp!sd!l£
  • 212
Re: Horrifying lines of code
This was a rather interesting approach to checking four points surrounding a position on a grid as part of an A* Pathfinding setup...

Code: [Select]
        for (yPos = -1; yPos >1; yPos = yPos + 2)
        {
                    for (xPos = -1; xPos >1; xPos = xPos = xPos+2)
                    {
                        xPosB = (xPos + (path[pointer].getXValue()));
                        yPosB = (yPos + (path[pointer].getXValue()));
                        current = new XYValue(xPosB, 0);
                        if (roads.getByLocation(current) != 0)
                        {
                            options.add(current);
                        }
                        current = new XYValue(0,yPosB);
                        if (roads.getByLocation(current) != 0)
                        {
                            options.add(current);
                        }
                    }
        }

From that point onwards, I decided to do it the old-fashioned way ;)

 

Offline blackhole

  • Still not over the rainbow
  • 29
  • Destiny can suck it
    • Black Sphere Studios
Re: Horrifying lines of code
Never, ever, ever convert an unsigned long to a double and then back again if you have 4294967295 assigned to it.

 

Offline lx

  • 22
Re: Horrifying lines of code
a few years ago i tried to write a class which simulates an integer, except that this integer could handle much larger values than a normal 4-byte integer.
naturally i had to implement all the basic arithmetic functions (+,-,*,/)....
a few months ago i found my code again and wanted to see how i solved this stuff... and i could figure out everything except my division function (which works correct and surprisingly(!) fast)... but i have no f*king clue how it works and what i did there *g*
also it seems that at that time i thought it's obvious how it works and just refused to comment it .... d'oh.

maybe it was a result of the Ballmer Peak.. i don't know

so, here it is, that ugly beast ... :

Code: [Select]
BigInt& BigInt::operator/=(BigInt divisor)
{
const BigInt biNull("0");
if (divisor==biNull)
throw exception("division through zero");
   
if (/*this*/isLess/*than*/(divisor)) {
// result would be 0.xxxxx
num_.clear();
num_.push_back('0');
return *this;
}

cVec result;
vsize div_len = divisor.length();

while (divisor<=*this)
divisor << 1; // fast divisor*10
divisor >> 1;

unsigned i=1, k=0, l=0;
while (true) {
while (divisor>*this) {
if (*this==biNull || (i>=2 && l==k)) {
result.insert(result.begin(), '0');
i=1;
}
divisor >> 1; // fast divisor/10
++i;
l=k;
}
++k;
if (divisor.length() < div_len)
break;

unsigned j=0;
for (; divisor<=*this; ++j)
*this -= divisor;

result.insert(result.begin(), j+'0');
}
if (result.size()>1 && *result.begin() == '0')
result.erase(result.begin());

num_.swap(result);

return *this;
}

maybe i will give it a second look now.. with a pen and a paper ;)
« Last Edit: March 10, 2009, 04:09:49 am by lx »

 

Offline lx

  • 22
Re: Horrifying lines of code
Browsing through the source files of my current project I found this brilliant piece code.

Converting a number to a string, the easy way:

Code: [Select]
string ObjectManager::CreateStringFromNumber(unsigned int value, unsigned int digit)
{
   #if (!((('9' - 9) == '0') && (('8' - 8)8) == '0') && (('7' - 7) == '0') && (('6' - 6) == '0') && (('5' - 5) == '0') && (('4' - 4) == '0') && (('3' - 3) == '0') && (('2' - 2) == '0') && (('1' - 1) == '0')))
   Logger log("CreateStringFromNumber");
   log.WriteLine(RootLogger::ERROR, "Failed to convert from int to char");
   throw "Error";
   #endif


   string result(digits, '0');
   int tmp = 1;

   for (unsigned int i = 1; i < digits; ++i)
   {
   tmp *= 10;
   }

   for (string::iterator iter = result.begin(); iter != result.end(); ++iter)
   {
   (*iter) = static_cast<char>((value/tmp)+'0');
   value %= tmp;
   tmp /= 10;
   }
   return result;
}

i guess sprintf just doesn't cut it any more, right uchuujin ;)
« Last Edit: April 02, 2009, 02:50:59 pm by lx »

 
Re: Horrifying lines of code
get lost :>

 

Offline blackhole

  • Still not over the rainbow
  • 29
  • Destiny can suck it
    • Black Sphere Studios
Re: Horrifying lines of code
Code: [Select]
void cUsable::Use();
Now my code really is on drugs.

 
Re: Horrifying lines of code
Thought I'd post one from within the SCP itself.
/analyse gives the rather tame
Code: [Select]
3>c:\users\james\documents\visual studio 2008\projects\freespace2\code\graphics\generic.cpp(36) : warning C6054: String 'argument 1' might not be zero-terminated: Lines: 32, 35, 36

The code that caused this warning?
Code: [Select]
ga->filename[strlen(ga->filename)] = '\0';
STRONGTEA. Why can't the x86 be sane?

 

Offline Aardwolf

  • 211
  • Posts: 16,384
    • Minecraft
Re: Horrifying lines of code
portej05:

That line makes no sense... strlen(ga->filename) won't work right if it isn't null-terminated, but assuming it doesn't crash from an access violation, the return value will have '\0' there already...

 :wtf:

 
Re: Horrifying lines of code
Aardwolf: I know :P Hence why it is here :D
STRONGTEA. Why can't the x86 be sane?

 

Offline taylor

  • Super SCP/Linux Guru
  • 212
    • http://www.icculus.org/~taylor
Re: Horrifying lines of code
That is just beyond wrong.  And it has gotten much worse after seeing it since it appears that was part of one of my commits. :shaking:

 
Re: Horrifying lines of code
Just found a pile of these:
orienteditor.cpp Line 220:
Code: [Select]
m_spin1.SetRange((short)99999, (short)-99999);

For those who aren't sure what's going on, the maximum value of a signed short is 32767
« Last Edit: May 22, 2009, 02:19:31 am by portej05 »
STRONGTEA. Why can't the x86 be sane?

 
Re: Horrifying lines of code
Some exercises in unintentional obfuscation from my game dev library.

Code: [Select]
if (o.x < cx - allow) {
safePush(c - 1, r, o);
}
if (o.x > cx+allow) {
safePush(c + 1, r, o);
}
if (o.y < cy - allow) {
if (o.x < cx - allow) {
safePush(c - 1, r - 1, o);
}
if (o.x > cx+allow) {
safePush(c + 1, r - 1, o);
}
safePush(c, r - 1, o);
}
if (o.y > cy + allow) {
if (o.x < cx - allow) {
safePush(c - 1, r + 1, o);
}
if (o.x > cx+allow) {
safePush(c + 1, r + 1, o);
}
safePush(c, r + 1, o);
}

safePush(c, r, o);

Code: [Select]
for (var c:int = 0; c < density; c++) {
for (var r:int = 0; r < density; r++) {
for (i = 0; i < grid[c][r].length; i++) {
var a:Entity = grid[c][r][i];
for (var j:int = i + 1; j < grid[c][r].length; j++) {
if (!a.checkDupeColl(grid[c][r][j]) && !grid[c][r][j].checkDupeColl(a)) {
a.collided.push(grid[c][r][j]);
grid[c][r][j].collided.push(a);
if (a.collisions.willHit(grid[c][r][j].collisions.type)) {
a.entityCollisionSolve(grid[c][r][j], true);
}
}
}
}
}
}

 

Offline Mongoose

  • Rikki-Tikki-Tavi
  • Global Moderator
  • 212
  • This brain for rent.
    • Minecraft
    • Steam
    • Something
Re: Horrifying lines of code
That is some fantastic use of whitespace and descriptive variable names.

 
Re: Horrifying lines of code
It made sense at the time.

Actually, and sadly, it still makes sense now.

The whitespace is because flashdevelop indents in tabs, and this was already tabbed in a great deal due to class/package/other functions.

 

Offline Sushi

  • Art Critic
  • 211
Re: Horrifying lines of code
It made sense at the time.

Actually, and sadly, it still makes sense now.

The whitespace is because flashdevelop indents in tabs, and this was already tabbed in a great deal due to class/package/other functions.

The problem isn't the tabs: the problem is having a tab spacing of 8. :) Cut that to 4 (or 2, if you prefer) and you'll be a much happier chipmunk.

 
Re: Horrifying lines of code
But... I like it like this ):

 

Offline castor

  • 29
    • http://www.ffighters.co.uk./home/
Re: Horrifying lines of code
Code: [Select]
if (o.x < cx - allow) {
safePush(c - 1, r, o);
}
if (o.x > cx+allow) {
safePush(c + 1, r, o);
}
Doh! quite generous spacing there. Well, at least it motivates you to keep the actual code lines short.
« Last Edit: June 13, 2009, 04:30:46 am by castor »

 

Offline kode

  • The Swedish Chef
  • 28
  • The Swede
    • http://theswe.de
Re: Horrifying lines of code
It made sense at the time.

Actually, and sadly, it still makes sense now.

The whitespace is because flashdevelop indents in tabs, and this was already tabbed in a great deal due to class/package/other functions.

The problem isn't the tabs: the problem is having a tab spacing of 8. :) Cut that to 4 (or 2, if you prefer) and you'll be a much happier chipmunk.

The whole point in using tabs (I personally don't, but ...) is that you, the programmer, can choose where your tab stops are. You want a tab spacing of 4? (setq tab-width 4) :)

So I say, let him use tabs if he wants. It's not like it's hard to "fix" it
Pray, v. To ask that the laws of the universe be annulled in behalf of a single petitioner confessedly unworthy.
- Ambrose Bierce
<Redfang> You're almost like Stryke 9 or an0n
"Facts do not cease to exist because they are ignored."
- Aldous Huxley
WAR IS PEACE
FREEDOM IS SLAVERY
IGNORANCE IS STRENGTH