Skip to main content

Inappropriate coding practices - Unused properties

Need

Elimination of unused properties in the codebase

Context

  • Usage of Swift for iOS and macOS app development
  • Usage of UIKit for building modern and responsive user interfaces

Description

Non compliant code

import UIKit

class ViewController: UIViewController {
// Unused properties
var unusedString: String = "Hello, World!"
var unusedInt: Int = 123
var unusedBool: Bool = true

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}

The above Swift code defines a ViewController class for an iOS application. This class includes three properties: unusedString, unusedInt, and unusedBool. These properties are initialized but never used anywhere in the code.

This is an example of inappropriate coding practices, specifically, defining unused properties. These unused properties add unnecessary complexity to the source code and can lead to suboptimal performance.

Unused properties consume memory and processing resources, even though they are not contributing to the functionality of the application. This can have a negative impact on the performance of the application, particularly on devices with limited resources.

In addition, unused properties can make the code more difficult to read and maintain. Other developers working on the code might spend time trying to understand why these properties are there and how they are supposed to be used. This can slow down development and increase the risk of introducing bugs.

Therefore, it is recommended to remove these unused properties to improve the efficiency and maintainability of the code.

Steps

  • Identify and remove any unused properties in the code.
  • Review the code to ensure that all properties are being used appropriately.
  • Consider the purpose and functionality of each property before removing it.
  • If a property is no longer needed, remove it from the code to reduce complexity.
  • Regularly review and refactor the code to identify and remove any unused properties.
  • Document the reasons for removing a property to maintain code clarity and understanding.

Compliant code

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}

The original code contained three properties: unusedString, unusedInt, and unusedBool. These properties were not used anywhere in the code, adding unnecessary complexity and potentially affecting the overall performance of the application.

In the fixed code, these unused properties have been removed. This simplifies the code and reduces the potential for confusion or errors. It's important to regularly review and refactor the code to identify and remove any unused properties. This practice helps maintain clean, efficient, and understandable code.

Remember, before removing a property, consider its purpose and functionality. If a property is no longer needed, it should be removed. However, if it's part of a feature that's not yet implemented or is temporarily disabled, it might be better to leave it in the code but clearly comment on its purpose.

Documenting the reasons for removing a property can help maintain code clarity and understanding among the team. It provides context for future code reviews and can be helpful when onboarding new team members or revisiting the code after a long period.

References