#### Archived

This topic is now archived and is closed to further replies.

# How's this for cryptic, uncommented code

This topic is 5802 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I needed a gcd function, so I quickly hacked out this one. Then I looked back at it and was glad I knew how it worked, because it looked pretty arcane for something so simple. What does everybody else think?
int gcd(int x, int y) {
if(x < 0) {
x = -x;
}
if(y < 0) {
y = -y;
}
if(x == 0) {
return y;
}
if(y == 0) {
return x;
}
while(true) {
if(x > y) {
x = x % y;
if(x == 0) {
return y;
}
} else {
y = y % x;
if(y == 0) {
return x;
}
}
}
}

Ok, fixed the divide by zero problem.[/edit] [edited by - jediknight219 on October 31, 2002 11:54:15 PM]

##### Share on other sites
It''s not bad really, pretty easy to follow. I guess it depends on how much nasty code you have had to wade through .

##### Share on other sites
Yeah, that''s not too bad, here''s a few lines of my worst. I really should go back and comment this so I can remember what it does
rotX = animationArray[activeAnimation].frameArray[animationArray[activeAnimation].activeFrame].rotationArray[ID].x + rotX;rotY = animationArray[activeAnimation].frameArray[animationArray[activeAnimation].activeFrame].rotationArray[ID].y + rotY;rotZ = animationArray[activeAnimation].frameArray[animationArray[activeAnimation].activeFrame].rotationArray[ID].z + rotZ;	deltaRotX = rotX - animationArray[activeAnimation].frameArray[animationArray[activeAnimation].activeFrame].rotationArray[ID].x*(1-fraction); deltaRotY = rotY - animationArray[activeAnimation].frameArray[animationArray[activeAnimation].activeFrame].rotationArray[ID].y*(1-fraction) ;deltaRotZ = rotZ - animationArray[activeAnimation].frameArray[animationArray[activeAnimation].activeFrame].rotationArray[ID].z*(1-fraction) ;

________________________
Grab your sword, and get prepared for the SolidSteel RPG engine, coming soon...(i.e. when it''s done)

##### Share on other sites
Too... much... indirection... *dies*

##### Share on other sites
OP: Thats not bad at all. Quite easy to follow. In fact, i would say that code is very readable.

To Cold_steel: Good God

##### Share on other sites
I imagine that the function would be hard to understand if you didn''t know the Eulerian method for computing the GCD. In terms of coding style, it seems fairly good.

Don''t listen to me. I''ve had too much coffee.

##### Share on other sites
Here's an obfuscated Eulerian GCD function for ya :-D

#DEFINE a(x) (((x)>0)?x:-x)      int gcd(int x,    int/* O    O */y){    x=a/*        */(x)    ;y=/* \____/ */abs    (y);while (x*y){if   (x<y)   swap   (x,y);x=x%y;}  return   x?x:y;}

Don't listen to me. I've had too much coffee.

[edited by - sneftel on October 31, 2002 12:52:20 AM]

##### Share on other sites
quote:
Original post by Amnesty
OP: Thats not bad at all. Quite easy to follow. In fact, i would say that code is very readable.

To Cold_steel: Good God

The scary thing is, I've seen much, much worse! I was playing some game, and an error occurred and some debug output popped up with the call that caused the error. Anyway, I have never seen so many ->'s in my life. Honestly there must have been at least 10, any maybe more.

Something->SomethingElse->MoreStuff->anotherThing->YetMore->AndAgain->andAgain->something();

Only worse
I really should just copy the rotation vector and get rid of those horendous 6 lines of code. It's got to be hurting performance Screw it, I'll optimize it later

________________________
Grab your sword, and get prepared for the SolidSteel RPG engine, coming soon...(i.e. when it's done)

[edited by - Cold_Steel on October 31, 2002 12:22:17 AM]

##### Share on other sites
Numerical double integrator
Implementation of properties in standard c++.

And my gcd can beat up your gcd:

  int gcd_of_evil(int a, int b){  while (true) {    if (a > (b << 2))      a %= b;    else      while (a >= b) a -= b;    if (!a) return b;    if (b > (a << 2))      b %= a;    else      while (b >= a) b -= a;    if (!b) return a;  }}

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
16
5. 5

• 9
• 13
• 9
• 33
• 13
• ### Forum Statistics

• Total Topics
632593
• Total Posts
3007278

×