Code Smells

Code Smells / Object-Orientation Abusers / Temporary Field

Temporary Field code smell icon

Temporary Field

A Temporary Field smell occurs when a variable that should be defined within a method’s scope is instead defined in the class’ scope.

This violates the information hiding principle, since all the methods in the class will have access to this variable, when only one method needs it.

Another issue this smell causes, is the unreliability of instance variables, since this instance variable would only be updated when the method is run, but we expect instance variables to reliably track information about our objects at all times.

Left chevron Right chevron

Books about code smells

Learn to write better smelling code.

Refactoring: Improving the Design of Existing Code book cover

Refactoring: Improving the Design of Existing Code

Code smells: A Predictor of Remarkable Quality Improvement book cover

Code smells: A Predictor of Remarkable Quality Improvement

Clean Code: A Handbook of Agile Software Craftsmanship book cover

Clean Code: A Handbook of Agile Software Craftsmanship

I receive a commission on sales made from the above affiliate links.