# Lecture - Imperative Programming TT23, VII

> Source: https://ollybritton.com/notes/uni/prelims/tt23/ip/lectures/vii/ · Updated: 2023-05-11 · Tags: uni, lecture

- Code smells
	- Not usually bugs; code can still work but can indicate weaknesses in design that may be slowing down development or increasing the risk of bugs in the future.
	- Bloaters
		- Long method
		- Large class
		- Long parameter lists
	- Expendables
		- Code duplication
		- Excessive comments
	- Couplers
		- Inappropriate intimacy (violating encapsulation)
		- Feature envy (over-using another class)
- Polymorphism
	- Constructs in programming languages can process objects of different data types in different ways
	- Overloading vs subtyping
	- Type erasure
		- In Scala, type parameters are removed before actually evaulating the program
		- The compiler inserts safe downcasts where needed

```scala
trait Ordered[T] {
	def compare(a: T): Boolean
}
```

Then you can use this like

```scala
def sort[T <: Ordered[T]](list: List[T]) = ???
```
```
A <: B  then  (C => A) <: (C => B)
A <: B  then  (B => C) <: (A => C)
```

### Flashcards
What is a “code smell”?::
Not necessarily a bug but an indicator of a weakness in design that might lead to more bugs.

What is “type erasure” in Scala?::
Where type parameters are removed before the program is actually evaluated, i.e. types are only used at compile time.

Can you give the definition of the `Ordered` trait?::
```scala
trait Ordered[T] {
	def compare(a: T): Boolean
}
```

Suppose `A <: B`. Then what can you deduce about functions of the type `(C => A)` and  `(C => B)`?::
```scala
(C => A) <: (C => B)
```

Suppose `A <: B`. Then what can you deduce about functions of the type `(A => C)` and `(B => C)`?::
```scala
(B => C) <: (A => C)
```

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