Code Smells Flashcards
(38 cards)
Why shouldn’t you use ‘Comments’? ( 3 points )
- Communication.
It’s hard to understand and read the code if comments have another meaning than a method - Flexibility.
It’s hard to support and change( Every time when you update the code you should also update the comment ) - Duplication.
You don’t need to write the same comment if your method name has already explained it.
When can we use ‘Comments’? ( 3 points )
- Explain difficult algorithm
- For some automation tolls ( annotate for example )
- To get the link to the guide which explains core behavior
Why shouldn’t we use ‘Long methods’? ( 2 )
- Flexibility
The long method contains too many responsibilities, that’s why it won’t be flexible for change. - Testability
It may be difficult to set up all initial behavior and test it.
Why shouldn’t we use ‘Long class/modules? ( 2 )
- Flexibility
Too many responsibilities and hard to change - Testability
Hard to test. We need to create too many setups before tests running.
Why shouldn’t we use ‘Long parameter list’?
- Simplicity
It tells us that the method tries to do too much and we should try to group params as an object together. - Flexibility
If we add new params, we need to update all invoke of the method. It’s not flexible - Communication
You should remember lot’s of things
What is ‘embedded in name’ smell?
When you add unnecessary value to the name:
- add_course(course)
When you add type to the name
- animal_string
What is ‘uncommunicative name’ smell?
When you use:
- One or two-character names
- Misleading names
- abbreviations
What is ‘Inconsistent name’ smell?
One name is used in one place, and a different name is used for the same thing somewhere else. For example, in a single application you might see add, store, put, and place for the same basic method.
Sample: ‘Destroy, delete, remove for the same method;’
What is ‘Dead code’ smell?
A variable, parameter, code fragment, method, module, or class is not used anywhere (perhaps other than in tests).
Why should we remove ‘Dead code’?
- Size( adds to the application’s size, and thus to the amount of code that must be understood by developers and maintainers)
- Communication ( hard to understand intentions)
- Flexibility (It’s stupid to support unused code)
What is ‘Speculative generality’ code smell?
The same as You ain’t gonna need it
This code smell describes a situation where people develop a class with all sorts of hooks and special cases just so it will handle things that might be required in the future but not at this stage
What is ‘Greedy Method’ code smell?
When code do more than one job or have several levels of abstraction. ( More than one responsibility )
What is ‘Procedural code’ smell?
Don’t use procedural code ( like loop, while with temp variables) if you can use each
What is ‘Dynamic code creation’ code smell?
Don’t use eval
What is Derived Value
code smell?
When you have code which was defined from scratch instead of getting from another value:
NAME = ‘Nic’
SURNAME = ‘Mil’
FULL_NAME = ‘Nic Mil’
But we can leave this code smell for the tests.
What is Repeated Value
code smell?
When you have a string more than one time in the code.
You need to move this string to the constant.
What is Duplication code
code smell?
Two fragments of code look nearly identical.
What is Alternative Modules with Different Interfaces
code smell?
Two classes or modules seem to be doing the same thing but are using different method names.
What is ‘Nil check` smell?
Try to avoid using nil check
What is Special case
code smell?
- Complex if statements.
- Guard clauses—checks for particular values before doing work (especially comparisons to constants).
What is Complicated Boolean Expression
code smell?
Code has complex conditions involving and, or, and not.
Example:
- if !a
- !(a && b)
What is Control Coupling
code smell?
• A method or block checks the value of a parameter in order to decide which execution path to take.
• A method’s name includes a word such as “or.”
What is Simulated Polymorphism
code smell?
• Code uses a case statement (especially on a type field).
• Code has several if statements in a row (especially if they’re comparing against the same value).
What is ‘Primitive Obsession’ code smell?
(Одержимость примитивными типами)
When you use primitive types as a string on number instead of creating new Class. ( For example Money, Data.. constant that includes Number instead of meaning )