Skip to main content
Disposition_Dev
Legend
March 2, 2017
Pregunta

Your opinion on best practices.

  • March 2, 2017
  • 1 respuesta
  • 1448 visualizaciones

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?

Este tema ha sido cerrado para respuestas.

1 respuesta

Silly-V
Legend
March 2, 2017

This is a great question! It is useful sometimes to have functions which work on an input to produce an output only, and other times some which change outside-scoped variables, and any mix of those.

With a method of only processing output of functions to consider how successful those functions are, you don't have the brain-racking that comes with keeping track of the global variable names, and you will likely know the exact cause of your error because you'll be alerted to the failing line which would correspond with a specific function at hand.

If you change global variables and keep a restricted name list for yourself so that you don't use them in other parts of your code for other things, it may be just as effective, I'm not sure. I do think that changing outside vars is what you want to do when you're logging outcomes of some processes which do not require you to halt the script, such as try-catch blocks.

Disposition_Dev
Legend
March 3, 2017

Well said. that's good advice.

The frustrating part about all of this is that most of the time I'm checking for errors that shouldn't exist in the first place. For example, if i have a function that simply looks for an existing layer, creates the layer if it doesn't exist, then saves that layer to a variable.

That kind of function should NEVER give me an error. but because of the unpredictable MRAP errors, i have to assume that any line of code can fail for any reason and that's turned my scripts into a complete rats nest of very short try/catch blocks because it's the only way i can produce meaningful error messages...

Quite frustrating.

Silly-V
Legend
March 3, 2017

So you get MRAPs with just trying to add layers in? Say, are you guys all on CS6 still? I've gotten PARMs but not yet for going over layers, but I've been on CC for the last couple of years now.