Jump to content

tiscavalcanti
 Share

Recommended Posts

Hello,

 

I have created this code:

 

if (w > maxWidth || h > textHeight) {
if (y < x) {
[y, y]
} else {
[x, x]
}
} else {
[value[0], value[1]]
}

 

It is a piece, but it gives to understand what I want to optimize without the rest.

 

How to modify this code to work with: Switch Statement instead If/Else mode?

 

This is a expression of After Effects.

 

Thanks.

Link to comment
Share on other sites

It would, but if you're doing a switch on a boolean it usually makes more sense to use an if/else structure. If your goal is optimization, you don't see big performance increases by using a switch over an if/else structure.

I can do this:

 

function getValue() {
switch (true) {
case yB < xA:
return [yB, yB];
default:
return [xA, xA];
}
}


E = getValue();


if (w > maxWidth || h > textHeight){
x = E[0]; y = E[1];
}


[x, y];
Edited by tiscavalcanti
Link to comment
Share on other sites

I can do this:

Yes, you can, but since we're talking about optimizing and efficiency, what does this do for you:

 

switch (true) {
  case yB < xA:
    return [yB, yB];
  default:
    return [xA, xA];
}
that is better than this:

 

if (yB < xA) {
  return [yB, yB];
}
else {
  return [xA, xA];
}
or this:

 

return (yB < xA) ? [yB, yB] : [xA, xA];
Link to comment
Share on other sites

Yes, you can, but since we're talking about optimizing and efficiency, what does this do for you:

 

switch (true) {
  case yB < xA:
    return [yB, yB];
  default:
    return [xA, xA];
}
that is better than this:

 

if (yB < xA) {
  return [yB, yB];
}
else {
  return [xA, xA];
}
or this:

 

return (yB < xA) ? [yB, yB] : [xA, xA];
This way is interesting:
return (yB <xA)? [yB, yB]: [xA, xA];
Could you explain a little more how it works.
The use of interrogation, is something new to me.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...