Boolean-Integer Typecasting to Replace Conditional [closed]
https://softwareengineering.stackexchange.com/questions/191729
-
06-11-2019 - |
题
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:
- Is this approach less resource intensive?
- Is this approach readable to you?
- Is assigning values in this way common?
- 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.
没有正确的解决方案