Tech Is Hard

Credibility = Talent x Years of experience + Proven hardcore accomplishment

Where to Declare


I think it’s common practice to declare (and sometimes initialize) all of a function’s local variables “at the top”, but I usually declare them close to where they’re referenced. The result of declaring at the top can lead to what I call functional striation and makes refactoring more difficult.

An example:

function innocent_looking_foo() {
    var A, B, C;
	
    do (something with A)
	
    do (something with B)
	
    do (something with C)
	
    return
}

Right from the outset, it looks like this function may not be very cohesive, but the truth is, one sees a lot of code that looks like this in the real world. Let’s say someone adds D and ‘somethingelse’ now has to be done with a few of the variables:

function innocent_looking_foo() {
    var A, B, C, D;
	
    do (something with A)
	
    do (something with B)
    do (somethingelse with B)
	
    do (something with C)

    do (something with D)
    do (somethingelse with D)
	
    return
}

or worse(?)

function innocent_looking_foo() {
    var A, B, C, D;
	
    do (something with A)
    do (something with B)
    do (something with C)
    do (something with D)

    do (somethingelse with B)
    do (somethingelse with D)
	
    return
}

In some shops, this kind of growth continues for years until no one remembers if we really need to do each thing to what. And we have people who add “do (anewthing with D)” in between A and B. Remember that the “do” pseudo statements are usually represented by more than one line of code in our function. If I am working on any part of this, I have to examine the preceding code, all the way to the top, for references to the variable I’m concerned with. there may be dozens or, lets be honest, hundreds of lines where a variable can be corrupted. There is more chance that the order of execution matters, which is bad if we can avoid it.

Now read this:

function foo() {
    var A
    do (something with A)
	
    var B
    do (something with B)
    do (somethingelse with B)
	
    var C
    do (something with C)
	
    var D
    do (anewthing with D)
    do (something with D)
    do (somethingelse with D)	
	
    return
}

This sort of aligns the code with the variables it’s doing work to, and would be easier to refactor as it gets more complex. It’s easier to see repeating patterns and turn them into callable functions.

This guideline is probably even more applicable to variable initialization, irrespective of where it’s declared. Try not to separate the initialization from the first reference.

Advertisements

One response to “Where to Declare

  1. Pingback: Populate a PHP Array in One Assignment « Tech Is Hard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: