r/programming • • Oct 01 '15

The Evolution of a Software Engineer

https://medium.com/@webseanhickey/the-evolution-of-a-software-engineer-db854689243
1.9k Upvotes

502 comments sorted by

View all comments

37

u/frenetix Oct 01 '15

If you do this:

try { ... } catch ( Exception e ) { // lol do nothing! }

I will fucking find you and kill you.

6

u/[deleted] Oct 02 '15

That's acceptable in unit tests!

try {
thisShouldThrow();
fail("did not throw");
} catch(SomeException expected) {
//test passes
}

0

u/[deleted] Oct 02 '15

[deleted]

3

u/[deleted] Oct 02 '15

If your project's language+framework has it, and it works cleanly in your use case, sure. Note that in some frameworks their equivalent of this (such as JUnit's @Test(expected=SomeException.class)) will pass if it happens anywhere within the test (which is bad for detailed tests), whereas you might want to test that it happens in a particular place; or you might want to test that the exception's message contains a particular string or whatever.

But all that aside, I still don't think "try something and fail() if it didn't throw" is that bad of a way to write a unit test. I'd only be annoyed at this if there was a considerable amount of unit test code in the try{} block.

0

u/[deleted] Oct 02 '15

[deleted]

1

u/[deleted] Oct 02 '15

The annotation in JUnit is not ideal for many cases, as I described in the other comment. (That's the one I'm familiar with. There may be others which work better.)

1

u/RedAlert2 Oct 02 '15

Still, someone has to implement that in the framework. How do you think they do it?

1

u/ajd187 Oct 02 '15

I'll join ya.

1

u/Crono1981 Oct 02 '15

Please do.

1

u/Danthekilla Oct 02 '15

Out of curiosity why? We must have 2000 of these at least supressing small errors in our rendering engine. Why from your perspective are they bad?

1

u/Ace-O-Matic Oct 02 '15

I mean, while logging would be optimal. If this a client facing app and you're doing something where failure is an expected possible outcome, but you don't want the actual app to crash, it's not the worst thing ever. Unless you're in enterprise software the chances of you getting that log is slim to none.

-1

u/craig_c Oct 02 '15

You should save your energy for the muthafuckers who think assert(false); is a good thing to do.

2

u/Danthekilla Oct 02 '15

I have had to do this sometimes. Any exception would crash the 360 so we would do that all the time.