# Clean code practice advice in ignition

**URL:** <https://forum.inductiveautomation.com/t/clean-code-practice-advice-in-ignition/76408>\
**Category:** Ignition\
**Tags:** perspective, scripting\
**Created:** [July 14, 2023, 7:31pm UTC](https://forum.inductiveautomation.com/t/clean-code-practice-advice-in-ignition/76408 "2023-07-14T19:31:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cameron\_Nickle](https://avatars.discourse-cdn.com/v4/letter/c/b3f665/32.png) [@Cameron\_Nickle](https://forum.inductiveautomation.com/u/Cameron_Nickle)\
**Post date:** [July 14, 2023, 7:31pm UTC](https://forum.inductiveautomation.com/t/clean-code-practice-advice-in-ignition/76408/1 "2023-07-14T19:31:27Z")

</div>

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:

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

```

Thank you,

Cam

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [July 14, 2023, 7:33pm UTC](https://forum.inductiveautomation.com/t/clean-code-practice-advice-in-ignition/76408/2 "2023-07-14T19:33:23Z")

</div>

That is the whole purpose of project library scripts.

---

<div class="post-metadata">

**Author:** ![Cameron\_Nickle](https://avatars.discourse-cdn.com/v4/letter/c/b3f665/32.png) [@Cameron\_Nickle](https://forum.inductiveautomation.com/u/Cameron_Nickle)\
**Post date:** [July 14, 2023, 7:38pm UTC](https://forum.inductiveautomation.com/t/clean-code-practice-advice-in-ignition/76408/3 "2023-07-14T19:38:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Felipe\_CRM](https://avatars.discourse-cdn.com/v4/letter/f/dfb087/32.png) [@Felipe\_CRM](https://forum.inductiveautomation.com/u/Felipe_CRM)\
**Post date:** [July 14, 2023, 7:40pm UTC](https://forum.inductiveautomation.com/t/clean-code-practice-advice-in-ignition/76408/4 "2023-07-14T19:40:26Z")

</div>

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](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/9/09063267e26608794c7fe1a0ca676a7c4b7204e6.png)

---

<div class="post-metadata">

**Author:** ![Cameron\_Nickle](https://avatars.discourse-cdn.com/v4/letter/c/b3f665/32.png) [@Cameron\_Nickle](https://forum.inductiveautomation.com/u/Cameron_Nickle)\
**Post date:** [July 14, 2023, 7:42pm UTC](https://forum.inductiveautomation.com/t/clean-code-practice-advice-in-ignition/76408/5 "2023-07-14T19:42:46Z")

</div>

I totally forgot about that section 😅

Thank you @Felipe_CRM

---

<div class="post-metadata">

**Author:** ![Felipe\_CRM](https://avatars.discourse-cdn.com/v4/letter/f/dfb087/32.png) [@Felipe\_CRM](https://forum.inductiveautomation.com/u/Felipe_CRM)\
**Post date:** [July 14, 2023, 7:50pm UTC](https://forum.inductiveautomation.com/t/clean-code-practice-advice-in-ignition/76408/6 "2023-07-14T19:50:38Z")

</div>

> [@Cameron\_Nickle](#):
>
> calling a lot of different component properties (text fields and checkbox) in the script

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

> [@Change button´s properties from script library](https://forum.inductiveautomation.com/t/change-button-s-properties-from-script-library/64618/2):
>
> If you want to do something like “programmatically alter all buttons the same way”, then I would suggest making a project library function that expects a particular component as the input. E.G: Project Library Script - utils def setColors(button): button.background = system.gui.color(251, 250, 249) Component Event Script utils.setColors(event.source.parent.getComponent('Button')) The crucial part is that you pass the component itself as a reference into your project script.

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.
