Design Flashcards

1
Q

What is clean code, and why is it important?

A

Clean code refers to:

  1. well-structured,
  2. readable,
  3. and maintainable code
  4. that follows best practices.

It:
1. enhances collaboration,
2. reduces bugs,
3. and facilitates easier future modifications and debugging.

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

What does SOLID stand for?

A

S - Single Responsibility Principle
O - Open/Closed Principle
L - Liskov Substitution Principle
I - Interface Segregation Principle
D - Dependency Inversion Principle

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

What are some techniques to improve code readability?

A

-Meaningful variable and function names
- Proper code indentation and formatting
- Keeping functions short and focused (Single Responsibility Principle)
- Writing descriptive comments only when necessary

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

What is DRY?

A

It stands for Don’t Repeat Yourself and advocates for avoiding duplication in code by extracting common functionalities into reusable components, functions, or modules. It promotes code reusability and maintainability.

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

Single Responsibility Principle (SRP)

A

The Single Responsibility Principle (SRP) states that a class should have only one reason to change.

If multiple responsibilities are included in a class and one of those responsibilities changes, it could lead to changes affecting unrelated functionalities, resulting in a less maintainable codebase.

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

Open/Closed Principle (OCP)

A

The Open/Closed Principle (OCP) asserts that software entities (classes, modules, functions) should be open for extension but closed for modification. This means that the behavior of a module can be extended without altering its source code.

By using techniques like inheritance, interfaces, and polymorphism, new functionality can be added to an existing codebase without changing its core implementation, thereby minimizing the risk of introducing bugs.

You can also use the Decorator pattern to apply new functionality to an existing component such that the component’s source code does not need to be changed.

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

Liskov Substitution Principle (LSP)

A

The Liskov Substitution Principle (LSP) states that objects of a superclass should be replaceable with objects of its subclasses without affecting the behavior of the program.

Examples:

  • Square vs Rectangle (changing length and width results in different behavior for things like calculating area).
  • Seagull vs Penguin (both birds but both don’t fly).

Solution: More granular interfaces (i.e. Interface Segregation Principle).

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

Interface Segregation Principle (ISP)

A

The Interface Segregation Principle (ISP) advocates that it is better to have many smaller interfaces than a few large ones.

A component should never have to implement or extend something that requires it to implement functionality that is not needed by that component.

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

Dependency Inversion Principle (DIP)

A

inverts the direction of dependencies away from implementation to abstractions.

  1. high-level modules should not depend on low-level modules and thus both should depend on abstractions
  2. and abstractions should not depend on details, details should depend on abstractions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are some common code smells?

A

-Duplicated code
- Long methods or functions
- Large classes or modules
- Inappropriate comments
- Switch statements or conditional complexity

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

What is Cohesion?

A

Cohesion refers to the degree to which elements within a module or class belong together.

High cohesion means that elements within a module are related and work together towards a single purpose.

Low cohesion signifies that the elements are not closely related.

Single Responsibility Principle

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

What is Coupling?

A

Coupling represents the level of dependency between different modules or classes.

Loose coupling is desirable as it reduces the dependency between modules, making them more independent.

Tight coupling implies a high level of dependency, which can make changes more challenging.

Open/Close Principle

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

What is dependency injection?

A

It is a design pattern or technique where one object supplies the dependencies to another object, rather than the object creating its dependencies directly.

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

What is polymorphism?

A

objects being treated uniformly through a common interface while exhibiting behavior specific to their actual types

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

What is composition?

A

Represents a more tightly coupled has-a relationship between classes where one class contains objects of another class such that:

  1. When the whole is destroyed so are it’s parts
  2. The whole is responsible for the creation, management and destruction of the parts
  3. Changes to the container will likely impact the parts
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is aggregation?

A

Represents a looser coupled has-a relationship between classes where one class contains objects of another class such that:

  1. When the whole is destroyed, the parts can still exist independently.
  2. The whole contains references to it’s parts but doesn’t own or create them.
    Changes to the container might not necessarily affect the parts
17
Q

What is Inversion of Control?

A

An architecture principle that refers to the inversion of the flow of control within an application from internal components to an external entity like a framework.

18
Q

What is idempotency?

A

A function applied multiple times has the same effect such that the results are always the same.

Examples: HTTP GET, PUT & DETETE where as POST is not because it may result in a different outcome or change in the server state.

19
Q

What is true duplication?

A

every change in one instance necessitates the same change to every duplicate of that instance.

20
Q

What is false/accidental duplication?

A

When 2 apparently duplicate sections of code evolve along different paths and eventually become 2 completely different and non-duplicate things.

21
Q

What’s an example of falsely applying the DRY principle when considering true and false duplication?

A

Input forms that look similar. If you use the same code for the form they will not be able to evolve along different paths over time but this very well may be necessary as the application changes.

22
Q

What is the law of Demeter (aka the Principle of Least Knowledge)

A

Only couple to things that you use and avoid coupling to things used by something you use.

Said another way, a method is only allowed to call:
1. Methods within it’s object
2. Method’s passed to the object as a parameter
3. Methods on an object you’ve created yourself.
4. Methods on a component of this object (i.e. an instance variable).

23
Q

What is a potential thing to watch for when considering the law of Demeter?

A

It can lead to a potential violation of the Interface Segregation Principle because when a calls b and b calls c, the b code must be expanded to handle calls to c for a which could potentially bloat b.

24
Q

What is an abstract class?

A

It is a class that cannot be directly instantiated but instead must be extended in order to be instantiated.

It can have default implementations that become available in the classes that extended it.

25
Q

What is the difference between an abstract class and an interface?

A

Typically the methods of an interface must be defined in the class that implements it such that there is no default functionality provided by the interface.