• 3 Posts
  • 73 Comments
Joined 1 year ago
cake
Cake day: September 21st, 2024

help-circle




  • Depending on the language exceptions are used in many different ways. Some use it liberally for all kinds of error handling.

    A good feature of Exceptions is you can throw them all the way up the stack and handle them there, giving you loose coupling between the code that calls the dangerous code and the one that catches it.

    Exceptions have a big runtime overhead, so using them for normal control flow and error handling can be a bit meh.

    Using return types can be great, if the language has good support for. For example swift enums are nice for this.

    enum ResultError  {
      case noAnswer;
      case couldNotAsk;
      case timeOut
    }
    
    enum Result {
      case answer: String;
      case error: ResultError
    }
    
    func ask(){
      let myResult = askQuestion(“Are return types useful?”);
      switch myResult {
        case answer: 
          print(answer);
        case error:
           handleError(error);
      }
    }
    
    func handleError(error: ResultError) {
      switch ResultError {
        case noAnswer:
          print(“Received no answer”);
        case couldNot:
          …
      }
    
    }
    

    Using enums and switch means the compiler ensures you handle all errors in a place you expect.


















  • Early Swift was very slow to compile and start. The debugger was nonfunctional.

    Otherwise it was pretty usable. Especially since it got to leverage the huge libraries written for Objective-C.

    Which meant it lacked some basic collection types. A Swift native Set was introduced with Swift 3 IIRC. Before that you had to bridge back and forth between Swift and Objective-C. Sometimes leading to unexpected behavior at runtime.

    In Objective-C if an object reference was nil, you could send it messages (call methods) without a problem. Swift however did away with this. Optionals had to be explicitly unwrapped. So if the annotations weren’t correct, Swift code would crash at runtime where Objective-C would have been fine. Lots of bugs related to that existed.

    Swift peaked around version 4. Since then, they have been adding kitchen sink features and lots of complexity to feel smart.

    I still would have preferred an Objective-C 3.0. Chris Lattner was a C++ guy and never really understood Objective-C culture and strengths.