Encapsulate Variable / Encapsulate Field

Examine

Identify a variable or field that has public or global access that you would like to encapsulate.

Prepare

Find all the places it is used and store this list or the means to re-create it.

Implement

At this point you’ve succeeded in you original aim - the global variable is encapsulated by getter and setter functions.

Clear

Change the encapsulated field or variable to be private (if your language allows).

Follow up

Prompted by Code Smells

Relevant Learning Hours

Sources

Back to All Refactorings