When choosing a value based off of 2 boolean values in this format

var foo:int;

if (X){
    foo = 50;
} else if (Y){
    foo = -50;
} else {
    foo = 0;
}

I discovered that I can condense this down by typecasting the boolean operations to integers, and mathing it up a bit like so:

var foo:int;

foo = ( int (X) - int(!X && Y) ) * 50;
// int() typecasts a boolean argument to an int of 1 if true, 0 if false.

Here are my questions:

  1. Is this approach less resource intensive?
  2. Is this approach readable to you?
  3. Is assigning values in this way common?
  4. If 1 is true then in your opinion, does the performance increase justify the less readable nature of this approach?

This situation arose in ActionScript 3.0, but I am interested in answers from all languages, and from a best practice perspective.

没有正确的解决方案

许可以下: CC-BY-SA归因
scroll top