I was watching a video regarding design patterns and the youtuber made an example of the builder pattern… I didn’t know about the pattern(there is a reason why I was watching the videos); But I had encounter the same type of problems so what I usually did was to return null to the fields I didn’t had their data.

Is it wrong what I was doing?

At the end the builder will make the object with a null data and realistically it takes the same amount of code…

@CasualTee@beehaw.org
link
fedilink
English
21Y

Contrieved examples you usually see in design pattern tutorials do not properly highlight the use case and usefulness of a specific pattern.

What you did might have been fine. You only rarely need the builder design pattern.

It’s useful when the object you want to build has a complex constructor, or several ones, or you want to more easily enforce the self consistency of the created object (some languages make it hard to share initialization code) or you want to hide some internal details (I.e., you are at an API boundary and want to be able to freely change the objects your main object is composed of).

In short, Builder is a way to have a handy interface to an object constructor. It’s nice for users of a library, or if you find yourself repeating a complex constructor invocation often, but you usually do not want to have to maintain such an interface that is, at the end of the day, mostly boilerplate code.

Create a post

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person’s post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you’re posting long videos try to add in some form of tldr for those who don’t want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



  • 1 user online
  • 1 user / day
  • 1 user / week
  • 1 user / month
  • 1 user / 6 months
  • 1 subscriber
  • 1.21K Posts
  • 17.8K Comments
  • Modlog