Re: Unit tests and parameter validation
Colin Vipurs <[email protected]>
| Newsgroups | gmane.comp.programming.test-driven-development |
|---|---|
| Message-ID | <CAKi4272swG9cY3zOPUYjD5AAhTDJjd2vb6KrftbOLJSrO1YeWA@mail.gmail.com> |
I believe it was this advice (from Ron) that I read many, many years ago (fairly sure it was on this list too) that led me to drop doing defensive coding and I've never looked back. Not only does it lead to less code overall but opened me up to focussing on contracts and ensuring they are adhered to. On Wed, Oct 16, 2013 at 12:42 AM, Ron Jeffries <[email protected]> wrote: > ** > > > Eb, > > On Oct 15, 2013, at 12:03 PM, Eb <[email protected]> wrote: > > I'm curious to know thoughts on writing tests that specify the behavior of > a method when its inputs are incorrect. Is this a common practice. How > about parameter validation in this case? Having some healthy debate on it > and wanted to get other insight. > > The context for this in a class/methods that are used within an internal > application - this is not an API that will be publicly exposed to external > consumers. > > > Generally speaking, I do not check arguments for correctness. If I am > building the entire app via TDD, the code that calls the method is already > tested, and therefore in fact calls with correct arguments. > > If the method in question is potentially harmful, then I will be more > inclined to cause it to protect itself, and I would generally test that. > > This approach generally works for me. My experience is that "defensive" > code generally makes the program much larger, increases its complexity, and > since the defensive code is both arcane and hard to test, it usually > embodies a disproportionate number of defects. > > So I focus on writing correct code, not code that lives in fear of its > neighbors. > > Ron Jeffries > www.XProgramming.com <http://www.xprogramming.com> > I have two cats, and a big house full of cat stuff. > The cats fight and divide up the house, messing up their own lives. > Nice work cats. > Meow. > > > -- Maybe she awoke to see the roommate's boyfriend swinging from the chandelier wearing a boar's head. Something which you, I, and everyone else would call "Tuesday", of course.