Code Smells Flashcards

1
Q

Why shouldn’t you use ‘Comments’? ( 3 points )

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When can we use ‘Comments’? ( 3 points )

A
  • Explain difficult algorithm
  • For some automation tolls ( annotate for example )
  • To get the link to the guide which explains core behavior
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why shouldn’t we use ‘Long methods’? ( 2 )

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why shouldn’t we use ‘Long class/modules? ( 2 )

A
  • Flexibility
    Too many responsibilities and hard to change
  • Testability
    Hard to test. We need to create too many setups before tests running.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why shouldn’t we use ‘Long parameter list’?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is ‘embedded in name’ smell?

A

When you add unnecessary value to the name:
- add_course(course)
When you add type to the name
- animal_string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is ‘uncommunicative name’ smell?

A

When you use:
- One or two-character names
- Misleading names
- abbreviations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is ‘Inconsistent name’ smell?

A

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;’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is ‘Dead code’ smell?

A

A variable, parameter, code fragment, method, module, or class is not used anywhere (perhaps other than in tests).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why should we remove ‘Dead code’?

A
  • 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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is ‘Speculative generality’ code smell?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is ‘Greedy Method’ code smell?

A

When code do more than one job or have several levels of abstraction. ( More than one responsibility )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is ‘Procedural code’ smell?

A

Don’t use procedural code ( like loop, while with temp variables) if you can use each

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is ‘Dynamic code creation’ code smell?

A

Don’t use eval

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is Derived Value code smell?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is Repeated Value code smell?

A

When you have a string more than one time in the code.
You need to move this string to the constant.

17
Q

What is Duplication code code smell?

A

Two fragments of code look nearly identical.

18
Q

What is Alternative Modules with Different Interfaces code smell?

A

Two classes or modules seem to be doing the same thing but are using different method names.

19
Q

What is ‘Nil check` smell?

A

Try to avoid using nil check

20
Q

What is Special case code smell?

A
  • Complex if statements.
  • Guard clauses—checks for particular values before doing work (especially comparisons to constants).
21
Q

What is Complicated Boolean Expression code smell?

A

Code has complex conditions involving and, or, and not.

Example:
- if !a
- !(a && b)

22
Q

What is Control Coupling code smell?

A

• 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.”

23
Q

What is Simulated Polymorphism code smell?

A

• 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).

24
Q

What is ‘Primitive Obsession’ code smell?

A

(Одержимость примитивными типами)

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 )

25
Q

What is ‘Data class` code smell?

A

It’s when a class contains only data ( attr_reader and initializer) but the whole class should contain data and behavior. So, in this case, will be better to use Struct.

26
Q

What is ‘Data clump` code smell?

A

(Глыба данных) It’s when you tried to send two or more items together in the params list.

Something like cord(x, y, z)
Or something(period_start, period_end, period_middle)

So, you basically need to move these params to the object.

27
Q

What is Temporary Field code smell?

A

An instance variable is set only in certain circumstances.

class Animal
attr_accessor :type

def show
if type.nil?
p ‘Not Set’
else
p type
end
end

end

animal = Animal.new
animal.type = ‘King’
animal.show

28
Q

What is implemetation inheritance code smell?

A

So when you use inheritance instead of composition.
Inheritance needs to use for is-a relations and composition for has-a relations.

Instances of the subclass should have the ability to passed as substitutes for instances of the parent.( we can replace parent object with a child object)

29
Q

What is ‘Refused bequest` code smell?

A

( Отказано в наследстве)

• Explicit Refusal: The subclass undefines an inherited method, makes an inherited method private, or makes it throw an exception when it is called.
• Implicit Refusal: A method inherited from the parent class just doesn’t work for instances of the subclass.

30
Q

What is Inappropriate Intimacy (Subclass form) code smell?

A

(Неуместная близость)

A class makes use of the implementation details of its superclass.

31
Q

What is Lazy Class code smell?

A

A class isn’t doing much—its parents, children, or clients seem to be doing all the associated work—and there isn’t enough behavior left in the class to justify its continued existence.

En example -> CreateForm < CoreForm without any behavior, so we shouldn’t create this form for future changes.

32
Q

What is Feature Envy code smell?

A

(Завистливая функция) A code fragment references another object more often than it references itself.

Example:

class Parcel
def addressee
“#{@person.first_name} #{@person.last_name}”
end
end

Should go to:

class Parcel
def addressee
@person.full_name
end
end

class Person
def full_name
“#{first_name} #{last_name}”
end
end

33
Q

What is Utility Function code smell?

A

(Функция полезности) - An instance method has no dependency on the state of the instance.

34
Q

What is Global variable code smell?

A

Your code uses a global variable, other than one predefined by Ruby itself.

35
Q

What is Method Chain code smell?

A

You see calls of the form a.b.c.d.

This code breaks the Law of Demeter

36
Q

What is Middle Man code smell?

A

A class that mostly delegates its work is known as a Middle Man:
• Most methods of a class call the same or a similar method on another object:

def f
@delegate.f
end

37
Q

What is Greedy Module code smell?

A

A module has more than one responsibility—for example, formatting a report as XML and sending it to a SOAP service.

38
Q

What is ‘Divergent change’ code smell?

A

It’s like Single Responsibility. When the class has many reason to be changed.
For example when the class has ‘send_email’ and ‘update’ method together.
It is likely that changes will be made to different parts of the class for different reasons.