The Most Misused Design Pattern in Swift

Поделиться
HTML-код
  • Опубликовано: 1 ноя 2024

Комментарии • 46

  • @karlostj4683
    @karlostj4683 Год назад +14

    There are ways to code that potentially cause problems, and there are ways to avoid actually causing those problems. The reality is: Every code "pattern" can be abused. So the real lesson to learn is: Never get emotionally attached to a piece of code. And as a corollary: Never get emotionally outraged over a piece of someone else's code. Children get emotional about things; adults are better than that.

  • @JonnyJonvilleJonathan
    @JonnyJonvilleJonathan Год назад +1

    As a jr ios dev I would love an update on what to use instead of singletons, great video!

  • @Alex-lp6bg
    @Alex-lp6bg Год назад +6

    Great video! Now do an episode on CLEAN architecture and how many layers one has to change every time one needs to add a feature.. it's depressing... entire layers that are just forwarding calls..

    • @seanallen
      @seanallen  Год назад +4

      I’m not a huge fan of super intricate architectures with tons of layers. I’m sure they have their place, but most of the time it’s more trouble than it’s worth.

    • @Alex-lp6bg
      @Alex-lp6bg Год назад

      ​@@seanallen exactly...

  • @oshinvats7304
    @oshinvats7304 Год назад +2

    I only use singletons for my core data stacks, which then uses combine to feed the rest of my application

  • @bitwisedevs469
    @bitwisedevs469 8 месяцев назад +1

    The same with Android, people tend to use it to store state but it can be really huge headache if not properly initialized due to lifecycles. Thanks for the information.

    • @seanallen
      @seanallen  8 месяцев назад

      Happy to help!

  • @AK907
    @AK907 Год назад +4

    Sean, love the content. Been following for years now and have your courses. I'm just a hobbyist who codes on the side for fun. Something I struggle with is learning new stuff with limited information online that I can find. Example for me right now is trying to drag and drop between lists in SwiftUI. I try diving into the documentation and find myself just getting more confused at times. Do you have any suggestions on learning new topics? Thought it might be an interesting video as well, having you learn something with us following along.

  • @WestCoastAce27
    @WestCoastAce27 Год назад +3

    Great job.
    I’m not one of those proposing ‘gotcha’ interview questions… but if someone showed up and badmouthed UserDefaults and said they should not be used, I’d probably vote no on hiring. Apple knew exactly what they were creating. And just like the girl with the hammer, anything can be misused.

  • @slmille4
    @slmille4 Год назад +3

    To help developers understand how singletons can go wrong, it's important to consider the problem of shared mutable state. Thankfully we now have actors that makes this easier, but with classes singletons can be accessed globally from any thread and aren't thread safe.

  • @FloWritesCode
    @FloWritesCode Год назад +5

    I like your balanced overview! Personally I'm not the biggest fan of the Singleton pattern, but I actually use it quite a bit in case where multiple instances really don't make sense. Alternatively I prefer to use an enum with static functions, so there can just never be an instance of the thing.

    • @seanallen
      @seanallen  Год назад +1

      Thanks Flo! Good tip about the enums.

  • @saeedrahmatolahi462
    @saeedrahmatolahi462 Год назад

    That was amazing and you explained it very nice thank you so much man ❤

  • @xyzoooo401
    @xyzoooo401 Год назад

    Hi Sean, love ur short and sweet easy to learn videos.
    Ive been watching ur videos for several years.
    Thanks a lot for all the info you've shared over time.
    I have a small suggestion regarding this video.
    You could dive into a code snippet explaining the cons more. Like multiple objects updating a variable in a singleton.
    Also I agree DI is a whole other concept. DI is the secret sauce for achieving great code architecture + testability. Looking forward to your sharing.

  • @abdorizak
    @abdorizak Год назад

    i really like the way you explain the Singleton

  • @KFrench1123
    @KFrench1123 Год назад

    Thanks, Sean. Looking forward to the DI video too :)

  • @petrosdavtyan308
    @petrosdavtyan308 Год назад +1

    Thanks for content just one point UserDefaults is not a singleton as it has a public constructors, init() and init?(suiteName suitename: String?)

  • @donathmm3881
    @donathmm3881 Год назад +1

    Good explained

  • @JasonMitchellAZ
    @JasonMitchellAZ Год назад

    Great explanation of the pros and cons

  • @Z1ew-k1q
    @Z1ew-k1q Год назад

    thank you bro, how DI will solve singleton

  • @KT-oz1md
    @KT-oz1md Год назад

    Thank you!

  • @jamesmajors530
    @jamesmajors530 Год назад

    I have to point out that if a singleton is created using 'static let' with 'private init' the instance is lazily created, atomic, and thread-safe. If it is created with 'static var', it will NOT be thread safe but still lazily created and atomic.

  • @danvilela
    @danvilela Год назад

    Omg I love apple! Calling that “standard” instead of “instance” makes soooo much more sense! That’s why I want to migrate from a react career to ios.. swift seems marvelous!

    • @muha990
      @muha990 9 месяцев назад

      Can't quite tell if this is sarcasm but this is horrible naming in my opinion.

  • @gordonw8992
    @gordonw8992 Год назад +4

    "The most misused design pattern in iOS dev" would be a more accurate title for this video, as you said there is nothing wrong about singleton pattern itself.

  • @amdizle28
    @amdizle28 Год назад

    Great info as usual!!❤

  • @rickharold7884
    @rickharold7884 Год назад +1

    Right on!

  • @yuno3364
    @yuno3364 Год назад +1

    The evolution of singleton: New coder: YAY SINGLETONS EVERYWHERE / Amateur coder: no never use it!!! / Veteran: Yeah they are ok

    • @strictnonconformist7369
      @strictnonconformist7369 Год назад

      Expert (higher comprehension of reality than merely a veteran, that has learned a lot from painful experience and actively engaged in searching out how to avoid making the same mostakes repeatedly): never creates new ones in applications, removes those not in external libraries that are already there previously, as time permits, because it’s actually easy to design an arbitrarily large system with a single instance of an object instead of using the OO fancy renamed version of a global variable.

  • @folahanmikolawole8219
    @folahanmikolawole8219 Год назад

    Hi Sean, would you be open to doing a tutorial on combine anytime soon?
    I use RxSwift most of the time when I work and would love to learn about combine and transition to that but I don’t want to yet just because I’m looking for the right resource to teach me and I’m sure it’s you because I like how you teach and break down concepts.

  • @foilhat
    @foilhat Год назад

    Can I at least use singletons for my preview data? Please?

  • @kohjiarong6452
    @kohjiarong6452 Год назад

    Thanks for the video. If uniqueness is the key reason for using singleton, then how do I architect a layer that I would like to assess from every where within my app but uniqueness is not required. An example of such use case is like FirebaseManager, or TwitterClient, where these files are the central classes to handle firebase and twitter networking calls.

  • @ChristopherCricketWallace
    @ChristopherCricketWallace Год назад

    practically every 3rd party library exposes their public functions via a singleton (especially for API services)

  • @meamanik
    @meamanik Год назад +1

    I love singleton. Bummer it's not a good practice.

  • @AmCanTech
    @AmCanTech Год назад

    like sharedPreferences in android?

  • @ToddHoff
    @ToddHoff Год назад +1

    I don't think of a singleton as requiring only one instance; it's more of a way to access global services. It solves the service discovery problem. It's really no different than scoping through a namespace. For example, you can unit test through the class without using the singleton. An infinite chain of passing references is far uglier IMHO. Thread safety is not an argument because any shared object must deal with thread safety regardless of how it's accessed. If the once instance thing is a hangup, you can create the class and then put the singleton in another ClassService type class.

  • @ioscaleb
    @ioscaleb 4 месяца назад

    A huge hole in the narrative here is that UserDefaults is not, as far as I can tell, a singleton. To recap for those who didn't get it the first dozen times, a singleton is a class that can only be instantiated _once_. UserDefaults is not such a class. Yes, it has a `.standard` method that returns a shared object, but you can still instantiate as many more instances of UserDefaults as you like (and there are good reasons that you might want to do that). Similarly, the other classes that you mention as examples, FileManager and URLSession, are also not singletons.

  • @victorriurean
    @victorriurean Год назад

    Nice

  • @ali-dp7wr
    @ali-dp7wr Год назад +1

    👌

  • @MaximKorobov
    @MaximKorobov Год назад

    Worst Design Patter in iOS Dev is PIDOR. But please don't make a video about it.