Jump to content

# Optimize Code

## 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.

##### Share on other sites

The switch statement is not the optimal structure to use in this situation. Why do you want to use it?

##### Share on other sites

The switch statement is not the optimal structure to use in this situation. Why do you want to use it?

And something like "switch(true)" work? If not, could edit this code for me, in If/Else same, of a better way?

Thanks!

##### 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.

##### 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
##### Share on other sites

I can do this:

Uh, no you can't.

##### Share on other sites

Uh, no you can't.

Why not?

##### Share on other sites

Hmmm... it does work, but what is the point? It reminds me of people who use for( ; ; ){} rather than while(true){}

##### 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];`
##### Share on other sites

Really, thanks for all,

justsomeguy

davej

Ingolme

In fact, I wanted to know different ways of dealing with conditions a little more complex. You all helped me realize how many ways I can do the same thing, that's what I needed really. I expressed myself badly.

Thanks again!

##### 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.
##### Share on other sites

Explained here http://www.w3schools.com/js/js_comparisons.asp under 'Conditional (Ternary) Operator'.

##### Share on other sites

Explained here http://www.w3schools.com/js/js_comparisons.asp under 'Conditional (Ternary) Operator'.

Thank you too!

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
×
• Create New...