Ok to use component properties as variables?

Is there anything wrong with using properties (color etc) of components (buttons etc) as variables ?

For example, I have several off/on buttons, which are toggled (between 'on' and 'off' ) by clicking. I have corresponding variables to keep track of the 'on' or 'off' state of each button. I also have the buttons change their background color based on whether they are in the 'off' or 'on' state. So I'm thinking I should just ditch those variables, and simply use the background color (of each button) to store its state.

You can do what you are saying, but it is much easier to use the built in blocks.

Eg instead of Set variable_Button_1 colour to Button1.backgroundcolour

Just use the in built Button1.background colour block

I did this in this project ...

@deanart2012 Can you be a little more specific about this ?

Hi @RustyShackleford

I would recommend you keep the variables and UI state as separate as possible. There might come a day when you wish to swap the buttons with a different component, or instead of toggling the colour you may wish to change the font size of the buttons' texts. You'd have to rewrite a lot of your app if you are to rely on the button's background colour as the source-of-truth for your app's state.

On the other hand, maintaining a set of variables that handle your app's business logic means you can swap out elements of the UI as and how you please without having to worry about how your app works.

TL;DR: You'd want to use dedicated variables to store the state so that you can separate the visuals from the workings of your app.

Cheers!

2 Likes

@vishwas Good point.

1 Like