Lecture - Imperative Programming TT23, VII


  • 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
trait Ordered[T] {
	def compare(a: T): Boolean
}

Then you can use this like

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?


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)?


(C => A) <: (C => B)

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


(B => C) <: (A => C)



Related posts