Skip to main content

Code sạch hơn với việc dùng Extension trong Swift


Nếu như với ARC sẽ giúp tăng hiệu năng ứng dụng của bạn hơn thì Extension sẽ giúp code của bản trở nên dễ dàng đọc hơn (clean code)

1. Extension là gì?

Trong swift Extension thường được sử dụng những nơi mà bạn muốn thêm các hàm mà không có quyền truy cập vào source code. Ví dụ như bạn muốn mở rộng Int có sẵn của Swift bạn có thể làm như sau :
extension Int {
  func multiply(by multiplier: Int) -> Int {
    return self * multiplier
  }
}
10.multiply(by: 2)
// Returns 20
Việc sử dụng extension không chỉ áp dụng với source không có quyền truy cập mà nó được dùng cho cả source bạn có thể truy cập để code của bạn trở nên dễ đọc, dễ hiểu hơn.

2. Ví dụ tiện lợi của việc dùng Extension

Ở đây chúng ta sẽ tiền hành xây dựng một class Binary Search Tree.

Bước 1: Xây dựng Protocol

Binary Search Tree là một cấu trúc dữ liệu dừng để lưu trữ và tìm kiếm dữ liệu. Nó dựa vào hoạt động của các Node, là nơi lưu trữ giá trị và tham chiếu tới các node khác của tree. Ngoài ra tree cũng có một số phương thức như add, remove, find dữ liệu trong cấu trúc. Giờ chúng ta bắt đầu với việc tạo ra 2 protocol
protocol SearchTree {
  associatedtype DataType: Comparable
  
  func add(_ data: DataType) -> Bool
  func contains(_ data: DataType) -> Bool
  func find(_ data: DataType) -> DataType?
  func delete(_ data: DataType) -> DataType?
  func remove(_ data:DataType) -> Bool
}

protocol BinaryNode {
  associatedtype DataType: Comparable

  var value: DataType { get set }
  var parent: Self? { get set }
  var left: Self? { get set }
  var right: Self? { get set }
  var isLeaf: Bool { get }
}

extension BinaryNode {
  var isLeaf: Bool {
    return self.left == nil && self.right == nil
  }
}
Trong đoạn code trên, protocol SearchTree chỉ định ra những hàm mà Search Tree sẽ triển khai, và cũng chỉ định ra kiểu của dữ liệu chúng ta chọn để lữu trữ trong tree phải implement Comparable protocol. BinaryNode protocol chỉ định các thuộc tính cần để biểu diễn node của tree mà chúng ta có thể xử lý và tính toán.

Bước 2: Implement các protocol

Tạo ra class implement các phương thức trong protocol ở trên :
class BST<DataType: Comparable>: SearchTree {
  private typealias NodeType = BSN<DataType>
  private var root: NodeType?

  init(rootValue: DataType) {
    self.root = NodeType(value: rootValue)
  }

  init(rootValue: DataType, leftTree: BST, rightTree: BST) {
    self.root = NodeType(value: rootValue, left: leftTree.root, right: rightTree.root)
  }

  private init(rootNode: NodeType) {
    self.root = rootNode
  }

  func add(_ data: DataType) -> Bool {
    return false
  }

  func contains(_ data: DataType) -> Bool {
    return false
  }

  func find(_ data: DataType) -> DataType? {
    return nil
  }

  func delete(_ data: DataType) -> DataType? {
    return nil
  }

  func remove(_ data:DataType) -> Bool {
    return false
  }

  private final class BSN<DataType: Comparable>: BinaryNode {
    var value: DataType
    var parent: BSN<DataType>?
    var left: BSN<DataType>?
    var right: BSN<DataType>?

    init(value: DataType) {
      self.value = value
    }

    init(value: DataType, left: BSN?, right: BSN?) {
      self.value = value
      self.left = left
      self.right = right
    }
  }
}
Nếu như triển khai code như thế này sẽ khiến cho chúng ta khó có một cái nhìn tổng quan. Sẽ khó để tìm kiếm khi cần sửa đổi một trong các phương thức của SearchTree protocol. Bạn sẽ tự đặt ra câu hỏi rằng: " Chúng ta sẽ thêm các phương thức mới ở đâu nếu chúng ta muốn có thể tìm kiếm chúng nhanh chóng sau này? "

Bước 3: Refactor sử dụng Extension

Thực chất Extension không được sinh ra để giải quyết vấn đề code dễ đọc hơn nhưng chúng ta có thể sử dụng theo cách này như một hiệu ứng phụ của nó.
class BinarySearchTree<DataType: Comparable> {
  fileprivate typealias NodeType = BinarySearchNode<DataType>
  private var root: NodeType?

  init(rootValue: DataType) {
    self.root = NodeType(value: rootValue)
  }

  init(rootValue: DataType, leftTree: BinarySearchTree, rightTree: BinarySearchTree) {
    self.root = NodeType(value: rootValue, left: leftTree.root, right: rightTree.root)
}

  private init(rootNode: NodeType) {
    self.root = rootNode
  }
}

extension BinarySearchTree: SearchTree {
  func add(_ data: DataType) -> Bool {
    return false
  }

  func contains(_ data: DataType) -> Bool {
    return false
  }

  func find(_ data: DataType) -> DataType? {
    return nil
  }

  func delete(_ data: DataType) -> DataType? {
    return nil
  }

  func remove(_ data:DataType) -> Bool {
    return false
  }
}

private extension BinarySearchTree {
  final class BinarySearchNode<DataType: Comparable>: BinaryNode {
    var value: DataType
    var parent: BinarySearchNode<DataType>?
    var left: BinarySearchNode<DataType>?
    var right: BinarySearchNode<DataType>?

    init(value: DataType) {
      self.value = value
    }

    init(value: DataType, left: BinarySearchNode?, right: BinarySearchNode?) {
      self.value = value
      self.left = left
      self.right = right
    }
  }
}
Có thể thấy code trở lên clean hơn việc không sử dụng extension. Bât cứ những thứ gì chỉ thuộc về class này sẽ được nằm trong scope của nó, bao gồm khởi tạo, thuộc tính và phương thức không thực hiện các protocol tại đây. Chúng ta sử dụng extension class để triển khai các phương thức trong protocol giúp cho việc quản lý code trở lên dễ dàng hơn.

Nguồn tham khảo :

https://medium.com/@JimmyMAndersson/using-swift-extensions-to-clean-up-our-code-1aed32da24bc?fbclid=IwAR24d0rdINQYHc8R3lqF5Fd8p2V-iEueSVZwoAmEvlstboPRS-v0_nKeeK8

Comments

Popular posts from this blog

Swift GCD part 1: Thread safe singletons

Preview Singletons are entities, referenced to the same instance of a class from everywhere in your code. It doesn't matter if you like them or not, you will definitely meet them, so it's better to understand how they work. Constructing and handling a set of data doesn't seem to be a big challenge at first glance. The problems appear when you try to optimise the user experience with background work and your app starts acting weird. ??‍♂️ After decades of watching your display mostly with a blank face, you finally realize that your data isn't handled consistently by the manager because you're accessing it (running tasks on it) from multiple threads at the same time. So you really do have to deal with making your singletons thread safe. This article series is dedicated to thread handling using Swift. In the first part below you will get a comprehensive insight into som...

Thread safe singleton’s in Swift

What are singletons? — Singleton is design patterns which says that there should be only one instance of the class for the lifetime of the application. One the best example of Singleton is AppDelegate . How to write a singleton class ? class DefaultDict{ private var dict:[String:Any] = [:] public static let sharedManager = DefaultDict() private init(){ } public func set(value:Any,key:String){ dict[key] = value } public func object(key:String) -> Any?{ dict[key] } public func reset(){ dict.removeAll() } }   Testing singleton class under concurrent circumstances. We are going to write an example where we will set values in dict from various threads and even try to access some with different threads. When we do this we will encounter a crash. If you look closely it will be because of race condition and the crash will be on line set(value:Any,key:String) . class ViewController: UIViewController { ...

Frame vs Bounds in iOS

This article is a repost of an answer I wrote on Stack Overflow . Short description frame = a view’s location and size using the parent view’s coordinate system ( important for placing the view in the parent) bounds = a view’s location and size using its own coordinate system (important for placing the view’s content or subviews within itself) Details To help me remember frame , I think of a picture frame on a wall . The picture frame is like the border of a view. I can hang the picture anywhere I want on the wall. In the same way, I can put a view anywhere I want inside a parent view (also called a superview). The parent view is like the wall. The origin of the coordinate system in iOS is the top left. We can put our view at the origin of the superview by setting the view frame’s x-y coordinates to (0, 0), which is like hanging our picture in the very top left corner of the wall. To move it right, increase x, to move it down increase y. To help me remember bound...