Alternative to a Million If Statements

Alternative to a million IF statements

A switch statement, as your code is only if-elses :-)

No, honestly. The best thing would be if you'd find a simple algorithm to create an email address from any given name, like

function mail(name) {
return name.toLowerCase() + "@gmail.com";
}
var email = mail("Bob") // example usage

If they differ to much, you might use an object as a key-value-map:

var mails = {
"Steve": "steve@gmail.com",
"Bob": "bob1@freemail.org",
...
}
var email = mails[name];

You could also combine those, if you have to determine which algorithm you need to use:

var map = [{
algorithm: function(name) { return name+"@something"; },
names: ["Steve", "Bob", ...]
},{
algorithm: function(name) { return "info@"+name+".org"; },
names: ["Mark", ...]
}];
for (var i=0; i<map.length; i++)
if (map[i].names.indexOf(name) > -1) {
var email = map[i].algorithm(name);
break;
}

or when it is a bit simpler:

var domains = {
"gmail.com": ["Steve", "Bob", ...],
"free.xxx": ["Mark", ...],
...
};
for (var domain in domains)
if (domains[domain].indexOf(name) > -1)
var email = name.toLowerCase()+"@"+domain;
break;
}

Just try to reduce the amount of data to deliver to the client as much as you can.

Python if statement efficiency

if statements will skip everything in an else bracket if it evaluates to true. It should be noted that worrying about this sort of problem, unless it's done millions of times per program execution, is called "premature optimization" and should be avoided. If your code is clearer with three if (a and b and c) statements, they should be left in.

What is best way to refactor these series of if statements?

I ended up refactoring it with switch statements and functions. I'm sure there are more elegant ways I could do it. Will update answer if I found a way.

Dealing with big IF statements in PHP

If you want to improve readability only, then you can always split up the expressions inside the if statement:

$exp1 = is_array($var) && isset($var['key']);
$exp2 = is_object($var) && isset($var->key);
$exp3 = substr($string, 0, 4) == 'foo';
$exp4 = ($exp1 || $exp2) && $exp3;
if ($exp4) {}

instead of

if (((is_array($var) && isset($var['key'])) || (is_object($var) && isset($var->key))) && substr($string, 0, 4) == 'foo') {}

Obviously, these are simplified examples, but you get the idea...

Refactoring a large block of chained if-else statements

Look here,

if($(this).text() == "Grocery"){
$(".type_changer").attr("id", "gro");
}else if($(this).text() == "Restaurant"){
$(".type_changer").attr("id", "res");
}else if($(this).text() == "Bar"){
$(".type_changer").attr("id", "bar");
}else if($(this).text() == "Pizza Delivery"){
$(".type_changer").attr("id", "piz");
}else if($(this).text() == "Quick Service"){
$(".type_changer").attr("id", "qui");
}else if($(this).text() == "Retail"){
$(".type_changer").attr("id", "ret");
}else if($(this).text() == "Salon"){
$(".type_changer").attr("id", "sal");
}

You have to think all the repetitions away. What would left over? Right, the text and id values:

"Grocery", "gro"
"Restaurant", "res"
"Bar", "bar"
"Pizza Delivery", "piz"
"Quick Service", "qui"
"Retail", "ret"
"Salon", "sal"

Let hold them in some data structure. An object is an obvious choice.

var types = {
"Grocery": "gro",
"Restaurant": "res",
"Bar": "bar",
"Pizza Delivery": "piz",
"Quick Service": "qui",
"Retail": "ret",
"Salon": "sal"
}

It can be accessed like an associative array with dynamic keys. Now you can use a single line:

$(".type_changer").attr("id", types[$(this).text()]);

How to replace the second part is left as exercise to you, but it boils down to the same.


Update: you seem to have a hard time in understanding this. Here's an explanation from my side:

When $(this).text() returns "Grocery", the above will resolve to

$(".type_changer").attr("id", types["Grocery"]);

The types["Grocery"] will in turn return "gro", so it basically ends up as

$(".type_changer").attr("id", "gro");

when $(this).text() is "Grocery".

See also:

  • JavaScript Object Notation (JSON) tutorial

Maximum conditions inside if in javascript

Use array for p and indexOf to check is i in array:

var p = [b - 1, b + 1, b + 6, b - 6, b + 5, b - 5, b + 7, b - 7];
if (p.indexOf(i) !== -1) {
bombcount++;
}


Related Topics



Leave a reply



Submit