What anti-patterns exist in development?
sobes.tech AI
Answer from AI
-
God Object: A class that contains too much functionality, knowledge, and responsibilities of other classes. Difficult to understand, test, and maintain.
-
Magic Strings/Numbers: Using "magical", unexplained string or numeric literals directly in code without assigning meaningful constants. Reduces readability and complicates changes.
-
Hardcoding: Embedding specific values (file paths, settings, API URLs) directly into code instead of using configuration files or constants. Makes deployment and environment changes difficult.
-
Duplicate Code: Repetition of the same or very similar code fragments in different places. Complicates maintenance, testing, and modifications.
-
Tight Coupling: Objects or modules are heavily dependent on each other. Changes in one component can lead to unexpected changes in another. Reduces flexibility and reusability.
-
Nested Conditionals: Excessive use of nested if/else/switch statements, making code hard to read and understand.
-
Feature Envy: A method in one class heavily depends on data or methods of another class, operating on them more than its own. Indicates that the method might need to be moved to another class.
-
Primitive Obsession: Using primitive data types (e.g., Int, String) to represent more complex concepts without creating dedicated objects or structures. Reduces expressiveness and can lead to errors due to lack of validation and encapsulation.
-
Boat Anchor: Classes or functions that are no longer used but are left in the code "just in case". Increases codebase size and complicates navigation.
-
Excessive Commenting: Commenting on obvious things or using comments instead of refactoring and improving code readability.
// Example of Magic String
let userDefaultsKey = "lastUserName" // Better to use a constant
// Example of Duplicate Code
func processOrder(_ order: Order) {
// Order processing logic
print("Processing order \(order.id)")
// ...
}
func processRefund(_ refund: Refund) {
// Similar processing logic
print("Processing refund for order \(refund.orderId)")
// ...
}
// Example of Tight Coupling
class OrderService {
func placeOrder(user: User, products: [Product]) {
let paymentProcessor = PaymentProcessor() // Strong dependency on PaymentProcessor
// ...
}
}