{
  "slug": "Toxic-mistake-to-use-the-Function-option-pattern-instead-of-the-Builder-pattern-5b363508ad73",
  "title": "Toxic mistake to use the Function option pattern instead of the Builder pattern",
  "subtitle": "Both the function option pattern and builder pattern are super operative design patterns. But as a software engineer, it is so important to use this pattern in the right conditions.",
  "excerpt": "Both the function option pattern and builder pattern are super operative design patterns. But as a software engineer, it is so important to use this pattern in the right conditions.",
  "date": "2023-11-09",
  "tags": [
    "My Experience",
    "Career"
  ],
  "readingTime": "2 min",
  "url": "https://www.linkedin.com/pulse/toxic-mistake-use-function-option-pattern-instead-mobin-shaterian-zoydf",
  "hero": "https://media.licdn.com/dms/image/v2/D4D12AQF8M_JHRxuz4Q/article-cover_image-shrink_600_2000/article-cover_image-shrink_600_2000/0/1699510694591?e=2147483647&v=beta&t=d8Bsy2Ksmgr3SpfQjCYp6RPF75A5tF5pkN5ll1omEw4",
  "content": [
    {
      "type": "paragraph",
      "html": "Both the function option pattern and builder pattern are super operative design patterns. But as a software engineer, it is so important to use this pattern in the right conditions. In this article, I want to say that using the function option instead of the builder pattern is wrong."
    },
    {
      "type": "paragraph",
      "html": "<a href=\"https://medium.com/@MTrax/golang-options-vs-builder-pattern-4123bfcc3c5c\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>The Options pattern can be a good alternative to the Builder pattern for creating objects with many optional parameters, especially if the object has fewer parameters.</strong></a>"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "When using the Function option pattern"
    },
    {
      "type": "paragraph",
      "html": "when we have a lot of parameters that they are optional, it is much better to use this pattern."
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Builder pattern"
    },
    {
      "type": "paragraph",
      "html": "<a href=\"https://devcharmander.medium.com/design-patterns-in-golang-the-builder-dac468a71194\" target=\"_blank\" rel=\"noreferrer noopener\">The motivation behind the Builder pattern is to create a complex object piece by piece. Our goal in this blog is to provide APIs to create complex objects step-by-step.</a>"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Golang Builder Design Pattern"
    },
    {
      "type": "paragraph",
      "html": "<a href=\"https://refactoring.guru/design-patterns/builder\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Builder</strong> is a creational design pattern that lets you construct complex objects step by step. The pattern allows you to produce different types and representations of an object using the same construction code.</a>"
    },
    {
      "type": "paragraph",
      "html": "I saw that some developers used the function option pattern for sending many optional parameters as input and it is true. But when we need to set a parameter and it is essential why get these parameters as an optional function design pattern? For example, we need to build a house. In simplest abstraction, we need a roof, wall, and door. Although we can set a default value for all of these three elements and get them by function option pattern in many cases, we don’t have a default value for them. In this case, it is much better to use the builder pattern and after defining all parameters, build a decided apartment, even in the builder pattern we can check everything will be ok. For example, we can check a The door matches the window."
    },
    {
      "type": "embed",
      "provider": "youtube",
      "url": "https://www.youtube.com/embed/1F0N1_KH7eo?start=100&feature=oembed&start=100"
    },
    {
      "type": "paragraph",
      "html": "<a href=\"https://www.reddit.com/r/golang/comments/waagos/option_pattern_vs_builder_pattern_which_one_is/\" target=\"_blank\" rel=\"noreferrer noopener\">With the builder pattern, you enable the user to mutate the type at any time. This can sometimes make it more difficult to reason about the data. This might be fine for a container holding data, but not for something like a server/service that needs to be immutable after creation.</a>"
    }
  ]
}