Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
GROVER CURES HOUSE
Aug 26, 2007

Go on...
Do you honestly not see how a loop could replace that massive tangled mess of ifs? I did not say it was a good idea, just that was a trivial one. One that even a newbie could grasp. Not a good idea. A simple one.

Broken Knees Club posted:

Yes? You can collapse all of those ifs into a loop and have it do the same thing. Obviously it's the Wrong Way but it should be obvious even for a total newbie.

As an aside, I also made a tile-based project. It did not have loops. :colbert:

Adbot
ADBOT LOVES YOU

mr_jim
Oct 30, 2006

OUT OF THE DARK

Broken Knees Club posted:

Do you honestly not see how a loop could replace that massive tangled mess of ifs? I did not say it was a good idea, just that was a trivial one. One that even a newbie could grasp. Not a good idea. A simple one.

I get that you could loop over all the rows and columns and test each one to see if the cursor is in it. That's not what's confusing me. What's confusing me is you saying that it's the first thing that should occur to someone reading the code, and then turning around and saying that it's a bad idea, but it would still somehow be simpler. Reducing the number of lines of code does not automatically make the code simpler to reason about, newbie or not.

mr_jim fucked around with this message at 00:22 on Nov 9, 2009

GROVER CURES HOUSE
Aug 26, 2007

Go on...
As I've said, highlighted and underlined, that comment was directed at newbies. People who are not confident enough to do things the Right Way. Do you understand why a newbie may be better off writing a convoluted loop rather than 200 if statements? It's not about reducing the amount of keystrokes per function, its about developing logical approaches to problems and yes, that means writing loops in places where they don't belong.

I don't know if you also missed this bit, but I wasn't suggesting that anyone should use a loop in that case. I merely considered that to be code that screamed "do a loop here" to anyone even slightly familiar with flow control and that merely made it all the more horrifying.

tl;dr: this is not about loops

Fecotourist
Nov 1, 2008

Broken Knees Club posted:

I merely considered that to be code that screamed "do a loop here" to anyone even slightly familiar with flow control but not at all with basic arithmetic.

geetee
Feb 2, 2004

>;[

Broken Knees Club posted:

Do you honestly not see how a loop could replace that massive tangled mess of ifs? I did not say it was a good idea, just that was a trivial one. One that even a newbie could grasp. Not a good idea. A simple one.

You talked poo poo when you were in no place to have done so and now you're back peddling. Let it go and in a few pages you can try again.

Painless
Jan 9, 2005

Turn ons: frogs, small mammals, piles of compost
Turn offs: large birds, pitchforks
See you at the beach!

geetee posted:

You talked poo poo when you were in no place to have done so and now you're back peddling.

Well CoC is a pretty good place for a back peddler, I think a lot of people here have back problems and would like to buy a new one

geetee
Feb 2, 2004

>;[

Painless posted:

Well CoC is a pretty good place for a back peddler, I think a lot of people here have back problems and would like to buy a new one

:derp:

geetee fucked around with this message at 23:43 on May 15, 2011

xarph
Jun 18, 2001


ZorbaTHut posted:

I pray to God that it did not, but honestly it was more blackboxy than that. You just said "optimize for X seconds!" and sometimes it did.

It would not entirely surprise me if, on the inside, it did exactly that.

My current job involves lots of code originating from Japan. I have had this window open for two minutes trying to describe it, but I just can't... :( I think the engineers there are just prohibited from telling their manager that a given methodology is a horrible idea, so you get this code written around fundamentally flawed ideas dictated by someone who hasn't touched a compiler in ten years.

"Optimize for X seconds [in wall time]" fits this style of engineering to a T. Like, a manager probably told someone that they could guarantee that the compile process will be [this] fast, and that was the only place they could think of to put a throttle to get it under the mark.

Edit: My best/worst "what the gently caress are the japanese smoking" story is the program that required LESS than 4GB of physical RAM to run. This is because it used the delay from paging out to swap to throttle a particular thread. I have been mulling it over for a year and still can't figure even a possible reasoning for this behavior to exist. It isn't even unintentional - there's a check to make sure that pages/sec had passed a certain amount before the thread resumes. :pwn:

xarph fucked around with this message at 02:15 on Nov 9, 2009

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"

xarph posted:

Edit: My best/worst "what the gently caress are the japanese smoking" story is the program that required LESS than 4GB of physical RAM to run. This is because it used the delay from paging out to swap to throttle a particular thread. I have been mulling it over for a year and still can't figure even a possible reasoning for this behavior to exist. It isn't even unintentional - there's a check to make sure that pages/sec had passed a certain amount before the thread resumes. :pwn:

Please post more details about insane Japanese software, because this post right here is incredible.

npe
Oct 15, 2004
code:
while (ii < arrayList.size())
{
	ss = arrayList.get(ii);
	jj = ss.indexOf(',');
	if (jj < 0)
	{
		++ii;
		continue;
	}
	cc = ss.charAt(jj + 1);
	serialNo = ss.substring(0, jj);
	tempSerialNo = ss.substring(0, jj);
	sb.append("\"" + serialNo + "\",");
	if (cc == 'A' && serialNo.equals(tempSerialNo))
	{
		sb.append("\"" + ss.substring(jj + 3) + "\",");
		if (ii < arrayList.size() - 1)
		{
			++ii;
			ss = arrayList.get(ii);
			jj = ss.indexOf(',');
			cc = ss.charAt(jj + 1);
			tempSerialNo = ss.substring(0, jj);
		}
	} else
	{
		sb.append("\"\",");
	}
	if (cc == 'B' && serialNo.equals(tempSerialNo))
	{
		sb.append("\"" + ss.substring(jj + 3) + "\",");
		if (ii < arrayList.size() - 1)
		{
			++ii;
			ss = arrayList.get(ii);
			jj = ss.indexOf(',');
			cc = ss.charAt(jj + 1);
			tempSerialNo = ss.substring(0, jj);
		}
	} else
	{
		sb.append("\"\",");
	}
	if (cc == 'C' && serialNo.equals(tempSerialNo))
	{
		sb.append("\"" + ss.substring(jj + 3) + "\",");
		if (ii < arrayList.size() - 1)
		{
			++ii;
			ss = arrayList.get(ii);
			jj = ss.indexOf(',');
			cc = ss.charAt(jj + 1);
			tempSerialNo = ss.substring(0, jj);
		}
	} else
	{
		sb.append("\"\",");
	}

	if (firstPass)
	{
		sb.append("\"D\"\r\n");
		firstPass = false;
	} else
	{
		sb.append("\"\"\r\n");
	}
	++ii;
}
A small sample of what we've been finding lately from one of our more interesting developers.

GROVER CURES HOUSE
Aug 26, 2007

Go on...

geetee posted:

You talked poo poo when you were in no place to have done so and now you're back peddling. Let it go and in a few pages you can try again.

No, really, I'm not. Look at this code again for a second:
code:
tw = 30;// tilewidth
	i = 30;
	if (_xmouse<i+xoffset) {
		who._x = xoffset+(i-tw);
		thiscol = 1;
	}
	// then we're near column 1  
	if (_ymouse<i+yoffset) {
		who._y = yoffset+(i-tw);
		thisrow = 1;
	}
	// then we're near row 2  
	i = 60;
	if (_xmouse<i+xoffset+1 && _xmouse>((i+xoffset)-tw)) {
		who._x = xoffset+(i-tw);
		thiscol = 2;
	}
	// then we're near column 2  
	// and this line equates to: if x < 81 and x > 20.. then go ahead and decide the position.
	if (_ymouse<i+yoffset+1 && _ymouse>((i+yoffset)-tw)) {
		who._y = yoffset+(i-tw);
		thisrow = 2;
	}
	//etc.
I see this as iteration done manually with a ton of ifs. My comment was aimed at the fact that this was manual iteration that could be replaced with a loop. I was not talking about the function of this horrifying code, which doesn't need a loop, but the structure. If you got some other meaning from my posts, then I'm probably a bad poster but I did not backpedal or mean anything other that what I said.

geetee
Feb 2, 2004

>;[
Please god no more. Make it stop.

Incoherence
May 22, 2004

POYO AND TEAR

Broken Knees Club posted:

I see this as iteration done manually with a ton of ifs. My comment was aimed at the fact that this was manual iteration that could be replaced with a loop. I was not talking about the function of this horrifying code, which doesn't need a loop, but the structure. If you got some other meaning from my posts, then I'm probably a bad poster but I did not backpedal or mean anything other that what I said.
A loop would be just as WTF-worthy as that code snippet. That's why people are making GBS threads on you: you're suggesting "optimizing" one WTF into another.

blorpy
Jan 5, 2005

Janin posted:

Please post more details about insane Japanese software, because this post right here is incredible.

Ruby.

Vanadium
Jan 8, 2005

Tell me more. :colbert:

Shavnir
Apr 5, 2005

A MAN'S DREAM CAN NEVER DIE

Incoherence posted:

A loop would be just as WTF-worthy as that code snippet. That's why people are making GBS threads on you: you're suggesting "optimizing" one WTF into another.

For what its worth I think what he's trying to say is a loop would be slightly less wtf worthy than writing 500 if statements despite still being wrong.

But that's me being a devil's advocate.

ErIog
Jul 11, 2001

:nsacloud:

Shavnir posted:

For what its worth I think what he's trying to say is a loop would be slightly less wtf worthy than writing 500 if statements despite still being wrong.

But that's me being a devil's advocate.

I understood it this way too. I'm chalking the ensuing clusterfuck up to people wanting to feel smart, and not reading what he actually wrote.

floWenoL
Oct 23, 2002

ErIog posted:

I understood it this way too. I'm chalking the ensuing clusterfuck up to people wanting to feel smart, and not reading what he actually wrote.

God forbid anyone point out coding horrors in the coding horror thread!

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

floWenoL posted:

God forbid anyone point out coding horrors in the coding horror thread!

He was pointing out a human horror, that goes in the other thread. The one on page twelve or whatever.

raminasi
Jan 25, 2005

a last drink with no ice
code:
private void IncreaseSubBoundaryNo()
{
        int iNo = int.Parse(sSubBoundaryNo);
        iNo++;
        sSubBoundaryNo = iNo.ToString();
}
Now, to be fair, this guy isn't a programmer and was forced to cobble stuff together because nobody else could even try, but I'm still completely unable to come up with a situation that would cause anyone, even a complete novice, to do this.

weaaddar
Jul 17, 2004
HAY GUYS WHAT IS TEH INTERWEBNET, AND ISN'T A0L the SECKZ!? :LOL: 1337
PS I'M A FUCKING LOSER
I just figure you guys might get a laugh out of this. So Jasshelper developed by Vexorian is a compiler that adds extra features to the Jass language. It basically translates your more complicated code into Jass.

He created a C like dialect called Zinc and one of its features is anonymous methods. As some natives, and some libraries use function callbacks, and sometimes you want to do something very simple.
e.g.
php:
<?
// an example of sane usage.
...
forGroup(g,function()
{
     BJDebugMsg(GetUnitName(GetEnumUnit()));
});
?>
However, that certainly doesn't belong in a thread like this. That previous example is actually useful and makes sense as it is.
I decided to implement the Y combinator in anonymous methods. Now they don't support closures [yet], so I had to hack it together by using a hashtable but here goes.
php:
<?
library anony
{
    hashtable closure = InitHashtable();
    type func_int_int extends function(integer)->integer;
    type Ucombinator extends function(Ucombinator)->func_int_int;
    type Yarg extends function(func_int_int)->func_int_int;
    type Ycombinator extends function(Yarg)->func_int_int;
    
    function U(Ucombinator f)->func_int_int
    {
        return f.evaluate(f);
    }
    function test()
    {
        
        //return U(r => a => f(r(r))(a));
        Ycombinator Y = function(Yarg f)-> func_int_int
        {
            SaveInteger(closure,1,0,integer(f));
            return U(function(Ucombinator r)-> func_int_int
            {
                SaveInteger(closure,1,1,integer(r));
                return function(integer a)->integer
                {
                    Yarg f =LoadInteger(closure,1,0);
                    Ucombinator r = LoadInteger(closure,1,1);
                    return f.evaluate(U(r)).evaluate(a);
                };
            });
        };
        
        
        
        func_int_int factorial = Y.evaluate(function(Yarg f)->func_int_int
        {
            SaveInteger(closure,0,0,integer(f));
            return function(integer n)->integer
            {
                func_int_int f = LoadInteger(closure,0,0);
                if(n == 0) return 1;
                return n*f.evaluate(n-1);
            };
        });
        BJDebugMsg(I2S(factorial.evaluate(5)));
    }
    
    function onInit()
    {
        test();
    }
}
?>
Prints 120 as you'd expect.

Kelson
Jan 23, 2005

Shavnir posted:

For what its worth I think what he's trying to say is a loop would be slightly less wtf worthy than writing 500 if statements despite still being wrong.

I read it the same way... that manual setting of i = <next value> looks like an unrolled loop. Ignoring the underlying algorithm, it screams "loop da loop"

shrughes
Oct 11, 2008

(call/cc call/cc)

weaaddar posted:

I just figure you guys might get a laugh out of this.

It's just a Y combinator, a useful utility function found in any real programming language. Where's the horror in that?

nbv4
Aug 21, 2002

by Duchess Gummybuns

ErIog posted:

I understood it this way too. I'm chalking the ensuing clusterfuck up to people wanting to feel smart, and not reading what he actually wrote.

i.e this entire thread

Bozart
Oct 28, 2006

Give me the finger.
The worst coding horror I've experienced is working with someone so good that you know you'll never be that good, and that someday you'll be working somewhere that absolutely everyone (including me) will be terrible.
:eng99:

weaaddar
Jul 17, 2004
HAY GUYS WHAT IS TEH INTERWEBNET, AND ISN'T A0L the SECKZ!? :LOL: 1337
PS I'M A FUCKING LOSER

shrughes posted:

It's just a Y combinator, a useful utility function found in any real programming language. Where's the horror in that?

Jass (the warcraft 3 scripting language) doesn't actually have function types, the actual produced code is probably some hideous monstrosity that looks nothing like the code I typed. Its just a complete abuse of syntactic sugar.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

weaaddar posted:

Jass (the warcraft 3 scripting language) doesn't actually have function types

Neither does machine code really, but that doesn't affect most people's opinion of the Y combinator in other languages.

blorpy
Jan 5, 2005

Vanadium posted:

Tell me more. :colbert:

Well you see, Ruby was written by a chinaman, and they have a poor ability to write code.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Markov Chain Chomp posted:

Well you see, Ruby was written by a chinaman, and they have a poor ability to write code.

At least Ruby's not a functional language, because it would be unable to distinguish between foldl and foldr!! :xd:

ton1c
Apr 30, 2005

Bozart posted:

The worst coding horror I've experienced is working with someone so good that you know you'll never be that good, and that someday you'll be working somewhere that absolutely everyone (including me) will be terrible.
:eng99:

I think the idea here is that you accept that there will always be those people who are totally insane. As long as you keep educating yourself then you're better than 90% of the dudes out there, and all you have to do is stay up to date, and keep trying to improve.

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
Whelp, I found four instances of gets() yesterday. I think I've hit the bottom of my codebase's barrel. Similarly, I found two of these:
code:
char buffer[BLAH];
input >> buffer;
And one of these:
code:
// thing.h
int things[4];

// thing.cc
case BLAH:
    things[4] = blah;
    things[5] = blah;
    break;
case BLAH_BLAH:
    things[6] = blah;
    things[7] = blah;
    break;
So maybe there's a barrel underneath this barrel.

BigRedDot
Mar 6, 2008

Mustach posted:

So maybe there's a barrel underneath this barrel.
I'm sorry no one told you before now, but it's barrels all the way down.

Mikey-San
Nov 3, 2005

I'm Edith Head!
Maybe there's a smaller barrel inside.

ZorbaTHut
May 5, 2005

wake me when the world is saved

Mikey-San posted:

Maybe there's a smaller barrel inside.

If there's one thing I've learned about bad code, it's that the inner barrel is invariably larger.

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"
My co-workers love the "delete one element at a time, clear after" pattern; I've found (and removed) no less than 10 versions of it. Several were written after I'd removed a few which were causing crashes. Every time I remove one, whoever wrote it is all "oh I never thought that could crash!". Somewhere there's an rear end in a top hat professor wondering how low he can drop standards before being fired, and every one of his students is working with me.

A sample:
code:
// every leaf in a tree has a pointer to its parent. why? gently caress if I know
SomeClass::SomeClass(SomeClass* parent2) {
  parent = parent2; // initialization lists = for fags
}


SomeClass::~SomeClass() {
  // hmm yes, app logic in a destructor, what can go wrong
  parent->UnregisterChild(this);


  vector<SomeClass*>::iterator itr;
  for (itr = children.begin(); itr != children.end(); itr++) {
    if (*itr) // "how does delete work again?"
      delete *itr; // "what's a dangling pointer?"
  }
  children.clear();
}

void SomeClass::UnregisterChild(SomeClass* child)
    throw () { // yeah, sure, why not
  vector<SomeClass*>::iterator itr;
  for (itr = children.begin(); itr != children.end(); itr++) {
    itr->UnregisterSibling(child);
  }
}

void SomeClass::UnregisterSibling(SomeClass* sibling)
    throw (SomeException) { // oh that's why not
  // yada yada
}

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Janin posted:

code:
delete *itr; // "what's a dangling pointer?"

You do realize this is not only legit, but required to avoid a leak? (Unless there's some other behavior in UnregisterChild that breaks things).

Avenging Dentist fucked around with this message at 04:24 on Nov 11, 2009

BattleMaster
Aug 14, 2000

Yeah maybe I'm missing something but isn't it good practice to check if a pointer is null or not before you try to delete or free() it?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

BattleMaster posted:

Yeah maybe I'm missing something but isn't it good practice to check if a pointer is null or not before you try to delete or free() it?

No because delete checks for null before it does anything.

BattleMaster
Aug 14, 2000

Avenging Dentist posted:

No because delete checks for null before it does anything.

Oh I didn't know that because I'm a C person. Cool.

Adbot
ADBOT LOVES YOU

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

BattleMaster posted:

Oh I didn't know that because I'm a C person. Cool.

I've got bad news for you.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply