Code smells are patterns indicate a potential problem with a section of code. While the associated code typically does actually work, it represents a strong indicator towards an un unrelated problem. Therefore, code smells should be investigated, fixed, and avoided. If the smelly code turns out to be necessary, it should be refactored so it’s as clear as possible, and as a last resort, commented.
Because these indicate where problems are, a good programmer will read these carefully and keep them in mind as they code.
The following is reference of code smells from Martin Fowler’s Refactoring and Robert Martin’s Clean Code.
Comments
- Inappropriate Information: Reserve comments for technical notes referring to code and design.
- Obsolete Comment: Update or delete obsolete comments.
- Redundant Comment: A redundant comment describes something able to sufficiently describe itself.
- Poorly Written Comment: Comments should be brief, concise, correctly spelled.
- Commented-Out Code: Ghost code. Delete it.
Environment
- Build Requires More Than One Step: Builds should require one command to check out and one command to run.
- Tests Require More Than One Step: Tests should be run with one button click through an IDE, or else with one command.
Functions
- Too Many Arguments: Functions should have no arguments, then one, then two, then three. No more than three.
- Output Arguments: Arguments are inputs, not outputs. If somethings state must be changed, let it be the state of the called object.
- Flag Arguments: Eliminate boolean arguments.
- Dead Function: Discard uncalled methods. This is dead code.
General
- Multiple Languages in One Source File: Minimize the number of languages in a source file. Ideally, only one.
- Obvious Behavior is Unimplemented: The result of a function or class should not be a surprise.
- Incorrect Behavior at the Boundaries: Write tests for every boundary condition.
- Overridden Safeties: Overriding safeties and exerting manual control leads to code melt down.
- Duplication: Practice abstraction on duplicate code. Replace repetitive functions with polymorphism.
- Code at Wrong Level of Abstraction: Make sure abstracted code is separated into different containers.
- Base Classes Depending on Their Derivatives: Practice modularity.
- Too Much Information: Do a lot with a little. Limit the amount of things going on in a class or functions.
- Dead Code: Delete unexecuted code.
- Vertical Separation: Define variables and functions close to where they are called.
- Inconsistency: Choose a convention, and follow it. Remember no surprises.
- Clutter: Dead code.
- Artificial Coupling: Favor code that is clear, rather than convenient. Do not group code that favors mental mapping over clearness.
- Feature Envy: Methods of one class should not be interested with the methods of another class.
- Selector Arguments: Do not flaunt false arguments at the end of functions.
- Obscured Intent: Code should not be magic or obscure.
- Misplaced Responsibility: Use clear function name as waypoints for where to place your code.
- Inappropriate Static: Make your functions nonstatic.
- Use Explanatory Variables: Make explanatory variables, and lots of them.
- Function Names Should Say What They Do: …
- Understand the Algorithm: Understand how a function works. Passing tests is not enough. Refactoring a function can lead to a better understanding of it.
- Make Logical Dependencies Physical: Understand what your code is doing.
- Prefer Polymorphism to If/Else or Switch/Case: Avoid the brute force of switch/case.
- Follow Standard Conventions: It doesn’t matter what your teams convention is. Just that you have on and everyone follows it.
- Replace Magic Numbers with Named Constants: Stop spelling out numbers.
- Be Precise: Don’t be lazy. Think of possible results, then cover and test them.
- Structure Over Convention: Design decisions should have a structure rather than a dogma.
- Encapsulate Conditionals: Make your conditionals more precise.
- Avoid Negative Conditionals: Negative conditionals take more brain power to understand than a positive.
- Hidden Temporal Couplings: Use arguments that make temporal coupling explicit.
- Don’t Be Arbitrary: Your code’s structure should communicate the reason for its structure.
- Encapsulate Boundary Conditions: Avoid leaking +1’s and -1’s into your code.
- Functions Should Descend Only One Level of Abstraction: The toughest heuristic to follow. One level of abstraction below the function’s described operation can help clarify your code.
- Keep Configurable Data at High Levels: High level constants are easy to change.
- Avoid Transitive Navigation: Write shy code. Modules should only know about their neighbors, not their neighbor’s neighbors.
Names
- Choose Descriptive Names: Choose names that are descriptive and relevant.
- Choose Names at the Appropriate Level of Abstraction: Think of names that are still clear to the user when used in different programs.
- Use Standard Nomenclature Where Possible: Use names that express their task.
- Unambiguous Names: Favor clearness over curtness. A long, expressive name is better than a short, dull one.
- Use Long Names for Long Scopes: A name’s length should relate to its scope.
- Avoid Encodings: No not encode names with type or scope information.
- Names Should Describe Side-Effects: Consider the side-effects of your function, and include that in its name.
Tests
- Insufficient Tests Test: everything that can possibly break
- Use a Coverage Tool: Use your IDE as a coverage tool.
- Don’t Skip Trivial Tests: …
- An Ignored Test is a Question about an Ambiguity: If your test is ignored, the code is brought into question.
- Test Boundary Conditions: The middle is usually covered. Remember the boundaries.
- Exhaustively Test Near Bugs: Bugs are rarely alone. When you find one, look nearby for another.
- Patterns of Failure Are Revealing: Test cases ordered well will reveal patterns of failure.
- Test Coverage Patterns Can Be Revealing: Similarly, look at the code that is or is not passed in a failure.
- Tests Should Be Fast: Slow tests won’t get run.
I’m pretty sure I borrowed this list from somewhere, but I can’t find the source anymore — email me for credit if it’s yours.
Please contact me for any thoughts, comments, or feedback.