views:

84

answers:

8

In other words, do you spend time anticipating errors and writing code to get around these potential issues, or do you write the code as you see fit and then work through any errors on an issue by issue basis?

I've been thinking a lot about this lately and I'm very much a reactive person. I write my code, give it a whirl, go back correct error and repeat until application works as expected. However a friend of mine offered that he spends time thinking how each line is interpreted and fixes errors before they occur.

I must point out that re-active is pure PRE-live. I definitely make sure my application is working before it goes live.

+4  A: 

There should always be a balance.

Too many error checking is slow and leads to garbage code. Not enough error checking makes your program crash on edge cases which is not very good to discover after having it shipped.

So you decide how reliable some piece of code should be and implement error checking accordingly. Some test utility can be not very reliable - less error checking. A COM server meant to be used by a third party search service in deep background should be super reliable - much more error checking.

sharptooth
my friend challenged me to code-off (because that's how cool we are) and we had to write some PHP to read data from a database and and place it into a form... almost like a reverse database insert. He finished before me surprisingly, but my code was around thirty lines long and his was 84 lines. He'd included validation and escape strings etc. regardless of the fact that this was just a quick exercise
Daniel Hanly
A: 

I think asking this in isolation is kinda weird, and very subjective, however there are obviously a bunch of techniques that permit you to do each. I tend to use these two:

  • Test-driven development (this would seem to be proactive)
  • Strong, static typing (reactive, but part of a tight iterative development cycle, as in, it's enforced by my ML compiler, and I compile a lot)

Very occasionally I swerve into the world of formal verification of programs. That's definitely "reactive", but if you think a little more up-front, it tends to make the verification easier.

I must also say that I value a lot of up-front thought in programming. The easiest way to avoid bugs is to not write them in the first place. Sometimes it's inevitable, but often a little more time spent thinking about the problem can lead to better-quality solutions, and then the rest can be taken care of using the kinds of automated methods I talked about above.

Gian
but using a reactive approach to programming can slowly but surely give you the experience to avoid bugs in the first place, i.e. if I use Java a lot, I'll gain an affinity for what works and what doesn't
Daniel Hanly
That's true: there's no substitute for experience, but whether you discover bugs by debugging or by having some tool tell you that there is something wrong, it's a learning experience either way.
Gian
A: 

I usually ask myself a bunch of what-ifs when coding, like

  • The user clicks the button, what if they didn't select a date?
  • The user is typing in the search box, what if they try to type html in there?
  • My label text depends on a value from a shared drive, what if it's not mapped?

and so on. By doing this I've found that when the application does go live, there are a ton fewer errors and I can focus on fixing more obscure bugs instead of correcting conditions that should have been in place to begin with.

somacore
so by this you are pro-active. Whereas I will build my page, then visit it and actively try and break it; in doing so thinking up solutions for how I can guard against what I have done (with great deal of help from the lovely users of SO of course :))
Daniel Hanly
A: 

I live by a simple principle when considering error-handling: garbage in, garbage out. If you don't want any garbage (e.g. invalid input) messing up your software, you have to find all the points in your software where it can get in and handle it. Of course, the more complicated your software is, the harder it is to find every point of entry, but I feel that the more you do up front the less reactive you will need to be later on.

Bernard
A: 

I advocate the proactive approach.

  • I try to write the code in that style which results in maintainable and reliable code
  • I use the defensive programming techniques to prevent stupid errors in code due to my loss of attention and similar
  • I design the database model according to the fortress principle, SQL code checking for results after each singular operation
  • I think of potential problems that can happen with that part of the code and I account for that. Not for every possibility but for major ones I can think of right now.

This usually results in software operating rather smoothly. At times it even surprises me but that was the intended goal, so here we are.

Developer Art
A: 

Basic View:

Attempt to capture what should be a reasonable level of errors but ensure that your code fails gracefully and logs the issue.

Longer Spiel

I am proactive but use asserts liberally so that the performance hit is not quite that strong in a production environment unless we have a reasonable expectation that errors will occur on a non-exceptional basis. That being said, if I spend all my time anticipating errors it slows down my work, sometimes to the point where there is little hope of meeting whatever deadline the customer came up with. I just am not smart enough to see every single little item that could go wrong in a large project. There are too many moving parts.

Creating robust software that fails gracefully is probably your biggest concern, rather than how much effort you spend with error handling in the beginning. I've found that regardless of how I work a piece of software, someone, somewhere will do something that I didn't expect or could even anticipate. Therefore, I weigh fail gracefully but log it above all else. Once that happens I can be reactive, investigate the issue, and discover what other things might go wrong.

wheaties
A: 

IMHO, the word "Error" (or its loose synonym "bug") itself means that it is a program behavior that was not foreseen.

I usually try to design with all possible scenarios in mind. Of course, it is usually not possible to think of all possible cases. But thinking through and allowing for as many scenarios as possible is usually better than just getting something working as soon as possible. This saves a lot of time and effort debugging and redesigning the code. I often sit down with pen and paper for even the smallest of programing tasks before actually typing any code into my editor.

As I said, this will not eliminate all errors. For me it pays off many times over in terms of time spent debugging. Another benefit is that it results in a more solid and maintainable design with fewer bugfixing hacks and special cases added on later. But in any case, you will have to do a lot of debugging after the code is done.

This does not apply when all you want is a mockup or rapid prototype. Also practical constraints such as deadlines often makes a thorough evaluation difficult or impossible.

MAK
A: 

What kind of programming? It's impossible to answer this in any general way. (It's like asking "do you wear a helmet when playing?" -- well, playing what?)

At work, I'm working on a database-backed website. The requirements are strict, and if I don't anticipate how users will screw it up, I'm going to get a call at some odd hour of the day to fix it.

At home, I'm working on a program ... I don't even know what it'll do yet. I can't deal with 'errors' because I don't know what 'an error' is in this context, because I don't know what correct behavior is going to be. The entire purpose of the program can and frequently does change on a timescale of minutes to hours, so even a couple minutes spent thinking about errors this early is a complete waste of time. (It's even worse than browsing SO, since error-handling adds lines of code.)

I guess the only general answer is "I do what makes sense in terms of saving time in the long term", which is, after all, the whole reason to use machines to do work for us.

Ken