Code Style & Taste
A blog where I share thoughts on code and practices

Do You Declare Many Variables?

I'm at an 8.5/10, I'll tell you why.

My call sites tend to look like

funcA(obj.var + var, anotherCall())

While debugging if I wanted to know what values were passed into a function, it would be a struggle to figure it out by looking at the call site. Instead, I like to preserve the non-object parameters passed into each of my functions.

int get(int position, Opt options) { int i = position; // write code with 'i'
//'i' is in the signature and I don't want to change the signature
int get(int i, Opt options) { int origI = i;

It's not unusual for me to write complex if statements, or an if statements that you don't see every day. I'll use a variable to make it more obvious.

var isComment = text.startsWith("//") || text.startsWith("#")
if (isComment) {

But I don't do it everywhere, this is self-explanatory

if (v >= 'a' && v <= 'f')

Other times I'll do it because the conditional might be long.

var c = letter | 32; //this is a common trick to lowercase ascii A-Z without breaking 0-9/a-z
//Although it may break separators like '_'
var isHex = (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || letter == '_'
if (isHex) //...

Sometimes a person needs to check error values

var letter = getCharFromHex(val & 15)
if (letter == -1) { handle_error(); }
doSomething(letter)

letter = getCharFromHex(val >> 4)
if (letter == -1) { handle_error(); }
doSomething(letter)

I almost never write it that way, I'd write it like this

var letter = getCharFromHex(val & 15)
if (letter == -1) { handle_error(); }
var res = doSomething(letter)

var letter2 = getCharFromHex(val >> 4)
if (letter2 == -1) { handle_error(); }
var res2 = doSomething(letter2)

Now if I'm debugging something after the second letter, I can easily see the value of the first or if that too is an error. In more complicated code I may write

int res1, res2;
{
	var letter = getCharFromHex(val & 15)
	if (letter == -1) { handle_error(); }
	var res = doSomething(letter)
	res1 = doMore(res, letter)
}
{
	var letter = getCharFromHex(val >> 4)
	if (letter == -1) { handle_error(); }
	var res = doSomething(letter)
	res2 = doAnother(res)
}

Usually, if I do this, the code is long (10+ lines each block) and or similar enough that I may mix up variables. Here I named the results res1 and res2 instead of res/res2, this is so I don't mistype the variable or forget that there are two. Using "letter" in previous examples is not a mistake, if a function is <10 lines it's hard not to notice letter2.

Sometimes I'm debugging a complex function. I'll write the below and never use the variables, this is so I can easily see the values in a debugger

var a = obj1.var1 //I don't need to expand obj and lose screen space
var b = obj1.var2 + obj1.var3 - 1234 //so I don't need to type this formula out in the watch window
var c = obj2.fn() //so I don't need to call this in the debugger repl
var d = fn(anotherParam + var) //can be the same reason as b or c

Yes, languages that error on unused variables drives me crazy.

Do you declare many variables?