Programming patterns and techniques Flashcards

1
Q

Guard clauses and example

A

Stop a functions execution by using the return keyword when certain conditions are met, continue otherwise.

function someFunction(isLoading){
if(isLoading){
return;
}

//else continue

}

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

Benefits of guard clauses

A

Prevents if-else nesting.
Makes code more legible.

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

What is an early return?

A

When the return keyword is used before the end of a function/method.

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

What is the strategy pattern?

A

Context class and strategy classes
The context class takes an instance of the strategy and can execute methods on the strategy via methods in the context class.

The strategies implement a common abstraction (inherits from an abstract class with abstract methods and properties OR implements a common interface)

Open Closed Principle, Dependency Inversion Principle

The client communicates with the context class.

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

What is the factory pattern/factory method pattern?

A

A factory class is meant to handle object creation of sub-classes that all use the same abstraction.

The abstraction may be:
1) An interface that each sub-class is meant to implement
2) An abstract class with abstract properties and methods that each sub-class is meant to inherit from

The client interfaces with the factory class

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

What is the observer pattern?

A

A subject class adds, removes and executes all observers, which are classes (that implement same interface) that are injected into the subject.

The observers must implement a common abstraction (abstract class with abstract properties and methods should be inherited from OR common interface should be implemented)

Dependency inversion principle, open-closed principle, single responsibility principle

The client should interface with the subject class.

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

What is the singleton pattern?

A

Ensures that only one instance of a class is in the application. Create a getInstance() method that either returns a new instance of the class, or the existing one if it already exists.

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

What are the SOLID principles

A

1) Single Responsibility Principle - class/function/module should be responsible for a single thing within the application.
2) Open-closed: Open for extension, closed for modification. A class, function, module should be extensible without modification being required of the source code.
3) Liskov substitution principle: should be able to replace any parent class instance/object with a child class instance/object - child class should not overwrite parent methods/properties. PHP use final keyword
4) Interface Segregation Principle: A class should not implement an interface unless it needs to expose all the methods/properties listed in the interface to the client.
5) Dependency Inversion principle: High-level modules/classes/functions should depend on abstractions (interface or abstract class) instead of concrete implementations as in dependency injection. These dependencies are object/class instances.

Classes only:
Interface segregation principle
Liskov substitution principle

Functions, classes, modules:
Single responsibility principle
Open closed principle
Dependency Inversion principle

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

Explain the Fluent Interface Pattern

A

Allows for method chaining, when a method(s) return the keyword this.

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

What are abstractions?

A

Interfaces
Abstract classes that implement abstract methods/properties, which child classes inherit from.

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

What is the client and an interface

A

The client is meant to interface with a class that implements a given interface. The interface tells the client which methods/properties are available. In other words, all interface properties and methods should be public.

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

What is UML

A

It stands for Unified Modelling Language.

It is meant to be a graphical representation of a code base, which includes interfaces, classes and relationships, inheritance and interface implementation.

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

Explain dependency injection vs dependency inversion

A

They are similar, where a class instance/function/modules depends on an instance of a different class(es).

Dependency injection: inject concrete class instance/object

Dependency inversion: inject class instance/object which implements an abstraction

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

What is a class vs object

A

An object is an instance of a class

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

What is loose coupling

A

This is when there is minimal interdependence between modules/classes/functions in a system.

Coding to abstractions helps achieve this (dependency inversion)

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

Tight coupling

A

Too much interdependence modules/functions/classes

Coding to concrete implementations causes this.

17
Q

What is separation of concerns?

A

This is a design principle where the source code is broken up into sections. Each section is responsible for addressing a certain concern.

One example - each has it’s own concern:
HTML - structure only
CSS - styling
Javascript - interactions

Another could be a validation class/object that does all validation, OR a custom React hook that only handles local storage.