views:

741

answers:

16

This is not really a "question" so I'm making it CW.

The

assert

Keyword is great!

It should make, feel your self more confident with the code you wrote, but, until today when I was creating a small test class ( < 20 lines ) I realize a never use it since it was introduced.

Heck! I barely use logger which is very useful indeed, but it wasn't until today I realize I don't use assertions.

Do you use assertions? If no, what's the reason?

+2  A: 

Nope, never use them. Dunno why, just never got into the habit.

jjnguy
A: 

I tend not to use them, although I'm not sure why either.

If you do unit testing, such as with nUnit, you would obviously use them all the time to verify your test results.

Will Eddins
+11  A: 

I was taught to use lots of assertions back in the 90s and they made great sense. It's good defensive coding.

However, I think this has now been superceded by unit testing that actually forces the code to run and tells where it is broken. Debug asserts required someone to actually be running debug code and looking at the ui or logs. Unit testing can automate this.

Barry Hurt
During testing, rats probably won't gnaw through a communications cable, someone playing a game won't exhaust memory, and log files won't fill the hard drive. These things might happen when your program runs in a production environment. [Hun 00] The Pragmatic Programmer
cmdev
So are you claiming that debug asserts somehow solve these problems? Or are you saying that debug asserts, unit testing, etc. can't solve these problems? Or that all testing is bad therefore don't do it?
Barry Hurt
I disagree strongly with this answer. It has NOT be superceded, it has been complemented. Unit tests don't replace assertions. Assertions are very useful to avoid programming mistakes, like passing null to a method which doesn't accept null. It's right there, for your eyes, your parameter CAN'T be null, so you don't have (if param != null) all over the place...
Gilles Philippart
@gphillip - I agree, complemented is a better way of looking at it.However, I stand by my claim that automated unit tests (and they have to be good unit tests) can tell you more about subtle breaks. Sometimes the debug asserts are buried way deep in code you are calling from a couple of levels up, so you may not see it when coding, or executing down a certain path. You are correct, it's right there for your eyes, but only if you are looking at it or executing it.Not that unit testing fixes that completely either, but a use of both might be good.
Barry Hurt
+11  A: 

I use them all the time. They're a nice way to practice the "Crash early" philosophy, better to have to solve why an assertion failed than to have to deal with bad/corrupted output.

The issue is you have to kind of make it a habit. I rarely see any middle ground in it, people are either not used to it and almost never use them or people use them and they're littered rigorously throughout the code. You just have to get into the mindset of noticing "Oh hey, I'm implicity assuming something here, let me explicitly confirm it 'assert(ASSUMPTION)'"

Falaina
For what I read in your answer, I think a validation would do better, because after all assert are meant to be removed on production code. The main purpose was to do expensive check ups in beta product.
OscarRyz
Assert should not be removed in production code... They're too useful to understand why the process is failing (think about null propagation in 4 or 5 layers, where does this null comes from ?). And don't get me started on performance hit...
Gilles Philippart
+2  A: 

I don't use them. Unit tests should be sufficient to test your code. Furthermore, since they're disabled by default, they're normally completely ignored anyways. Then they just tend to clutter up your code with useless assertions that could be better expressed as comments.

If you really need this, though, some libraries have assertion static methods you can call that will not be skipped - these are also a lot more readable, because the assert keyword is uncommon and immediately may cause a "wtf" moment, but the Assert.x methods are just methods that can be traced through. The Spring framework, in particular, uses an assertion library.

MetroidFan2002
+3  A: 

Short answer - Yes.

Long Answer - Not always, but quite often. I usually use assertions for errors I know I can do nothing about(while the program is running) and logging isn't really required. For example - If I have to check if a certain value is out of bounds or if a pointer is NULL even though it should have some value.

For other stuff like "Parsing files" and "File couldn't be found", I usually throw exceptions. That way I can log the error and use some fail safe file/method instead.

And I quite agree with with Falaina, you really should make it a point to notice - "HEY! I'm making some assumptions here"

vhanda
+1  A: 

I tend to check for error conditions and throw exceptions instead. The reason is that I want these conditions to always be checked, even in production, and exceptions provide for easier handling of the failed condition rather than assertions.

Larry Watanabe
+7  A: 

I use assertions to make sure that I don't introduce errors in my code. If I know that a value should be in a map, I assert for that (using the assert keyword). If I know that a parameter should never be null, I assert for that. If I know that the parameter can be null, then I will check it and throw the appropriate exception.

I read it at Code Complete or Effective Java - assertions should be used to detect programming errors - exceptions should be used to handle exceptional but possible situations. You don't need to check for null on every method on your code if you know that the value will not be null (as defined by a contract), but it doesn't hurt to assert if a value is not null. Assertions are only enabled if you specify the parameter -ea to the VM and they should not impact the performance of your application if disabled.

You should use more logging too :-). Learn when to use trace, debug and info and make sure that you log everything that your application does. It makes life so easier when you have to figure out why something is not working in a production environment.

Ravi Wallau
"assertions should be used to detect programming errors" **exactly** +1
TM
A: 

No, I don't use them.

I was taught, that asserts should not be used in 'production' code, and before I start using something that I have to remove anyway - according to what I've learned - I stick with exception to validate conditions.

Andreas_D
A: 

I never used to use them, but I had an awful time debugging my last program, and at one point was logging when variables were null or didn't contain the values that I would expect. Once I'd got it all working I asserted everything that my program needed in order to run successfully.

lhnz
+1  A: 

The Java assert keyword is pretty half assed (need to run the program with the -ea commandline option) so I find myself relying on exceptions instead of asserts.

James McMahon
that should be "-ea" (short for -enableassertions)
newacct
Thanks, I corrected that.
James McMahon
+4  A: 

The only real use for assertions after unit testing was introduced is to communicate the invariant of a method to other programmers. And it is much better to do this in actual code than in comments which they will ignore anyway. It is hard to ignore an assert!

Kristian
A: 

No. But can't wait. I used to unit test everything with junit, but that was school, small projects no pressure. i'm in real life now, i supposed to finish this code yesterday....

jbendahan
+1  A: 

If you like asserts, you'll LOVE contracts. They are basically the idea of asserts extended to more situations.

Imagist
+2  A: 

Yes! Always! The keyword is my very best friend in every language!

There's no real reason not to use asserts, they ensure that the basic assumptions I make on input values and states are upheld.

If an assert fails, it means that I need to re-evaluate my assumptions and update the code to handle new exotic input that I did not think of when writing the current revision. What's not to like about that?

As usual in programming, it's a tool that's powerful only when used properly.

Christoffer
+1  A: 

Yes, I use asserts all the time, basically whenever I'm making an assumption that:

  1. Can be checked with a fairly simple predicate.
  2. Isn't obviously true simply from reading the nearby code.

However, for asserts that are likely to have a negligible impact on performance, I use the enforce function in the D programming language standard library instead of assert. This does basically the same thing as assert except that it stays around in release mode. For more expensive assertions, I use assert, which is stripped from release mode builds.

dsimcha