views:

52

answers:

2

When writing a function I always have this confusion whether to check for errors first and declare the variables later (or) assign the parameters to local variables and then check for errors. Which of the following way is preferred and why? I usually stick to the first type.

void DoSomething1(Object x, Object y){

  // All sort of error checking goes here
  if IsError(x) return;
  if IsError(y) return;

  // Variable declaration
  int i,j;
  Object z = x;
}


void DoSomething2(Object x, Object y){

  // Variable declaration
  int i,j;
  Object z = x;

  // All sort of error checking goes here
  if IsError(z) return;
  if IsError(y) return;

}
+6  A: 

You should follow a proximity rule and declare the variables as late as possible. This localises their creation and use. You should also check parameters for validity at the earliest possible opportunity to minimise the work performed.

Hence I agree that your first one is better but it is subjective. There's possibly arguments for the other approach but I've yet to hear convincing ones, so I consider those two guidelines as best practice.

Since you state "language agnostic" despite the fact your code looks somehow strangely familiar :-), there are almost certainly some languages where you don't get a choice and variables have to be declared at the top.

paxdiablo
Any cases when the first one is not better than the second one? Ofcouse, not taking into account language specific limitations.
Ravi Gummadi
+1 for mentioning the proximity rule. It's a bloody pain to have to scroll to the top of a function just to understand what `t` is declared as. But like you said, for some languages, one doesn't have a choice.
Noufal Ibrahim
@Ravi, honestly, I can't think of any. That doesn't mean there _aren't_ any. I'm good, but I'm not a deity :-)
paxdiablo
@paxdiablo: Got it! But I have seen way many too programmers who just stick to second type, so I was wondering if there is indeed a strong reason for this. And also why some languages enforce this rule: is it just how the grammar is defined?
Ravi Gummadi
@Ravi: Suppose the initialization of the variable is very costly. Now suppose the function is designed to wait for a signal from another thread, and as soon as the signal comes extract some information from the data structure (which may be very cheap) and pass it somewhere. In that case it might make sense to initialize the variable before starting the wait for the signal, so the processing after the signal was received is faster.
Space_C0wb0y
@Cowboy: Awesome! You should post this as an answer. Thank you.
Ravi Gummadi
@Noufal: "It's a bloody pain to have to scroll to the top of a function" - for any reason. Which is why we write short functions ;-p
Steve Jessop
Steve Jessop : *We* do. *They* don't. But often times, *We* have to fix *Their* code. :-|
Noufal Ibrahim
+1  A: 

Declare variables when you need them, that's usually when some intermediate result is ready or when you're just about to enter a loop.

So this does imply that error checks will often come before declarations.

djna