Skip to content

Instantly share code, notes, and snippets.

@claybridges
Created March 24, 2020 20:28
Show Gist options
  • Save claybridges/6c3abcaf361fb515c86c31b9640e97bf to your computer and use it in GitHub Desktop.
Save claybridges/6c3abcaf361fb515c86c31b9640e97bf to your computer and use it in GitHub Desktop.

Revisions

  1. claybridges created this gist Mar 24, 2020.
    41 changes: 41 additions & 0 deletions n10ns.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    ## Notification observations and handlers

    For a class, each notification observation should be handled by a single method. The name of that method should be `handle` appended with the name of the notification being observed. For instance, for notification name
    ```
    NSNotification.Name.LUUICachedPaymentMethodDidUpdate
    ```
    the handler would be
    ```
    private func handleCachedPaymentMethodDidUpdate(...)
    ```
    Skip whatever LU/LUUI prefixes exist.

    **Only the notification observation should call/reference the handler method.**

    Here's how the resultant code would look:
    ```
    // somewhere
    NotificationCenter.default.addObserver(self, selector: #selector(handleCachedPaymentMethodDidUpdate),
    name: NSNotification.Name.LUUICachedPaymentMethodDidUpdate,
    object: nil)
    // later
    private func handleCachedPaymentMethodDidUpdate(_ notification: NSNotification) {
    // 🧹 handle it!
    }
    ```

    All notification handler methods should be private, and should be grouped together in a section delineated by
    ```
    // MARK: - Notification handlers
    ```

    ## Motivation

    When trying to comprehend code, especially when debugging, it can become confusing to have a method that is called by:
    - multiple notifications
    - both by a notification and direct calls

    Breaking out notification handlers on their own is a simple, consistent way to structure this for clarity.