{
  "@context": "https://schema.org",
  "@type": "BlogPosting",
  "@id": "https://www.vidyasource.com/blog/when-you-do-want-none/",
  "url": "https://www.vidyasource.com/blog/when-you-do-want-none/",
  "mainEntityOfPage": "https://www.vidyasource.com/blog/when-you-do-want-none/",
  "headline": "When You Do Want None",
  "description": "A quick tip for Scala developers for handling empty and whitespace strings in their code.",
  "datePublished": "2015-02-25T00:00:00.000Z",
  "author": {
    "@type": "Person",
    "name": "Neil Chaudhuri",
    "jobTitle": "President",
    "url": "https://www.linkedin.com/in/neil-chaudhuri/",
    "sameAs": "https://www.linkedin.com/in/neil-chaudhuri/"
  },
  "publisher": {
    "@id": "https://www.vidyasource.com/#organization"
  },
  "image": "https://www.vidyasource.com/img/blog/mixalot.jpg",
  "keywords": [
    "Scala",
    "Programming",
    "Functional Programming"
  ],
  "articleBody": "In the applications I typically develop, I only care when strings are meaningful or when they aren’t because they are\n`null` (or `nil`, `None`, or `Null` depending on the language). Empty strings and strings containing only whitespace are this\nweird, in-between third category I really would prefer not to deal with. When I go to McDonald’s, I want to focus on the\nburgers and fries. I want absolutely nothing to do with the [Filet-O-Fish](https://www.youtube.com/watch?v=6bJOIqVAD-s).\n\nScala’s [Option](http://www.scala-lang.org/api/2.11.4/index.html#scala.Option) gives us a neat way to avoid meaningless strings if we wish.\n\nI have [blogged](https://www.vidyasource.com/blog/know-your-options/) about `Option` before.\nIt helps us avoid null in an elegant, typesafe way at compile-time. The collection-like semantics of `Option` helps us turn\n[Some](http://www.scala-lang.org/api/current/index.html#scala.Some)(*meaninglessString*) into [None](http://www.scala-lang.org/api/current/index.html#scala.None$)\n(not to be confused with Python’s [None](http://stackoverflow.com/questions/19473185/what-is-a-none-value))\nso we can deal with it as if it were an organic `None` value.\n\nIt’s really pretty easy.\n\n~~~scala\nval option: Option[String] = Some(\"\") \n//or Some(\"Vidya\"), Some(\"   \"), None, or whichever Option[String] you want\nval result: String = option\n  .filter(_.trim.nonEmpty)\n  .map(string => s\"You found a meaningful string $string\")\n  .getOrElse(\"You found None or an empty or whitespace-only string.\")\nprintln(result)\n~~~\n\nIf you know Scala, you can see exactly what happens here. Check out that call to `filter`. We typically associate it\nwith collections where `filter` removes all elements from the collection that don’t pass the condition. Option offers an\nanalogous `filter` that returns None if the value “inside” Some doesn’t pass the condition. In the case of an empty or\nwhitespace-only string, trimming it keeps it or makes it empty, so the `filter` test doesn’t pass and yields None. This is a really\nconcise way of letting us deal with only two categories of strings--meaningful or not--instead of three. One place where\nthis could come in handy is backend form validation of mandatory fields in web applications.\n\nIf you have the luxury of not having to distinguish between empty-ish strings and null, keep this tip in mind.\nUnless you’re Sir Mix-A-Lot, you actually might [want None](https://www.youtube.com/watch?v=_JphDdGV2TU)."
}