1
\$\begingroup\$

1st approach:

let whatsNewHandler = WhatsNewHandler(buildNumber: buildNumber)
 whatsNewHandler.showWhatNewScreeen(completion: { whatsNewVC in

      self.present(whatsNewVC, animated: true, completion: nil)
 })

2nd approach, which looks a bit functional:

WhatsNewHandler(buildNumber: buildNumber).showWhatNewScreeen(completion: { whatsNewVC in

      self.present(whatsNewVC, animated: true, completion: nil)               
})

While both allocate the same amount of memory. I like the functional approach, I just do something and that's really it. But the reason I'm asking is I haven't seen anyone write like this. I see things similar to it when you're using a singleton, but this isn't a singleton.

EDIT:

My WhatNewHandler class:

import Foundation
import UIKit



typealias WhatsNewCompletionHandler = (_ whatsNewViewController: WhatsNewViewController) -> ()

class WhatsNewHandler{

    let buildNumber : String
    lazy var route : Router = {
      return Router.whatsNew(buildNumber: self.buildNumber)
    }()

    init(buildNumber: String){
        self.buildNumber = buildNumber
    }

    func showWhatNewScreeen(completion: @escaping WhatsNewCompletionHandler){

        NetworkHandler.sharedInstance.request(route, success: { json,_  in

            guard let json = json else{
                writelog("what's new json was nil")
                return
            }
            if let releaseBuild = ReleaseBuild(json: json){
                let WhatsNewVC = WhatsNewViewController(releaseBuild: releaseBuild)
                WhatsNewVC.modalPresentationStyle = .overFullScreen

                // TEST ALPHA ON REAL DEVICE
                WhatsNewVC.view.backgroundColor = UIColor.black.withAlphaComponent(0.985)
                completion(WhatsNewVC)
            }
            }, failed: {_,_,_ in

        })
    }
}

As for my WhatsNewViewController, kindly assume it to be a Viewcontroller that shows a list of new features added to the app.

\$\endgroup\$
1
\$\begingroup\$

The only difference between the first and the second approach is whether the handler is assigned to a variable or not. None of them is more "functional" than the other. Therefore:

  • If you need a reference to the handler (perhaps to cancel the request) then use the first approach.
  • If you don't need that reference, choose whatever you feel more comfortable with, or what you find better readable.

Some more remarks:

  • The method name showWhatNewScreeen is (apart from the typo) misleading. The method creates a view controller but does not present it. Something like createWhatsNewController might be more appropriate.
  • According to the API Design Guidelines, variable names should be lowerCamelCase, that is violated at let WhatsNewVC = ....
  • The right-hand side of

    whatsNewVC.view.backgroundColor = UIColor.black.withAlphaComponent(0.985)
    

    can be abbreviated to .black.withAlphaComponent(0.985), the type UIColor is then inferred from the context.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.