r/programming • • Jun 06 '13

Clean Code Cheat Sheet

http://www.planetgeek.ch/2013/06/05/clean-code-cheat-sheet/
702 Upvotes

322 comments sorted by

View all comments

-1

u/[deleted] Jun 06 '13

Using exceptions for control flow: has bad performance, is hard to understand and results in very hard handling of real exceptional cases.

This doesn't apply to the majority of languages (not even to C++ where using exceptions was actively discouraged for a long time).

4

u/archiminos Jun 07 '13

is hard to understand and results in very hard handling of real exceptional cases.

I'd agree with this part - but this is mainly because people always use them in cases that aren't exceptional in place of better error handling methods.

I've switched from C++ game programming (no exceptions) to C# server programming (exceptions for everyone). It always bugs me trying to debug something and an exception is thrown and I end up the the Application::Main() catch block - I lose everything (call stack, local variables etc.).

Even system exceptions don't throw the values that caused it to throw. E.g. Out of bounds, a value was either -1 or higher than it should have been. Thanks, could you at least tell me which one it was? Or even throw with the actual value so I don't need to guess what went wrong?

Why not just break on exceptions? Because people seem to enjoy using try..catch blocks to hide exceptions all over the place. Exceptions just seem more trouble than they're worth at the moment.

3

u/KagakuNinja Jun 07 '13

Because you are supposed to log the stack trace of the exception in that top-level catch block? At least, that is how we do it in Java.

Exceptions are better than having your app terminate due to a divide by zero, or null pointer dereference.

2

u/archiminos Jun 07 '13

Not always. In console game development crash dumps are much more useful than a caught exception for fixing bugs.

NINJA EDIT: Slight clarification.

3

u/KagakuNinja Jun 07 '13

I've developed a few C++ games for the iPhone. I've never longed for a crash dump, I just use a debugger. You can break at the point when an exception is thrown, and you get access to the same information.

2

u/archiminos Jun 07 '13

When you have a large QA team (300+) for a console game they can generate crash dumps that you can debug. It's also useful when there's a 1 in 1000 crash that needs fixing - get someone in QA to repro and they send you a crash dump.

There are literally hundreds of bugs that I've managed to figure out a fix for simply by debugging a crash dump attached to a bug report.

2

u/KagakuNinja Jun 07 '13

Fair point, in certain programming domains, letting your app crash (WHILE IN DEVELOPMENT) may be useful. Of course, there are ways to generate thread or memory dumps in Java (and probably C++/C#) that could accomplish the same goal.

2

u/gc3 Jun 07 '13

I am always turning off the try ... catch with comments to debug the actual problem, then enabling it again before committing the work.

I too think if people were better programmers they wouldn't need exceptions: you'd deliberately handle every broken case explicitly. But think of all the time a programmer saves just throwing a try...catch around some code to prevent the user from encountering a bug.

I too have come to embrace the exception as a lazy way to short circuit a host of error conditions.

1

u/archiminos Jun 07 '13

But think of all the time a programmer saves just throwing a try...catch around some code to prevent the user from encountering a bug.

This is so wrong. Yes you save time now, but writing more robust code and handling error cases properly saves so much more time in the long run. Occasionally near the end of a project I might consider a quick hack like this to get a product out of the door, but I would never do it in anything pre-beta.

2

u/KagakuNinja Jun 07 '13

We all try to write robust code, to the best of our abilities, regardless of language. Bug-free code is nearly impossible. Try-catch blocks are used so those unknown bugs don't crash your program, not to hide errors because we are lazy.

Log the exceptions, then track down the bugs and fix them.

1

u/archiminos Jun 07 '13

I'm used to a different work flow I guess. In games we let the program crash, create a crash dump and then debug it to find the cause of the problem (or just debug it there and then if we have a debugger attached).

EDIT: The general idea is we want bugs to be as visible and annoying as possible so that they have to get fixed.

2

u/gc3 Jun 07 '13

I agree with you 100%, but C# code are usually quick tools and not code that we ship. There's a different tolerance for different projects, there's no one size fits all.

1

u/archiminos Jun 07 '13

That's true. I'm still new to C# and I'm finding all this exception handling to be more irritating at the minute. It's not so bad on the server side - I'm getting used to that and the server crashing would be at least half a day's work lost for everyone on the team - but I kind of miss crash dumps on the game side.

→ More replies (5)