Link to home
Start Free TrialLog in
Avatar of ltpitt
ltpitt

asked on

Optimize simple Javascript code to use no repetitions

I have this working code:

function setvar(name, value) {
  if (this.hasOwnProperty(name)) { //typos happen
    this[name] = value;
    myOtherFunction.setValue(value);  
  } else {
  }
}

if (Variable1 == "TBD")    {
    setvar("StdErr",StdErr.concat("\nValidation for Variable1 failed, no valid value found\n");
    setvar("ComponentState","Failed");
}
if (Variable2 == "TBD") {
    setvar("StdErr",StdErr.concat("\nValidation for Variable2 failed, no valid value found\n");
    setvar("ComponentState","Failed");
}
if (Variable3 == "TBD")  {
    setvar("StdErr",StdErr.concat("\nValidation for Variable3 failed, no valid value found\n");
    setvar("ComponentState","Failed");
}
if (Variable4 == "TBD")  {
    setvar("StdErr",StdErr.concat("\nValidation for Variable4 failed, no valid value found\n");
    setvar("ComponentState","Failed");
} 

Open in new window


I would really like to remove repeated code, how can I do it?
Avatar of Mukesh Yadav
Mukesh Yadav
Flag of India image

Can you please explain this piece of code in bit detail what it's doing?

function setvar(name, value) {
  if (this.hasOwnProperty(name)) { //typos happen
    this[name] = value;
    myOtherFunction.setValue(value);  
  } else {
  }
}

Open in new window

Avatar of ltpitt
ltpitt

ASKER

That part can be simply ignored.

Consider it as a function that needs to becalled all of the times to update a variable.

It is not changing the code you see because it is just a function receiving data: it is not returning anything.
Try this ;)

Create an array of variables you want to check:

variables = [Variable1, Variable2, Variable3, Variable4];
for(var i = 0, len = variables.length; i < len; i++){
  if(variables[i] == 'TBD'){
    setvar("StdErr", StdErr.concat("\nValidation for Variable" + (i + 1) + " failed, no valid value found\n");
    setvar("ComponentState", "Failed");
  }
}

Open in new window


By this way you can check as many variables you want to check;
try:

function setvar(name, value) {
  if (this.hasOwnProperty(name)) { //typos happen
    this[name] = value;
    myOtherFunction.setValue(value);  
  } else {
  }
}

var msg = '';
if (Variable1 == "TBD")    {
    msg = "\nValidation for Variable1 failed, no valid value found\n";
}
if (Variable2 == "TBD") {
    msg = "\nValidation for Variable2 failed, no valid value found\n";
}
if (Variable3 == "TBD")  {
    msg = "\nValidation for Variable3 failed, no valid value found\n";
}
if (Variable4 == "TBD")  {
    msg = "\nValidation for Variable4 failed, no valid value found\n";
} 

if( msg != '' ) {
    setvar("StdErr",StdErr.concat( msg );
    setvar("ComponentState","Failed");
}

Open in new window

Avatar of ltpitt

ASKER

I've written a more efficient solution:

// Here I create an hashmap containing all the values
var obj = {};
obj.Conn_IP = Conn_IP;
obj.Conn_IPmon = Conn_IPmon;
obj.Conn_Pass = Conn_Pass;
obj.Conn_User = Conn_User;

// I use a loop to remove code redundancy and use both key and value from the hashmap
for(key in obj)
    if (obj[key] == 'TBD') {
        StdErr += "\nValidation for " + key + " failed, no valid value found\n"
        ComponentState = "Failed";
    }

Open in new window

Try this ;)

Removed repetition:

var obj = {
  Conn_IP,
  Conn_IPmon,
  Conn_Pass,
  Conn_User
};

// I use a loop to remove code redundancy and use both key and value from the hashmap
for(key in obj)
  if (obj[key] == 'TBD') {
    StdErr += "\nValidation for " + key + " failed, no valid value found\n"
    ComponentState = "Failed";
  }
}

Open in new window

Avatar of ltpitt

ASKER

Hi @Mukesh,

I tried your code but it seems like it is not working (changed my code slightly to implement it):

// Component variables
Conn_IP = 'TBD';
Conn_IPmon = 'TBD';
Conn_Pass = 'TBD';
Conn_User = 'TBD';

// Error variables
ComponentState = "Working";
StdErr = "";

// Here I create an hashmap containing all the values
var obj = {
  Conn_IP,
  Conn_IPmon,
  Conn_Pass,
  Conn_User
};

// I use a loop to remove code redundancy and use both key and value from the hashmap
for(key in obj)
  if (obj[key] == 'TBD') {
    StdErr += "\nValidation for " + key + " failed, no valid value found\n"
    ComponentState = "Failed";
  }
}

// Printing error variables to console for testing
console.log("Final ComponentState: " + ComponentState);
console.log("Final StdErr: " + StdErr);

Open in new window

SOLUTION
Avatar of ltpitt
ltpitt

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
When you are creating object properties same as a variable name then you can only pass variable name, it will add a property and will assign that variable value to property. Like:

var obj = {
  Conn_IP,
  Conn_IPmon,
  Conn_Pass,
  Conn_User
};

Open in new window

Avatar of ltpitt

ASKER

Can you please post your full tested code?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ltpitt

ASKER

Now I understand!

It is a beauty, very well done :)