Complexity Code SmellsReading Only
A code smell is a characteristic or pattern you see in the code that possibly indicates a deeper problem. They could violate certain principles or they could negatively impact your design quality. They're not bugs, they're potentially bad implementations.
Complexity is the problem because of the symptoms it presents. Here are some code smells that indicate implementation complexity that might need to be avoided or refactored:
Shotgun Surgery
If a new requirement or feature (especially the simple ones) requires you to change several classes or methods, you're performing shotgun surgery. It's a sign that the code is tightly coupled. The methods and/or classes know too much about each other; they lack encapsulation.
Divergent Change
When you update the same class to implement two very different requirements, you're dealing with divergent change. It occurs when a single class changes for multiple reasons, hint hint, it has multiple responsibilities. "When the Account's status changes, I need to change these three methods, and when the rating changes, I need to update these two methods.". The fix here is to extract and bundle relevant methods with one another in a new class.
Cognitive Load
This is the amount of information our working memory can process at any given time. When we're reading code, we'd like to be able to flow through the code quickly, with each line or segment being an easy-to-understand building block for the next. Keep an eye out for large classes and methods; these are indicators that a code segment is doing too much. In these cases, you'd want to abstract the logic away by placing it in another layer.
Fortune Teller Architecture
Imagine you're working on an Apex class to process CSV files. You predict that you might need to process other file types in the future, so you create a complex abstraction with many classes and patterns. This can overcomplicate the code and make it harder to manage, especially when the current requirements are straightforward. You're building software for problems you may never have.