top of page

Code Sample

Refactored Poll Class
(Unity Script Component)

Summary

The goal of refactoring the poll class was to resolve several bugs related to UI objects' creation not enabling or disabling related buttons correctly.

​

The poll UI had an TextMesh Pro input field for the question, a button to add option (answer) input fields, a submit button and a close button.

​

The buttons were not disabling / enabling correctly.
For example, the submit button would be enabled when some of the fields had no text which was unacceptable.

 

The Original Poll Class

Summary of the Original Poll Class

The original class had hit or miss functionality.
Still, the “bones” or structure for the desired functionality was there and *mostly* working.

​

There were many places where code repeated, specifically in regards to checking the number of option fields (optionCount).

​

The listeners used also didn’t have associated remove listener functionality for when the poll gameobject was inactive or when the option fields associated with a listener were removed from the poll.
 

The Refactored Poll Class

Summary of the Refactored Poll Class

As this was a project for an employer who wanted the access modifiers to remain public for the time being, rather than my own project, I did not institute private level access modifiers and the _varName naming convention.


I moved all private methods, including Unity state methods, below all public methods.

 

I added Awake(), OnEnable(), and OnDisable() unity state methods to properly add/remove listeners when the script component or attached game object became inactive.

 

I also moved all code logic related to enabling/disabling buttons from the Update() method to listener methods that would determine if those buttons should be enabled or disabled.

 

All repeating logic checks were moved to their own methods (CanCreateOption(), EnableOrDisableSubmitButton()).

I also created similar functionality that was implemented in the AllFieldsNotEmpty() method which was logic previously not existing.

​

The general approach was that this class should listen to all input fields and update the status of the Submit button based on OnValueChanged built-in Unity events.

Note that this was not done in the Editor / Inspector but rather through code in this class.

 

Per company policy, I noted where and when code was changed or added with a description using commented code.

 

Once completed, I thoroughly tested the functionality and found it to be working as desired.

Laptop Keyboard
bottom of page