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
The Real Ambassador
Apr 9, 2005

I'll explain and make it plain, I represent the human race.

rjmccall posted:

Curly braces inside parentheses are how you get functional composition of expressions in C.

Let's take it to the next level with lambdas:

code:
#define lambda(return_type, body_and_args) \
  ({                                       \
    return_type __fn__ body_and_args       \
      __fn__;                              \
  })

Adbot
ADBOT LOVES YOU

Bonfire Lit
Jul 9, 2008

If you're one of the sinners who caused this please unfriend me now.

Sorry about the hungarian
code:
bool NetworkGame::JoinClient(ClientInfo &CInfo, NetworkConnection *pConn, const char *szError)
{
 	if (!pConn) return false;
 	if (!fJoinAllowed) { szError = "join refused"; return false; }
...
"But it's a pointer!" :downs:

Smugdog Millionaire
Sep 14, 2002

8) Blame Icefrog
code:
public override void Error(string message)
{
	throw new NotImplementedException();
}

public override void Notify(string message)
{
	throw new NotImplementedException();
}
Discovered by a coworker on production. It sure does Notify you.

Orzo
Sep 3, 2004

IT! IT is confusing! Say your goddamn pronouns!
It's possible that someone just started creating a class that implements some interface (error / notify) but forgot to finish it, and hopefully forgot to even hook it up to anything. Not really that unusual or horrible.

Orzo fucked around with this message at 23:26 on Nov 22, 2010

Smugdog Millionaire
Sep 14, 2002

8) Blame Icefrog

Orzo posted:

It's possible that someone just started creating a class that implements some interface (error / notify) but forgot to finish it, and hopefully forgot to even hook it up to anything. Not really that unusually or horrible.

Of course it was hooked up to stuff :) It's code that was pushed to a production server and was found by the client trying to use their site.

Orzo
Sep 3, 2004

IT! IT is confusing! Say your goddamn pronouns!
Oh. That is horrible then.

Cabbages and VHS
Aug 25, 2004

Listen, I've been around a bit, you know, and I thought I'd seen some creepy things go on in the movie business, but I really have to say this is the most disgusting thing that's ever happened to me.
the last place I worked, a vendor delivered a webapp which used a process with root rights to read a cookie with a raw python Eval(). the developer who discovered this was the case, used it to send out a department-wide email alerting everyone to the awesome 'feature' of the app.

this was a six-figure application. as delivered, it could handle one request per second per server. it was entirely re-written in-house, in less time.

npe
Oct 15, 2004
code:
        static bool IsUtf16BE(byte[] data)
        {
            if (data.Length < 7)
                return false;
            if ((data[0] == 0) && (data[2] == 0) && (data[4] == 0) && (data[6] == 0))
                return true;
            return false;
        }

        static bool IsUtf16LE(byte[] data)
        {
            if (data.Length < 8)
                return false;
            if ((data[1] == 0) && (data[3] == 0) && (data[5] == 0) && (data[7] == 0))
                return true;
            return false;
        }
:allears:

POKEMAN SAM
Jul 8, 2004

npe posted:

:allears:

I love how one of them has data.Length < 7, and one has data.Length < 8. Obviously this is correct, but it means that there was a bit of attention paid to the functions...

Kelson
Jan 23, 2005

Ugg boots posted:

I love how one of them has data.Length < 7, and one has data.Length < 8. Obviously this is correct, but it means that there was a bit of attention paid to the functions...
You're right there was little attention paid; they got that part wrong...

Hot Dog Day #55
May 22, 2003

The noblest of all dogs is the hot-dog; it feeds the hand that bites it.
My boss woke up the other morning and decided we'd be writing all future code in Python 2.4. We develop high speed real time systems and we're all C++ developers.

Dooey
Jun 30, 2009
I just started a new job and I broke the build with my very first commit. A fairly trivial and isolated one too. I was pulling my hair out trying to figure out what I did because it




Then I get an email saying there are some race conditions in the build script that cause it to fail randomly.

NotShadowStar
Sep 20, 2000

Hot Dog Day #55 posted:

My boss woke up the other morning and decided we'd be writing all future code in Python 2.4. We develop high speed real time systems and we're all C++ developers.

You could always go with stackless py-- haha oh god I can't do it.

Smugdog Millionaire
Sep 14, 2002

8) Blame Icefrog
I thought of scenario where the for-case antipattern might be valid:
code:
System.Threading.Tasks.Parallel.ForEach(Enumerable.Range(1, 4), delegate(int i)
{
	try
	{
		switch (i)
		{
			case 1:
				DoSomethingForOne();
				DoOtherThingForOne();
				break;
			case 2:
				DoSomethingForTwo();
				DoOtherThingForTwo();
				break;
			case 3:
				DoSomethingForThree();
				DoOtherThingForThree();
				break;
			case 4:
				DoSomethingForFour();
				DoOtherThingForFour();
				break;
		}
	}
	catch (Exception ex)
	{
		logger.LogException(ex);
	}
});
Coding horror or no?

It might not be the best solution (better imo) but at least it does something you don't get from just writing the code out sequentially.

Smugdog Millionaire fucked around with this message at 22:50 on Nov 24, 2010

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...
I can't think of a real-world scenario in which this is a valid option. You're calling four pairs of methods that take no parameters in parallel and you can't figure out how to refactor that to call the same two methods four times with a parameter?

And if they're doing totally different things, then this code is misleading, because the for loop structure implies that you're doing something similar in each step.

It looks like the only "gain" here is that you don't have to wrap each individual method in a try-catch, but again, refactoring the called methods to take a parameter would be the way to go.

This is a horror because it's not obvious what is happening. Your linked code isn't really any better.

POKEMAN SAM
Jul 8, 2004

Smugdog Millionaire posted:

Coding horror or no?

It might be be the best solution (better imo) but at least it does something you don't get from just writing the code out sequentially.


Why wouldn't you do this? (.NET 4.0) I don't know why you're using that Parallel ForEach poo poo.

code:
Task.Factory.StartNew(MyFunction1);
Task.Factory.StartNew(MyFunction2);
Task.Factory.StartNew(MyFunction3);
Task.Factory.StartNew(MyFunction4);

Smugdog Millionaire
Sep 14, 2002

8) Blame Icefrog

Ryouga Inverse posted:

I can't think of a real-world scenario in which this is a valid option. You're calling four pairs of methods that take no parameters in parallel and you can't figure out how to refactor that to call the same two methods four times with a parameter?

And if they're doing totally different things, then this code is misleading, because the for loop structure implies that you're doing something similar in each step.

It looks like the only "gain" here is that you don't have to wrap each individual method in a try-catch, but again, refactoring the called methods to take a parameter would be the way to go.

This is a horror because it's not obvious what is happening. Your linked code isn't really any better.

Each method pair is dumping some selected records from a database into a CSV file and FTPing them someplace. It's not feasible to refactor the data dump into a single method with a parameter. I did, in fact, make the FTP part of the process a method with a parameter.

The gain I see is that the parallel.foreach-case form most closely mimics the form of the code if it were written to be run in serial.

Ugg boots posted:

Why wouldn't you do this? (.NET 4.0) I don't know why you're using that Parallel ForEach poo poo.

The code I actually wrote does use the TPL but your example doesn't wait for the results and doesn't handle the exceptions. Here's the code I wound up using the TPL:
code:
WaitAndLogExceptions(
	Task.Factory.StartNew(delegate()
	{
		DoSomethingForOne();
		DoOtherThingForOne();
	}),
	Task.Factory.StartNew(delegate()
	{
		DoSomethingForTwo();
		DoOtherThingForTwo();
	}),
	Task.Factory.StartNew(delegate()
	{
		DoSomethingForThree();
		DoOtherThingForThree();
	}),
	Task.Factory.StartNew(delegate()
	{
		DoSomethingForFour();
		DoOtherThingForFour();
	})
);


private static void WaitAndLogExceptions(params Task[] tasks)
{
	var unfinishedTasks = tasks.ToList();
	while (unfinishedTasks.Count > 0)
	{
		var i = Task.WaitAny(unfinishedTasks.ToArray());
		if (unfinishedTasks[i].Exception != null)
		{
			foreach (var ex in unfinishedTasks[i].Exception.InnerExceptions)
			{
				logger.LogException(ex);
			}
		}
		unfinishedTasks.RemoveAt(i);
	}
}
Not too pretty.
I'm not seriously advocating this pattern. Just bringing it up :)

necrobobsledder
Mar 21, 2005
Lay down your soul to the gods rock 'n roll
Nap Ghost
I thought the for-case pattern starts becoming valid when you start running things out of sequence like in a series of random tests and may be useful to show that they're different test cases with so much test coverage of inputs:

code:
int ncasestried = 0;
while (ncasestried < whatevernumber) {
  int caseNum = getRandomValidCaseNumber();
  ncasestried++;
  switch (caseNum) {
    case CODE0:
    break;
    case CODE1:
    break;
    case CODE2:
    break;
    default:
    // unhandled case
  }
}

POKEMAN SAM
Jul 8, 2004

Smugdog Millionaire posted:

...

Here's an improved version of yours:
code:
WaitAndLogExceptions(
    () => {
        DoSomethingForOne();
        DoOtherThingForOne();
    },
    () => {
        DoSomethingForTwo();
        DoOtherThingForTwo();
    },
    () => {
        DoSomethingForThree();
        DoOtherThingForThree();
    },
    () => {
        DoSomethingForFour();
        DoOtherThingForFour();
    }
);


private static void WaitAndLogExceptions (params Action[] actions) {
    Task[] tasks = new Task[actions.Length];
    for (int i = 0; i < actions.Length; i++)
        tasks[i] = Task.Factory.StartNew(actions[i]);

    try {
        Task.WaitAll(tasks);
    }
    catch (AggregateException e) {
        foreach (var ex in e.InnerExceptions) {
            logger.LogException(ex);
        }
    }
}
I bet we can make it prettier, too.

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.

Ugg boots posted:

Here's an improved version of yours:
code:
WaitAndLogExceptions(
    () => {
        DoSomethingForOne();
        DoOtherThingForOne();
    },
    () => {
        DoSomethingForTwo();
        DoOtherThingForTwo();
    },
    () => {
        DoSomethingForThree();
        DoOtherThingForThree();
    },
    () => {
        DoSomethingForFour();
        DoOtherThingForFour();
    }
);


private static void WaitAndLogExceptions (params Action[] actions) {
    Task[] tasks = new Task[actions.Length];
    for (int i = 0; i < actions.Length; i++)
        tasks[i] = Task.Factory.StartNew(actions[i]);

    try {
        Task.WaitAll(tasks);
    }
    catch (AggregateException e) {
        foreach (var ex in e.InnerExceptions) {
            logger.LogException(ex);
        }
    }
}
I bet we can make it prettier, too.
Add reflection, it will make it shine.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

necrobobsledder posted:

I thought the for-case pattern starts becoming valid when you start running things out of sequence like in a series of random tests and may be useful to show that they're different test cases with so much test coverage of inputs:
i.e. when it's not actually for-case?

POKEMAN SAM
Jul 8, 2004

Plorkyeran posted:

i.e. when it's not actually for-case?

It just happens to be a for loop creating the test data, and it just so happens to be a switch statement that does the validation, so yeah, it's not really for-case.

Trapick
Apr 17, 2006

I'm doing a group project for uni, and there's some *horrendous* code flying around. I mean, I don't know what I'm doing, but I know better than this...

code:
function renamedFunction(parm1)
{
    if(someOtherFunction(global1, global2))
    {
        return true;
    }
    return false;
}
There's about a dozen functions just like this. It hurts my brain every time I think about it, for several reasons.

Amarkov
Jun 21, 2010

Trapick posted:

I'm doing a group project for uni, and there's some *horrendous* code flying around. I mean, I don't know what I'm doing, but I know better than this...

code:
function renamedFunction(parm1)
{
    if(someOtherFunction(global1, global2))
    {
        return true;
    }
    return false;
}
There's about a dozen functions just like this. It hurts my brain every time I think about it, for several reasons.

Unfortunately, things like that are kinda inevitable when working in group projects. If you told your partners you'd make a function named X that does Y, you sometimes have to deliver that function, even if it turns out you need to do things like wrap another function inside it and use global variables. There are just too many people who will refuse to refactor their code with the changes you made.

karoshi
Nov 4, 2008

"Can somebody mspaint eyes on the steaming packages? TIA" yeah well fuck you too buddy, this is the best you're gonna get. Is this even "work-safe"? Let's find out!

Amarkov posted:

Unfortunately, things like that are kinda inevitable when working in group projects. If you told your partners you'd make a function named X that does Y, you sometimes have to deliver that function, even if it turns out you need to do things like wrap another function inside it and use global variables. There are just too many people who will refuse to refactor their code with the changes you made.

It also can be dereduntalely rewritten reducing it's written redundant redundacy as:
pre:
function renamedFunction(parm1)
{
    return someOtherFunction(global1, global2);
}

Amarkov
Jun 21, 2010

karoshi posted:

It also can be dereduntalely rewritten reducing it's written redundant redundacy as:
pre:
function renamedFunction(parm1)
{
    return someOtherFunction(global1, global2);
}

I was considering pointing that out too, but I've had at least one somewhat competent guy explain to me that he really prefers it when people have an explicit branch. Not that it isn't a retarded way to write code, but I don't like to cry coding horror when some poor dude is just following the style guide.

Goat Bastard
Oct 20, 2004

If your style guide requires you to do that then you have an awful style guide and you should take steps to change it.

bobthecheese
Jun 7, 2006
Although I've never met Martha Stewart, I'll probably never birth her child.
Just be ecstatically happy that you're in a group where other people actually write code at all.

tef
May 30, 2004

-> some l-system crap ->
Hell is other peoples code

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
And after a couple of years, to me I'm other people.

karoshi
Nov 4, 2008

"Can somebody mspaint eyes on the steaming packages? TIA" yeah well fuck you too buddy, this is the best you're gonna get. Is this even "work-safe"? Let's find out!

Amarkov posted:

I was considering pointing that out too, but I've had at least one somewhat competent guy explain to me that he really prefers it when people have an explicit branch. Not that it isn't a retarded way to write code, but I don't like to cry coding horror when some poor dude is just following the style guide.

Yeah, it wasn't clear from his post what exactly made him laugh/cry. Maybe it was a 5-depth nesting of interface implementations, each one just calling another function that calls just another function...
Personally I prefer code I can read quickly, where I don't need 5 seconds to parse it, when 1 second should be enough. That would be my style guideline. vOv

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

bobthecheese posted:

Just be ecstatically happy that you're in a group where other people actually write code at all.
Some of my favorite group projects in college were the ones where other people didn't try to write code. A terrible group member is much worse than one that does nothing.

Trapick
Apr 17, 2006

Plorkyeran posted:

Some of my favorite group projects in college were the ones where other people didn't try to write code. A terrible group member is much worse than one that does nothing.
It's worked out pretty good, actually. We have a group member who can't write code to save his life, but he knows it, so he's doing an insane amount of testing. Very, very handy.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

Trapick posted:

It's worked out pretty good, actually. We have a group member who can't write code to save his life, but he knows it, so he's doing an insane amount of testing. Very, very handy.

Ours did the documentation :D

bobthecheese
Jun 7, 2006
Although I've never met Martha Stewart, I'll probably never birth her child.
One of ours spen the whole time offering to get everyone else meth, one left the country until the project was nearly done. I ended up writing about 20k lines of code myself because the only people in the group who could have helped were busy with our 600 page design document.

This was our final project, and it was for a real client who had paid the university to get it done. Failure to jump through all the hoops that the uni put up meant that we would have to do another year long project the next year.

plushpuffin
Jan 10, 2003

Fratercula arctica

Nap Ghost

Zaxxon posted:

why do I keep seeing crap like this.

#define BSP_DISABLE_RXMP __BSP_DISABLE_RXMP__

and such. Really who does this poo poo help?

You know what I found in our source the other week?

#define PROTRAIT PORTRAIT

One of the programmers consistently mistyped the word PORTRAIT (an identifier for some graphics library or something), and decided to solve the problem once and for all.

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
poor spelling certainly is a pro trait.

evensevenone
May 12, 2001
Glass is a solid.
it's the end of the quarter, and I already wrote transpose, so why waste time?
code:
GraphRef copyGraph(GraphRef G)
{
	return transpose(transpose(G));
}

_aaron
Jul 24, 2007
The underscore is silent.

evensevenone posted:

it's the end of the quarter, and I already wrote transpose, so why waste time?
code:
GraphRef copyGraph(GraphRef G)
{
	return transpose(transpose(G));
}
Haha, I actually kinda like this. It's bad, but still mildly clever :)

Adbot
ADBOT LOVES YOU

Nigglypuff
Nov 9, 2006


BUY ME BONESTORM
OR
GO TO HELL
Any decent compiler will be able to optimize it away :colbert:

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