When should I use a separate class to represent an empty container?

丶灬走出姿态 提交于 2019-12-24 11:36:42

问题


I have recently been going through the book "Scala by Example" in which the author creates an abstract class to represent a set of ints "IntSet" with two subclasses (EmptySet and NonEmptySet) as follows:

abstract class Stack[A] { 
  def push(x: A): Stack[A] = new NonEmptyStack[A](x, this)
  def isEmpty: Boolean
  def top: A
  def pop: Stack[A]
}

class EmptyStack[A] extends Stack[A] {
  def isEmpty = true 
  def top = error("EmptyStack.top")
  def pop = error("EmptyStack.pop")
}

class NonEmptyStack[A](elem: A, rest: Stack[A]) extends Stack[A] {
  def isEmpty = false
  def top = elem
  def pop = rest
}

My question is this: How useful is this paradigm of representing an empty container as its own class instead of creating one concrete class to handle both the empty and non-empty cases?


回答1:


Each implementation is simpler and more readable as the is-empty-check does not have to be done in the implementation. This leads to better code metric values (like cyclomatic complexity) too.

Moreover, It makes the implementations slightly faster in general, since the distinction between empty and non-empty does not have to be done at runtime. As far as I know, Scala's Set applies this technique and implements different types of sets (used depending on their size) to optimize performance.

Obviously this works for immutable data structures only.



来源:https://stackoverflow.com/questions/14197890/when-should-i-use-a-separate-class-to-represent-an-empty-container

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!