Clean code practice advice in ignition

Hi all,

I've created a button that has an 'onClick' script on it and in that script I have a lot of duplicated code at the moment. Generally when you have duplicated code you want to encapsulate it but I'm not sure the best way to do this in Ignition. Does anyone have any advice as to how they would go about cleaning the code up in this button?

Its formated more or less like this:

if something == True:
  #lots of code
else:
   #Duplication of most of 'lots of code'

Thank you,

Cam

That is the whole purpose of project library scripts.

1 Like

How would I use that if I'm calling a lot of different component properties (text fields and checkbox) in the script?

You can try to pull the duplicate code out of the if-else block entirely. Anything that is a pre-requisite would go before the if block, and anything that depends on something in the if block would go after.

You can make use of custom methods to your advantage if you want to keep things clean-looking without going all the way out to the project library.

If you right-click on the component, there's a "configure scripts" option where you can define any custom methods for that component.
image

I totally forgot about that section :sweat_smile:

Thank you @Felipe_CRM

Also, you can actually do this in the project library if you pass the component info to the function:

Useful if you plan to use that code for more than one component.

I forget if for Perspective you need to pass a session handle as well or not. Might depend on what exactly you're doing.
EDIT: Passing the component (i.e. self from an event handler script) is enough to be able to modify prop values. Not sure what I was thinking about.