This function works, but it is full of code smells. Let’s break down the problems using this example:
- Long Methods: When you find a method that is unnecessarily long, that’s usually a code smell. The above function is doing too many things:
- Validating passwords
- Checking email format
- Verifying the user’s age
- Storing user data
- Sending notifications
This makes the function difficult to read and maintain.
2. Too Many Parameters: If you have a function that requires five or more parameters, it’s likely a code smell. The above function takes 10 parameters (name, email, password, confirmPassword, age, phone, address, accountType, notifyAdmin, sendWelcomeEmail). Passing this many parameters is confusing and makes the function hard to use.
3. Duplicated Code: Duplicating code in multiple places is a clear sign of a code smell. The function manually checks if password !== confirmPassword and then separately checks if email.includes(“@”) && email.includes(“.”). If these validations are needed elsewhere in the system, they will have to be duplicated.
4. Feature Envy: This occurs when a method in one class spends too much time interacting with the data of another class. In practice, this could lead to tight coupling between classes, making it difficult to change or extend the code without affecting other parts of the system. For example the above function interacts too much with unrelated concerns:
- It decides when to notify an admin.
- It sends a welcome email.
- It stores user information in the database.
- It performs multiple types of validation.
These tasks should belong to different parts of the system.
5. Dead Code: Dead code refers to code that is never executed but still exists in the codebase. For example: If the notifyAdmin and sendWelcomeEmail flags are set to false, the related code does nothing. This conditional logic adds unnecessary complexity.
6. Inconsistent Naming: Consistency in naming is crucial for code readability. When similar things are named differently or different things are named similarly, it leads to confusion and makes the code harder to understand. For example, The function uses notifyAdmin, but sendWelcomeEmail instead of notifyUser. Naming inconsistencies like this make the code harder to understand.
7. Obvious Behavior Is Unimplemented: Functions should do what their names suggest. If a function’s name implies behavior that isn’t implemented, it creates a code smell. For example, the function registerUser implies it will create a new user, but it doesn’t explicitly return the user object. Instead, it just prints a message.
8. Obsolete Comment: A comment that is outdated or no longer relevant is a code smell. Comments should provide value by explaining why something is done, not just what is done. When a comment becomes obsolete, it can mislead developers, making it harder to understand the current state of the code. For example: the function contains a comment // It checks age limit up to 18 . and if someone changes the logic without updating the comment, it will create confusion.
9. Vertical Separation: Variables and functions should be defined close to where they are used. A variable that is declared far from its usage or a private function defined far from its first invocation creates a smell, making the code harder to read and maintain.
For example: the function jumps between unrelated concerns (validation → database storage → notifications), making it harder to follow.
10.Too Much Information: Exposing too many details, such as internal variables or methods, in a class interface leads to high coupling and low cohesion. This makes the class difficult to use and maintain. For instance, the function is directly handling how user data is stored, validated, and processed. A better approach would be to delegate these tasks to different modules.
11. Magic Numbers: Hardcoding literal numbers or strings in your code, instead of using named constants, is a common code smell. For example, the function hardcodes the age limit (18). It should instead use a named constant, like MINIMUM_AGE.
This list includes some of the most common code smells, but it’s not exhaustive. There are many other potential issues that could arise in your codebase.