Minimizing Code In The draw() Function

I understand that we want to keep the code in the draw() loop to a minimum. Does that mean that code like this:
image

would be improved if it were written like this:
image

I’m thinking it is better, but wondering what others think.

Thanks,

John

I suppose it would depend on the level of the students you’re working with, but it seems to me that any time you can use a variable over repeated code in a loop it is preferable. I’d love to hear the opinions of others too. Isn’t it neat how code can include creativity and style?!

Typically, it’s always best to declare variables outside the loop and use the loop only for updating values, so yes, your 2nd block of code is better optimized as it won’t repeatedly run the code that doesn’t change.

You can use blocks that chase the textFont and textSize and stroke, etc. inside the loop, but I wouldn’t unless it’s needed as part of the repeated commands (like it could change to red inside a conditional or something). In this case, I don’t see that as necessary.

Because of how backgrounds and animations work, the background and text blocks are needed as they need to be redrawn every frame.

Mike