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
pigdog
Apr 23, 2004

by Smythe
This is a very nice article on how to use exceptions:
http://www.sysart.fi/news/9/37/Exceptions---the-big-picture/d,artikkeli

Adbot
ADBOT LOVES YOU

Plorkyeran
Mar 22, 2007

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

Gul Banana posted:

- a background thread or detached process which reports on its failure
- a WCF service which encodes them into faults
- a library component which needs to encapsulate failures of its own dependencies into a predictable exception type
- auditing code which logs the exceptions then lets them continue up the call stack
The first three are all pretty much the same as catching everything in Main; it's really more of that catching everything at a module boundary and translating it to a different form frequently makes sense.

Volte
Oct 4, 2004

woosh woosh
Also, if you're rethrowing the exception then you're effectively not catching it at all, from a control flow point of view. You're just temporarily intercepting it and doing some debugging side effects.

GrumbleGrumble
Jul 11, 2004

Ithaqua posted:

The code example I gave above was like that from top to bottom. Every method started with a try block and ended with an empty catch. It was a program that was supposed to be run 24/7 (it wasn't a service because that's harder than a console application run on the task scheduler), so I guess the developer responsible never wanted it to crash.

Note: the application did not work very well or reliably for some reason. But it never crashed!

I think we've worked on the same application, in my case that wasn't even the worse horror.

The worse horror was the 1000 line method that managed game states through a web of conditional checks juggling local and member variables. I renamed the method "gameManager" in case I didn't have time to rewrite it... :(

Along with that there was something like :
code:
if ((( a == false) || (b == false)) && (a == b ))
{
  c = false;	
}
which I deciphered as meaning that at one point they were using the same untyped variables for every check within the method. Yay code re-use! :golfclap:

Gul Banana
Nov 28, 2003

Plorkyeran posted:

The first three are all pretty much the same as catching everything in Main; it's really more of that catching everything at a module boundary and translating it to a different form frequently makes sense.

true enough

Zhentar
Sep 28, 2003

Brilliant Master Genius

Volte posted:

Catching all exceptions in Main is fine for production releases, since in the event of a fatal error you can display a pretty error message with an appropriate amount of information rather than whatever normally happens (like a generic .NET exception message followed by an illegal operation), plus you can generate an error report that can be sent in.

Nope, this is still wrong. .NET offers several events (such as AppDoman.UnhandledException) that you can (and should) hook to achieve this, without writing a try/catch at all.

Volte
Oct 4, 2004

woosh woosh

Zhentar posted:

Nope, this is still wrong. .NET offers several events (such as AppDoman.UnhandledException) that you can (and should) hook to achieve this, without writing a try/catch at all.
I could be wrong but I'm pretty sure that will not prevent the standard .NET exception window from showing. You can use both at the same time but I'm pretty sure you need try/catch to actually absorb the exception and display something more user-friendly.

pigdog
Apr 23, 2004

by Smythe
A clusterfuck of genuine coding horrors:

http://facepunch.com/showthread.php?t=1226297

Amarkov
Jun 21, 2010
^^^^^

facepunch posted:

>Another method : Create a Dictionary<string, string>, fill it with your characters and their replacements, then replace the characters one by one by their representations in the Dictionary

This is O(n^2) or O(n log n).

You know, I really embarrassed myself in my first coding interview. I had a lookup table that didn't need any sort of order on it, but I insisted it should be implemented as a linked list despite multiple prompts from the interviewer.

I guess some people just kinda do that whenever or something

Amarkov fucked around with this message at 04:16 on Dec 3, 2012

Opinion Haver
Apr 9, 2007

quote:

Here's my O(n) or O(2n) implementation (depending on pre-existence of the target buffer):

Tell me more about your constant factors in asymptotics :allears:

Zhentar
Sep 28, 2003

Brilliant Master Genius

Volte posted:

I could be wrong but I'm pretty sure that will not prevent the standard .NET exception window from showing. You can use both at the same time but I'm pretty sure you need try/catch to actually absorb the exception and display something more user-friendly.

You are, in fact, wrong. I've used them to replace the standard .NET popup with automatic error reporting.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
This isn't really a coding horror of any sort (well, maybe it is and I don't know it), but it made me laugh. I wanted to draw a "polygon graph" with the number of edges as a parameter, using TikZ, and came up with the following code:

code:
\newcommand{\circumferencenode}[3]{\node (#1) at (#3: #2) [label = #3: $#1$] {};}
\newcommand{\polygon}[2]{
    \pgfmathparse{subtract(#1, 1)}
    \foreach \x in {0, ..., \pgfmathresult} {
        \pgfmathparse{90 - 360 * \x / #1}
        \circumferencenode{\x}{#2}{\pgfmathresult}
    }
    \pgfmathparse{subtract(#1, 1)}
    \foreach \x in {0, ..., \pgfmathresult} {
        \pgfmathparse{mod(\x + 1, #1)}
        \draw (\x) -- (\pgfmathresult);
    }
}
The result:



Looks bizarre, right? The reason is that the PGF "mod" function returns a float, so I was basically doing things like \draw (0) -- (1.0). And TikZ uses syntax like (nodename.angle) to mean points on the perimeter of a node at the specified angle from the centre, so I was telling it to draw each line from the centre of one node to the right-hand side of the next. :downs:

Volte
Oct 4, 2004

woosh woosh

Zhentar posted:

You are, in fact, wrong. I've used them to replace the standard .NET popup with automatic error reporting.
I stand corrected (your avatar goes well this this post).

Summit
Mar 6, 2004

David wanted you to have this.
I came across a real gem this morning (C#):

code:
namespace Site.Clarity.@public {

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

Summit posted:

I came across a real gem this morning (C#):

code:
namespace Site.Clarity.@public {

:barf: Just because you can use keywords inappropriately by prefixing them with an @ doesn't mean you ever should.

davejk
Mar 22, 2007

Pillbug

Ithaqua posted:

:barf: Just because you can use keywords inappropriately by prefixing them with an @ doesn't mean you ever should.

Unless you're using ASP.NET MVC, in which case adding CSS classes to any generated markup means having new { @class = "stupid-class" } all over the place.

davejk fucked around with this message at 16:44 on Dec 3, 2012

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Summit posted:

I came across a real gem this morning (C#):

code:
namespace Site.Clarity.@public {

language design

Zorro KingOfEngland
May 7, 2008

code:
data = new Array(
                2, 2, 2, 2, 2, 2, 
                4, 4, 4, 4, 4, 4, 
                4, 4, 4, 4,     4, 
                3, 3);
Is there a significance to that 4 having whitespace? What does 4 even mean? Why do we keep paying these contractors?

:iiam:

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Zorro KingOfEngland posted:

Is there a significance to that 4 having whitespace?

They're paid by the line.

Zamujasa
Oct 27, 2010



Bread Liar
:downs: Do we have a way to test something-or-other yet?
:cool: About that... I was going to work on rewriting some of your stuff.
php:
<?
        protected function request_file_list($path){
            $fp = fopen("/tmp/". $this->_out_path, "w");
            $our_url = "http://some-url-whatever-flkshaflskdfhsaldkfhsfkh/". $path;
        
            $ch = curl_init($our_url);
            curl_setopt($ch, CURLOPT_FILE, $fp);
            curl_setopt($ch, CURLOPT_VERBOSE, false); # Not needed per PHP defaults
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
            curl_setopt($ch, CURLOPT_PORT, $this->_alternate_port); # can be left out by specifying the port in the URL
            curl_setopt($ch, CURLOPT_USERPWD, $this->http_username":".$this->http_password);
        
            curl_exec($ch);
            curl_close($ch);
            fclose($fp);
            $response = file_get_contents("/tmp/". $this->_out_path);
            unlink("/tmp/". $this->_out_path); # never heard of CURLOPT_RETURNTRANSFER I guess?
            return $response;
        }
        
        protected function request_file($file_path){
            $our_url = "http://some-url-whatever-slightly-different-though/". $file_path;
        
            $ch = curl_init($our_url);
        
            $fp = fopen("/tmp/". $this->_file_path, "w");
            curl_setopt($ch, CURLOPT_FILE, $fp);
            curl_setopt($ch, CURLOPT_VERBOSE, false);
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
            curl_setopt($ch, CURLOPT_PORT, $this->_alternate_port);
            curl_setopt($ch, CURLOPT_USERPWD, $this->http_username.":".$this->http_password);
        
            curl_exec($ch);
            curl_close($ch);
            fclose($fp);

            // notice this does not return the contents of the file
            // it is read elsewhere in the class, unlike the above one
            // these little 'gotchas' are everywhere
        }

 /* there are at least 2 other instances of this 
    all of the comments are mine */

?>
:cool: ...like this. See how you're doing the same thing here? That could just be 'curl_request' or something and you'd just need to pass the details.
:downs: clearly offended by the suggestion his code is subpar But that'd save like five lines!
:cool: Yes, but it means that the only part of that method is the part that matters.
:saddowns: I just don't think it's worth worrying about right now.
:cool: The whole point of this rewrite is because nobody ever did this poo poo, so we ended up with zero functions. We're here to do it right this time, not make the same mistakes.


:sigh:

It wouldn't be so bad, but he has two classes; class A has X() and Y(), and class B has X(), Y(), and Z() (and so on). X and Y are the exact same.

And yet, he did not just leave them out of the second class and add inheritance.

:( I wish I could rewrite all of this crap, but it's really specific bullshit and undocumented, of course.

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

Zamujasa posted:

:downs: Do we have a way to test something-or-other yet?
:cool: About that... I was going to work on rewriting some of your stuff.

:cool: ...like this. See how you're doing the same thing here? That could just be 'curl_request' or something and you'd just need to pass the details.
:downs: clearly offended by the suggestion his code is subpar But that'd save like five lines!
:cool: Yes, but it means that the only part of that method is the part that matters.
:saddowns: I just don't think it's worth worrying about right now.
:cool: The whole point of this rewrite is because nobody ever did this poo poo, so we ended up with zero functions. We're here to do it right this time, not make the same mistakes.


:sigh:

It wouldn't be so bad, but he has two classes; class A has X() and Y(), and class B has X(), Y(), and Z() (and so on). X and Y are the exact same.

And yet, he did not just leave them out of the second class and add inheritance.

:( I wish I could rewrite all of this crap, but it's really specific bullshit and undocumented, of course.

There's a saying I've heard (and believe):
If you write the same code twice, you're probably doing something wrong.
If you write the same code more tha twice, you're definitely doing something wrong.

Hughlander
May 11, 2005

Ithaqua posted:

There's a saying I've heard (and believe):
If you write the same code twice, you're probably doing something wrong.
If you write the same code more tha twice, you're definitely doing something wrong.

I call it my rule of 3. Do something once ok. Have to do it a second time? Grumble a bit but probably do it. A third time? Take the time to refactor that poo poo right then and there. Though I'm probably the horror for the 2nd time.

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.
C++ code:
// TODO: WTF is going on here?!?
// Volume control (ramping)
static inline u16 ADPCM_Vol(u16 vol, u16 delta)
{
        int x = vol;
        if (delta && delta < 0x5000)
                x += delta * 20 * 8; // unsure what the right step is
                //x += 1 * 20 * 8;
        else if (delta && delta > 0x5000)
                //x -= (0x10000 - delta); // this is to small, it's often 1
                x -= (0x10000 - delta) * 20 * 16; // if this was 20 * 8 the sounds in Fire Emblem and Paper Mario
                        // did not have time to go to zero before the were closed
                //x -= 1 * 20 * 16;

         // make lower limits
        if (x < 0) x = 0;      
        //if (pb.mixer_control < 1000 && x < pb.mixer_control) x = pb.mixer_control; // does this make
                // any sense?

        // make upper limits
        //if (mixer_control > 1000 && x > mixer_control) x = mixer_control; // maybe mixer_control also
                // has a volume target?
        //if (x >= 0x7fff) x = 0x7fff; // this seems a little high
        //if (x >= 0x4e20) x = 0x4e20; // add a definitive limit at 20 000
        if (x >= 0x8000) x = 0x8000; // clamp to 32768;
        return x; // update volume
}
From the Dolphin emulator, http://code.google.com/p/dolphin-emu/source/browse/Source/Core/Core/Src/HW/DSPHLE/UCodes/UCode_AX_ADPCM.h
see this article if you want to read about the plugin being rewritten

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
Yeah, that's what clean room reverse engineered code looks like. I hope you never stumble into the wrong parts of glibc by accident.

hobbesmaster
Jan 28, 2008

Suspicious Dish posted:

Yeah, that's what clean room reverse engineered code looks like. I hope you never stumble into the wrong parts of glibc by accident.

A line or three describing the methodology would be helpful though.

Zamujasa
Oct 27, 2010



Bread Liar

Ithaqua posted:

There's a saying I've heard (and believe):
If you write the same code twice, you're probably doing something wrong.
If you write the same code more tha twice, you're definitely doing something wrong.

That's good advice I'm starting to take into account. I've always been more of a procedural guy (especially since I started with BASIC and progressed to PHP; C's pointers and lack of easy-to-use strings made my brain hurt years ago) and getting into classes and inheritance and all that poo poo is a pretty big shock.

It's basically been non-stop self-obsolescence, but the time-frames here make it so that I can't go back and make a version two of the first thing I wrote (with the boss's "IT loving WORKS DON'T EVER TOUCH IT AGAIN :mad: except to implement these few things :downs:"... I do what I can.

That's a good, simple rule, though. Doing things twice can be understandable (something I'm writing now requires two slightly-different ways of getting it to work that are different enough to not be worth melding at this point), but three is probably overkill regardless of the situation.

The Gripper
Sep 14, 2004
i am winner
Most of the time the methodology boils down to trial-and-error, and the result is "whatever works". With Nintendo it's even more of a mindfuck because not every game handles things the same way, and on the Wii at least the actual system can differ depending on game.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
What do you want for a methodology? They found something that works, they wrote code, and then it broke for a game, and then they fixed something for both.

The way the system works wasn't properly worked out yet when they wrote the code, so they played around until something worked.

The Gripper
Sep 14, 2004
i am winner

Suspicious Dish posted:

What do you want for a methodology? They found something that works, they wrote code, and then it broke for a game, and then they fixed something for both.

The way the system works wasn't properly worked out yet when they wrote the code, so they played around until something worked.
I don't know if that was directed at me, but I was agreeing! Games on the Wii can reimplement the operating system, so a lot of the code is undocumentable since it's band-aids of the "oh, this game wants this to work a different way? Ok, here you go" kind, without actually delving into the why's of it (because it's irrelevant).

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
It was directed at the guy who asked for a methodology.

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.

The Gripper posted:

Games on the Wii can reimplement the operating system,
Wait, they can? I knew that the OS is run off the game disc but not that they could do that.

The Gripper
Sep 14, 2004
i am winner

Aleksei Vasiliev posted:

Wait, they can? I knew that the OS is run off the game disc but not that they could do that.
Yep. The Wii has a standard IOS (the operating system), and a lot of games just use that, but even then that OS is stored on-disc even if they use the default. Developers can reimplement parts if necessary, and a lot of Nintendo titles do that (most of the Marios, Zeldas etc.)

There's no hypervisor like there is with the PS3 and Xbox360.

edit; I guess it's handy because it means Nintendo never has to worry about their system updates breaking games, and for developers since they don't need to wait for upstream changes by Nintendo for things like peripheral support e.g. Guitar Hero.

The Gripper fucked around with this message at 07:04 on Dec 4, 2012

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

The Gripper posted:

Developers can reimplement parts if necessary, and a lot of Nintendo titles do that (most of the Marios, Zeldas etc.)

Nope. IOS is solely developed by Nintendo, and every single version is available through the Nintendo Update Service ("NUS"). Some games have an update partition, but this is simply an offline mirror. It is against the developer agreement to modify IOS.

Wii development is a whole other set of horrors; you're correct in that there is no OS or hypervisor running on the main CPU (IOS runs on an small ARM I/O bridge fan-named "Starlet" found in the GPU die). Things like the Home Menu are found on every game disc.

If people want to know more about this, or the horrors of it, I can explain it. For now: remember when System Menu 4.0 added WiiWare launching direct from SD? They implemented it by copying it to Internal RAM, running it from there, and then deleting it afterwards, for various silly reasons related to how they set up their architecture.

Suspicious Dish fucked around with this message at 07:16 on Dec 4, 2012

hobbesmaster
Jan 28, 2008

The Gripper posted:

I don't know if that was directed at me, but I was agreeing! Games on the Wii can reimplement the operating system, so a lot of the code is undocumentable since it's band-aids of the "oh, this game wants this to work a different way? Ok, here you go" kind, without actually delving into the why's of it (because it's irrelevant).

Well you have magic numbers, it'd be nice to know how you came up with them so if something is off when you find another game you know what each knob does.

I've seen far worse embedded code. At least they're not ignoring nice register and flag defines.

The Gripper
Sep 14, 2004
i am winner

Suspicious Dish posted:

Nope. IOS is solely developed by Nintendo, and every single version is available through the Nintendo Update Service ("NUS"). Some games have an update partition, but this is simply an offline mirror. It is against the developer agreement to modify IOS.
Huh, well it looks like there are a handful of games using their own version of IOS but they're all Nintendo titles (SSMB, Mario Galaxy), so I guess it's a thing but not a thing unless you're Nintendo.

Suspicious Dish posted:

EDIT: not sure what "SSMB" is. Were you talking about "Super Smash Bros. Brawl" (SSBB)?
Yes, I had mario on the brain so I merged them both into super smash mario brawl.

The Gripper fucked around with this message at 07:57 on Dec 4, 2012

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

The Gripper posted:

Huh, well it looks like there are a handful of games using their own version of IOS but they're all Nintendo titles (SSMB, Mario Galaxy), so I guess it's a thing but not a thing unless you're Nintendo.

Other games besides the one you mention use IOS35 and IOS36. It's released to all developers, first-party or third-party, at the same time through the SDK distribution channels.

EDIT: not sure what "SSMB" is. Were you talking about "Super Smash Bros. Brawl" (SSBB)?

The Gripper
Sep 14, 2004
i am winner
I didn't realise they were actually reused, though I knew they were available.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Hughlander posted:

I call it my rule of 3. Do something once ok. Have to do it a second time? Grumble a bit but probably do it. A third time? Take the time to refactor that poo poo right then and there. Though I'm probably the horror for the 2nd time.

Looks like we've discovered the DRY principle for the second time in 5 pages. Except this time we're more serious about it.

ninjeff
Jan 19, 2004

Wheany posted:

Looks like we've discovered the DRY principle for the second time in 5 pages. Except this time we're more serious about it.

SRP-driven caution in 5... 4...

Adbot
ADBOT LOVES YOU

McGlockenshire
Dec 16, 2005

GOLLOCKS!

Wheany posted:

Looks like we've discovered the DRY principle for the second time in 5 pages. Except this time we're more serious about it.

So if we continue this discussion on the next page, does that mean we have to go back and refactor our posts?

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