I was having a discussion with my coworker about a function that returns either an empty string, or a string of a css class. The function is passed an object which has an HTTP status code, and then depending on that statusCode, it returns a value. We couldn't agree which is cleaner.
He wanted the function written as so:
function getCssClass(options) {
var statusCodeCssClass = '';
var statusCode = parseInt(options.statuscode, 10);
if ((statusCode >= 100 && statusCode < 200) || statusCode >= 400) {
statusCodeCssClass = 'text-danger';
}
return statusCodeCssClass;
};
And I wanted it written as so:
function getCssClass(options) {
var statusCode = parseInt(options.statuscode, 10);
if ((statusCode >= 100 && statusCode < 200) || statusCode >= 400) {
return 'text-danger';
}
return '';
};
The main difference is that I don't think the initial empty variable should be there, and he does. His argument is that the variable makes it easier to read, and my argument is that the variable obfuscates the function.
I know it's splitting hairs, but I was unable to find an answer in the Clean Code book or on Google about unnecessary variable declarations.
Can someone tell me why one is better than the other?