Your opinion on best practices.
Over the 2 and a half years I've been writing javascript, I've changed my practices time and time again as I've learned new techniques or encountered unforeseen problems with other techniques.
At this point, I'm essentially writing everything as small function modules and then doing "running flag" (term coined by Silly-V) validation checking like so:
function doSomething(input)
{
//do something
}
function doSomethingElse(input)
{
//do something else}
}
var valid;
if(valid)
{
doSomething(input)
}
if(valid)
{
doSomethingElse(input)
}
The question I want to ask of you knowledgeable folks is whether you think it's better to do the validation within the function and always return a boolean and just redefine global scope variables (or object properties) from inside the function like so:
function doSomething(input)
{
var localValid = true;
var value;
//do something
//get value
if(value != undefined)
{
globalObject.property = value;
}
else
{
//log some error
localValid = false;
}
return localValid;
}
var globalObj = {};
var valid = true;
if(valid)
{
valid = doSomething(input);
}
if(valid)
{
valid = doSomethingElse(input);
}
or return a value from the function and do the validation after the function has returned the value, like so:
function doSomething(input)
{
var value;
//do something
//get the value
return value;
}
var globalObj = {};
var valid = true;
if(valid)
{
globalObj.property = doSomething(input);
if(globalObj.property != "correct value")
{
valid = false;
}
}
if(valid)
{
globalObj.otherProperty = doSomethingElse(input);
if(globalObj.otherProperty != "correct format")
{
valid = false;
}
}
I'm sure the correct answer is "it depends". If that's the case, do you have certain desired return data types that you do one way, and others you do another way?
