Lazy Developers?

When I was out with my new dog (I Chihuahua, wonderful dog), I was thinking about Defensive Programming and Design by Contract. I like to do some refactoring and help other people to write cleaner code etc,  not that I’m an expert in the area but I think most of developers can see stuff that blinds other developers during development. I remember a scenario that made me kind of mad, I often notice that some developers doesn’t even care to validate arguments on public API or other methods, they assume the caller pass in the right values. When I started to develop apps, I didn’t validate arguments (In know I was a noob ;)). But since I started to do validation, it reduces the number of bugs in my code, and the difference was big. But that isn’t the only reason I like to do validation. Another  reason is because the caller of the method should know if they have passed a wrong value, there are of course some other reasons also, but the main reason is make sure the caller or the method pass in the correct value, and if they do, they will get what they want.

I notice that several developers today don’t care about validation, I ask them why and they all gave me the same argument “Why should I, it will only make me add more code and it’s boring to write code that checks values”. Can this be summarized that some developers are lazy?

Maybe I’m stupid because I get mad when I hear that kind of argument, but I’m special ;)

Published Wednesday, May 07, 2008 8:29 PM by Fredrik N

Comments

# re: Lazy Developers?

Wednesday, May 07, 2008 2:56 PM by Brian Khhn

You are not stupid, that kind of 'argument' not only reflects a lazy attitude, but also is disregarding the fact that adding appropriate validation will in the long run result in developers having to write less code; as validation is often a good defense against introducing bugs that inevitably will have to be fixed.

# re: Lazy Developers?

Wednesday, May 07, 2008 4:15 PM by persa

I think one big reason for this lazy thing is that most people learn from reading other peoples code and if validation is missing from most of the code samples it will not get through to the readers...

# re: Lazy Developers?

Wednesday, May 07, 2008 6:05 PM by BennyXNO

A former boss me told me that the reason I was better programmer then most she knew was that I where lazy, and tried to get the best code as fast as possible. I didn't agree with her, because I use a lot of time on refactoring my code until I have a certain degree of satisfaction.

I believe that the reason that people tend to jump over the validation stuff is because the tools don't support validation out of the box.

# re: Lazy Developers?

Wednesday, May 07, 2008 6:41 PM by Steven

I often used the 'less code is better' argument, but it's actually a bad argument. You shouldn't write the least code as possible; you should write the most maintainable code as possible! When I look back at my ‘less code is better’ period, I see sometimes obfuscated one-liners, that are hard to read, hard to maintain. The people who skip validation, possibly believe in the ‘less code is better’ philosophy.

Writing validation leads to more maintainable code, because after reading the first few lines of a function you know exactly what the preconditions are. Besides that, not validating your public (and protected) members complicates your API and often creates a debugging hell. How many times did some external code throw a NullReferenceException in your face from deep down the stack? Sometimes it's like finding a needle in a haystack.

B.t.w. I’m a lazy programmer too, so I created code snippets that allow me to insert these lines of validation very quickly. And otherwise I think tools like Resharper can help you on that, so laziness can never be a good reason to skip validation.

# re: Lazy Developers?

Wednesday, May 07, 2008 7:23 PM by Stevi Deter

I may be lazy, but I am also impatient. Which is why I'm a huge advocate of fail-fast programming, including always checking your parameters before you bother doing anything else.

# re: Lazy Developers?

Thursday, May 08, 2008 2:50 AM by Torkel

Some developers are lazy, no doubt about it (I can be that too sometimes). What bothers me is how the laziness is often excused with arguments like "there was not enough time".

For argument validation I usually use a utility class, for example:

Guard.ArgumentNotNull("type", type);

# re: Lazy Developers?

Thursday, May 08, 2008 4:15 AM by Lazydev

Its not lazyness that makes the dev implement validation, its stupidity/ignorance/lack of competence.

Its like not paying the bills, its very boring but the consequenses of not doing it is pretty bad.

# How to validate a method's arguments?

Thursday, May 08, 2008 4:19 AM by Fredrik Normén

Yesterday I wrote a post about developers that skip validation of arguments . In this post I will give

# How to validate a method's arguments?

Thursday, May 08, 2008 4:36 AM by Cornerstones utvecklarblogg

Yesterday I wrote a post about developers that skip validation of arguments . In this post I will give

# re: Lazy Developers?

Friday, May 09, 2008 2:49 PM by shahar

I am certainly validating arguments on public methods. Do you validate on each and every method you add to your code? even the mst inner private methods?

# re: Lazy Developers?

Friday, May 09, 2008 3:47 PM by Fredrik N

Shahar:

The most important part is the public methods which someone can call; most of my private methods will be created during refactoring. When a private methods is called from another method. The method which call the private method may already have done the validation of the arguments, so in that case the private method don’t need to do the validation.

To use validation on every single method including private methods etc can affect performance, so it’s not good to overuse validations.

# Use constraints instead of argument validation

Saturday, May 10, 2008 7:21 AM by Fredrik Normén

I will still continue with the argument validation track in this post also, I think it's an interesting

# Use "constraints" instead of argument validation for int and double etc

Saturday, May 10, 2008 7:26 AM by Cornerstones utvecklarblogg

I will still continue with the argument validation track in this post also, I think it's an interesting

# ???????????? (2008-05-20) [JeffHung.Blog]

Tuesday, May 20, 2008 11:31 AM by ???????????? (2008-05-20) [JeffHung.Blog]

Pingback from  ???????????? (2008-05-20)   [JeffHung.Blog]

Leave a Comment

(required) 
(required) 
(optional)
(required)