Myths about Debug.Assert!
Earlier tonight, I was giving a presentation on and I interjected a sidebar - “Everyone here uses Debug.Assert, Right? ” because the code I was demonstrating was littered with Debug.Assert Statements. Why did I feel like asking this? Well, because to date,my use of Assertions has either pi33ed people off, been dismissed as a“useless waste of time”, or got me made fun of. The best response I got before this was merely telling me that they are unnecessary and not to use them. So I guess I was looking for validation of some sort because it'ssomething that i can't bring myself to give up. Nor is it something I have the least bit of desire to give up.My name isBill and I'm an AssertionAholic. I'm proud of that fact.If anything, I don't use them enough. So anyway, ONE person raised therehand when I asked if anyone used them. One hand. There were probably between 25-40 developers - all of which indicatedearlier that they were .NET developers. I got a few blank looks, but I also got abunch of eye rolling and smirks. One other person indicated he used to useAssertions but doesn't any more.
I guess I wouldn't have thought twice about it if I hadn't had such scorn in the past for using them.But it sunk intonight - I've never MET someone other than at product demos - who openly advocates using Assertions. Maybe I have, butI never heard anything said about it. I've never seen them in a product demo (although to be fair, I know Jeff Prosise uses them - demo code isn't the same stuff we write in production - Somewhere deep in my heart I know Jeff uses them). Here I am in aroom full of experienced .NET Developers, no doubt talented ones, and the best I got was one hand raised, one guy that used to use them, and apathy. IF YOU'VE NEVER HEARD OF ASSERTIONS - YOU ARE NOT THE SUBJECT OF MY RANT HERE! Unless of course upon learning about the virtue of assertions - yougo over to the dark side andbecome on of those thatridicule people who do. I was ignorant of them too, but “Where Once I saw darkness, now I see light”
Two men, who would follow off of a cliff - John Robbins and Paul Kimmel are responsible for helping me heal my evil ways. But this enlightenment, has brought me nothing but scorn. The problem is, John Robbinsused to jump out of airplanes when he was in the special forces, in the middle of the night from low altitude, and then go kick people's a33. Plus, he helped found http://www.wintellect.com/anddudes like Jeffery Richter, Dino and Francesco got his back, so no one messes with him. Paul, well, he's Paul Kimmel. There's also Dave Thielen, author of No Bugs, so when they use Assertions, no one says anything about it. But I'm just Bill - so the best I get is, well, a friendly ear on occassion - but most of the time I get laughed more than John Stewart does. Except they aren't laughing at him, and they are laughing harder at me. Or I get a polite but pretty stern rebuke. I'm not kidding here - seriously. On two occassions I've downright had my a33 chewed out becuase “MY” STUPID boxes keep popping up when they used one of the libraries I had written. If “MY” stupid boxes were popping up because I wrote the assertion incorrectly, that'd be fine - but in both cases it was a test for NULL and an object that was supposed to be instantiated but wasn't, failed my assertion. In short - my assertions DID exactly what I wanted them to - exactly. But I get yelled at about it.
John's Debugging Applications for Microsoft .NET and Microsoft Windows, which is a MUST READ, has cured me of more bad habits than I care to mention. Furthermore, he too was caught a lot of crap about using Assertions - although the manager that chewed him out sounds like so much of a dumb a33 that he gave John a lot to laugh back about. Paul quotes Dave on page 617 of His Book- “ASSERT THE WORLD”. But the part that really confuses me is that I've gotten crap from REALLY GOOD DEVELOPERS - DEVELOPERS WHO ARE UNIT TEST LOVERS - DEVELOPERS WHO HATE BUGS AND DO ANYTHING TO (except using assertions) TO ELIMINATE THEM.
How can this be? Well, I'll sum up the arguments I've heard against assertions - and CRUSH THEM. I couldn't convince them when I heard their arguments, but in this instance, it's not because I had the weaker argument. It's that they are WRONG, WRONG, WRONG. And I'm not trying to belittle any of the folks that I had these discussions with - on the contrary, I have the utmost of professional respect for each of them. But we're all wrong sometimes, and they are wrong on this one. The problem is that it's the ANTI-Assertion crowd that seems to be swaying popular opinion - at least the part that they are useless.
1) Assertions clutter code - True, but so do comments if you define stuff that isn't code as clutter, particularly XML Comments. Anyone want to argue that Comments are bad “Because they clutter code”? No. So how doesthis logically invalid argument apply here? Yes, if they cluttered code and added NO VALUE, or Less value than they provided, this argument would be valid - but it would have to be changed to “They clutter the code and provide no value”. But that would be preposterous. (Technically, I guess it would be that they cost morein clutter than the value they provided, but that's not the case either). Assertions not onlyADD Tons of value, they make code more CLEAR. Yes, I've been laughed at over this.This was the “silliest thing” someone ever heard. Oh really? Whatmakes my code easier to read, the fact that I assert SomeObject.Value gt; 100 or “Thismethod willcall the lt;see cref=”SomeObject”/gt; class' lt;igt;Whateverlt;/gt; method and return the sum of the two parameters? Now, what if I assert that both of the parameters are greater than 0? My three assertions will help a new developer understand what's going onin my method1,000 times more than lt;see cref=”DumbArgument”/gt;any day of the week.They only way they wouldn't is if the developer didn't know what an assertion was - but the learning curve on Assertions is 15 seconds, sothe cost of them learning is nothing - even if you think that they should never bother using them. YES, THEY ADD CLARITY. Not to mention that they will cause problems - forcing someone to look at them. If the logic changed, they will notify you ASAP because they'll blow up. All the/// In the world won't NOTIFY THE Developer that they are out of date.If I'm wrong here, please tell me where. So let's just take this right off the table becauseat best it's an ignorant argument - unless of course you are anti-comment - but in my case, none of the people I ran across were. And does anyone want to tell me that comments never get out of date and become more problematic than no comments - if they do? I've spent well over a week working on stuff in my career because of outdated comments that lead me down the wrong road. An assertino wouldn't do this. I'd immediately ask “Hey, I passed in 100 to this method and it blew up. but the spec says it can be anything from 0 to 1,000 - am I missing something?“ Then I get told, “No, 100 is legit, it's when it's over a 1,000 - the spec changed recently“ Then I add a 0 to the assertion and all is well.
2) They break code - True, but so does bad logic.They do however make sure you get the memo - bad code may or may not. Spare me the lectures on logging - because I'm pro logging. Seriously, just this morning I saw an Assertion and a Trace Logger playing together. They were having a lot of fun. No animosity whatsoever. If they break code, it's either because they are broken and should be fixed (and they are nice enough to inform you that there's aproblem) or your Code is.
3) We write Unit Tests so we don't need them. By that logic, I guess we shouldn't have airbags in cars, or have them activated because we wear seatbelts. Bugs suck. They cost money and inconvenience people. The more protection you have the better. AND YES, NEWSFLASH - you can use Both. Hey, what do you do in NUNIT? ASSERT? Wow, so it's cool in NUNIT - but not in code. Sure, if you write perfect unit tests all the time, and always update them ASAP, then you may have some tiny little point. But the developer that ALWAYS writes Perfect unit tests, covers all of his code, and always updates his Unit tests immediately - well, he/she lives right next to the Easter Bunny. Why are there so many code coverage tools if everyone's so f****** good at this?
But there's more. Unit tests are only as good as the logic that they are based on. I can write total sh1t code, and have all sorts of unit tests that don't bark. I shouldn't - if I write them right, but just about everyone I know makes mistakes occassionally. I make them every day. Let Ye Without an Imperfect Unit Test cast the first stone. Assertions are used in unit tests, but the DELIVERABLE is the code, not the Unit Tests. Unit Test should make the deliverable better - I'm all about Unit tests- but they aren't the deliverable. If all your unit tests pass but your code blows up - well, all that means is that you write sh1ttier unit tests than you do code. There are books written&about writing GOOD unit tests, entire blogs&about writing good unit tests - how can this be if it's so easy to write perfect ones?
Understand - I TOTALLY ADVOCATE UNIT TESTS - I just think that Assertions are cool too.
4) Logging negates the need for those 'things' - Really. Do Seatbelts negate the need for breaks? Logging is done ex post facto. Plus, you have to actually examine the log - oh - yeah, right, you can email. Well, that would probably cause a situation more annoying than spam (emailing all code failures). On the other hand, let's just assume for one second, that one developer in your organization is a d1ckhead - who hides his mistakes. My Homie Phil can verify their existence, and I'm sure my homies can back me up - such people do exist. So you're a responsible Anti-Assertion program lead and you have something bad happen, maybe a car wreck and injury, loved on sick or dying, something bad, which takes you out of the game for a week right before release. And I'm joe d1ckhead that doesn't like acknowledging my problems. While you're out - who's going to read those logs? Hmmm? Any mechanism you can come up with to negate my passive aggressive bs can be worked around. If you email everyone - the email server could be down - people could filter them out because they get so many of them, people could automatically move them to a folder because of the volume (which they don't check) [Yeah, this never happens].
But screw pragmatism for a second - Logging/Tracing is ex post facto - I have to either check the log or have it pushed to me. Period. Ever remoted stuff? The debugger can work pretty well most of the time, but it's not perfect. Even if it was - I can ignore what just happened. Maybe the testers don't have access to the server. Who knows? But one thing I do know is that having a BIG UGLY ASSERTION BOX RIGHT UP IN YOUR FACE, ONE THE DELIVERABLE - that's the least likely thing there is to go unnoticed. And just like with unit tests, We can do BOTH! And I'm all about doing both.
BUGS ARE EXPENSIVE AND EMBARASSING. And they set the tone for your customer relationships. If the install and initial engagement is flawless, customers are more likely to attribute bugs to something on their end. But let the install blow up in 20 different ways and take 8 hours longer than it should have - YOu'll never convince them - no matter what they do - that they are the cause of the problem. But f*ck that - they paid you for something that worked. Ok, a few bugs are tolerable - but I've used a bunch of stuff that I've never found a bug in. And I bought version++ of damn near all of them. But screw all that pride and customer focus stuff - let's take it to greed. It takes 3 seconds tops for most assertions to fail. It usually takes less than a few minutes to find out why and fix the problem. F*ck it though, assume it takes a day. It's still going to be cheaper than the ass kissing you're going to have to do if they problem hides until it gets to the customer.
HAVE I MADE MY POINT THOUGH? Please, I totally welcome contrary opinions. Please, bring them on. If I'm wrong, fine. But I'm not. I promise, I'll address every argument anyone wants to present without calling John and telling him I live 4 miles from the BMW Plant, and I'll buy him a new BMW Bike if he Asserts your ass out of a window (Actually, John Robbins is probably one of the nicest people you'll run across - this is just me being silly) and then HALO jumps off the roof and shoots you before you hit the ground. He probably could do it too.