# Lecture - Imperative Programming TT23, I

> Source: https://ollybritton.com/notes/uni/prelims/tt23/ip/lectures/i/ · Updated: 2023-04-27 · Tags: uni, lecture

### Notes
- Key conceptual tools
	- Decomposition/modulatrity
		- The identification and clarification of the boundaries between the major conceptual components of a system or of its implementation.
		- Each components should play a single well understood role.
		- Components should have well-defined interfaces
	- Abstraction
		- Extract the essential conceptual core of a problem or solution from the mass of detail which is not relevant in a particular context.
		- Components should be viewed as black boxes.
		- Can interchange black boxes with identical specifications.
		- Don’t rely on a component’s implementation details.
	- Reusability
 - Objects
	 - Each ovject has a specification that describes what its operations do, and an implementation that provides code to do it.
	 - To use an object we only need to know the specification or public interface; different implementations can be used interchangeably.
 - Classes
	 - Objects are instances of classes that define the data and operations for a family of similar objects
	 - The power of OOP comes from being able to use many different predefined classes and to make our own classes.
 - Object identity
	 - Multiple instances of a class should have seperate “identities”, changing one should not change another.
	 - Object identities can be copied without copying the whole object. This is known as aliasing.
	 - Scala has `a eq b` to test object identity.
	 - Could be defined using pointers to memory location, but this has issues where clever pointer arithmetic can bypass encapsulation, like in C.
	 - Scala abstracts over this to prevent issues.
 - Encapsulation
	 - The seperation between the external interface of a component and its internal implementation.
	 - Good for debugging because you can ask “who broke their promise” -- were pre- or post-conditions not satisfied?
	 - “Design by contract”; writing specifications using pre- and post-conditions.

### Flashcards (GPT-4)
What is the purpose of decomposition/modularity in software design?::
To identify and clarify the boundaries between major conceptual components of a system or its implementation, allowing for a better-organized and more maintainable codebase.

What is the key principle of abstraction in programming?::
Extracting the essential conceptual core of a problem or solution from the mass of irrelevant details, allowing components to be viewed as black boxes.

What is a benefit of reusability in software design?::
It promotes efficient code development by allowing components to be repurposed across multiple projects or systems.

What is the difference between an object's specification and its implementation?::
The specification describes what an object's operations do, while the implementation provides the actual code to perform those operations.

Why are object identities important in object-oriented programming?::
Object identities allow multiple instances of a class to have separate "identities," ensuring that changes to one instance do not affect others.

What is aliasing in the context of object identity?::
Aliasing refers to copying an object's identity without copying the entire object itself.

What is encapsulation, and why is it useful?::
Encapsulation is the separation between the external interface of a component and its internal implementation, which helps maintain code integrity, simplifies debugging, and enforces modularity.

What does "design by contract" mean in the context of encapsulation?::
Design by contract refers to writing specifications using pre- and post-conditions, which helps ensure that components meet their intended requirements and adhere to their specified interfaces.

How does Scala test object identity?::
Scala uses the `a eq b` syntax to test object identity.

What is the primary advantage of object-oriented programming (OOP)?::
The ability to use many different predefined classes and create custom classes, promoting code reusability, modularity, and maintainability.

---
Olly Britton — https://ollybritton.com. Machine-readable index: https://ollybritton.com/llms.txt
