Upgrade to Pro — share decks privately, control downloads, hide ads and more …

実践!Swift API Design Guidelinesに基づいた簡潔明瞭なAPIの作り方

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for Yusaku Nishi Yusaku Nishi
September 26, 2023

実践!Swift API Design Guidelinesに基づいた簡潔明瞭なAPIの作り方

後夜祭 iOSDC Japan 2023 登壇資料
https://andpad.connpass.com/event/290856/

コードを読みやすく・挙動を予測しやすくするために、SwiftのAPI Design Guidelinesを活用しましょう。

本トークでは実際のコード例をもとに

- ガイドラインの示すAPIの理想像とは?
- API設計における大事な観点とは?

を解説します。
APIの構成要素すべてを洗練させ、簡潔明瞭で美しいAPIをデザインしましょう!

Avatar for Yusaku Nishi

Yusaku Nishi

September 26, 2023
Tweet

More Decks by Yusaku Nishi

Other Decks in Programming

Transcript

  1.  let string = input.message.trim() let mail = try await

    Mailer.sendMail(string, to: "Taro") mailBox.update(mail: mail, bottom: false) ԿΛτϦϛϯά͍ͯ͠Δʁ input.messageࣗମΛՃ޻͍ͯ͠Δʁ ԿΛ͍ͯ͠Δʁ bottom: falseͱ͸ʁ trueʹ͢ΔͱͲ͏ͳΔʁ ϝʔϧΞυϨεͰ͸ͳ͘ਓ໊ʁ 🤔
  2. let message = input.message.trimmingWhitespaces() let sentMail = try await Mailer.send(message,

    toAddress: "Taro") mailBox.insert(sentMail, at: .top)  🙂 Ҿ਺ؒҧ͑ͯΔʂ
  3.  let mail = try await Mailer.sendMail(string, to: "...") mailBox.update(mail:

    mail, bottom: false) let string = input.message.trim()
  4. let string = input.message.trim() func trim() -> String  Q.

    APIͷ໾ׂ͸ʁ Q. ࣗ਎Λมߋ͢Δ͔ʁ ༨നΛτϦϛϯάͨ͠จࣈྻΛฦ͢ Q. ৑௕͞͸ͳ͍͔ʁ Q. ৘ใ͸଍Γ͍ͯΔ͔ʁ
  5. let string = input.message.trimWhitespaces() func trimWhitespaces() -> String  Q.

    APIͷ໾ׂ͸ʁ Q. ࣗ਎Λมߋ͢Δ͔ʁ ༨നΛτϦϛϯάͨ͠จࣈྻΛฦ͢ ͠ͳ͍ Q. ৑௕͞͸ͳ͍͔ʁ Q. ৘ใ͸଍Γ͍ͯΔ͔ʁ FEɺJOHܗʹ͢Δ
  6.  let string = input.message.trimmingWhitespaces() func trimmingWhitespaces() -> String Q.

    APIͷ໾ׂ͸ʁ Q. ࣗ਎Λมߋ͢Δ͔ʁ ͠ͳ͍ Q. ৑௕͞͸ͳ͍͔ʁ 0, Q. ৘ใ͸଍Γ͍ͯΔ͔ʁ 0, FEɺJOHܗʹ͢Δ ༨നΛτϦϛϯάͨ͠จࣈྻΛฦ͢
  7.  let mail = try await Mailer.sendMail(string, to: "...") mailBox.update(mail:

    mail, bottom: false) let string = input.message.trimmingWhitespaces()
  8.  let mail = try await Mailer.sendMail(string, to: "...") func

    sendMail( _ message: String, to destinationAddress: String ) async throws -> Mail Q. APIͷ໾ׂ͸ʁ Q. ෭࡞༻͸͋Δ͔ʁ ࢦఆͷѼઌʹϝʔϧΛૹ৴͢Δ ͋Δ Q. ৑௕͞͸ͳ͍͔ʁ Q. ৘ใ͸଍Γ͍ͯΔ͔ʁ to͚ͩͰ͸ͲΜͳจࣈྻͳͷ͔ར༻ଆͰ෼͔Γʹ͍͘ ໋ྩܗʹ͢Δ ܕ৘ใ͕ऑ͍ͱى͖͕ͪ Any, Int, String, ...
  9.  func sendMail( _ message: String, toAddress destinationAddress: String )

    async throws -> Mail Q. APIͷ໾ׂ͸ʁ Q. ෭࡞༻͸͋Δ͔ʁ ࢦఆͷѼઌʹϝʔϧΛૹ৴͢Δ ͋Δ Q. ৑௕͞͸ͳ͍͔ʁ Mail͸ແͯ͘΋఻ΘΔʢMailer͔ΒਪଌͰ͖Δʣ Q. ৘ใ͸଍Γ͍ͯΔ͔ʁ let mail = try await Mailer.sendMail(string, toAddress: "...") ໋ྩܗʹ͢Δ ܕ৘ใ͕ऑ͍ͱى͖͕ͪ Any, Int, String, ... to͚ͩͰ͸ͲΜͳจࣈྻͳͷ͔ར༻ଆͰ෼͔Γʹ͍͘
  10.  func send( _ message: String, toAddress destinationAddress: String )

    async throws -> Mail Q. APIͷ໾ׂ͸ʁ Q. ෭࡞༻͸͋Δ͔ʁ ࢦఆͷѼઌʹϝʔϧΛૹ৴͢Δ ͋Δ Q. ৑௕͞͸ͳ͍͔ʁ Mail͸ແͯ͘΋఻ΘΔʢMailer͔ΒਪଌͰ͖Δʣ Q. ৘ใ͸଍Γ͍ͯΔ͔ʁ let mail = try await Mailer.send(string, toAddress: "...") ໋ྩܗʹ͢Δ ܕ৘ใ͕ऑ͍ͱى͖͕ͪ Any, Int, String, ... to͚ͩͰ͸ͲΜͳจࣈྻͳͷ͔ར༻ଆͰ෼͔Γʹ͍͘
  11.  func update(mail: Mail, bottom: Bool) Q. APIͷ໾ׂ͸ʁ Q. ࣗ਎Λมߋ͢Δ͔ʁ

    ϝʔϧΛϝʔϧϘοΫεͷτοϓʹૠೖ͢Δ Q. ৑௕͞͸ͳ͍͔ʁ Q. ৘ใ͸଍Γ͍ͯΔ͔ʁ mailBox.update(mail: mail, bottom: false)
  12.  func insert(mail: Mail, bottom: Bool) Q. APIͷ໾ׂ͸ʁ Q. ࣗ਎Λมߋ͢Δ͔ʁ

    ϝʔϧΛϝʔϧϘοΫεͷτοϓʹૠೖ͢Δ Q. ৑௕͞͸ͳ͍͔ʁ Q. ৘ใ͸଍Γ͍ͯΔ͔ʁ mailBox.insert(mail: mail, bottom: false) ໨తޠ
  13.  func insertMail(_ mail: Mail, bottom: Bool) Q. APIͷ໾ׂ͸ʁ Q.

    ࣗ਎Λมߋ͢Δ͔ʁ ϝʔϧΛϝʔϧϘοΫεͷτοϓʹૠೖ͢Δ ͢Δ Q. ৑௕͞͸ͳ͍͔ʁ Q. ৘ใ͸଍Γ͍ͯΔ͔ʁ bottom: false͸ᐆດ mailBox.insertMail(mail, bottom: false) ໋ྩܗʹ͢Δ enumʹͯ͠ΈΔ
  14.  func insertMail(_ mail: Mail, at position: InsertPosition) Q. APIͷ໾ׂ͸ʁ

    Q. ࣗ਎Λมߋ͢Δ͔ʁ ϝʔϧΛϝʔϧϘοΫεͷτοϓʹૠೖ͢Δ ͢Δ Q. ৑௕͞͸ͳ͍͔ʁ Q. ৘ใ͸଍Γ͍ͯΔ͔ʁ Mail͸ແͯ͘΋఻ΘΔ bottom: false͸ᐆດ mailBox.insertMail(mail, at: .top) ໋ྩܗʹ͢Δ enumʹͯ͠ΈΔ
  15.  func insert(_ mail: Mail, at position: InsertPosition) Q. APIͷ໾ׂ͸ʁ

    Q. ࣗ਎Λมߋ͢Δ͔ʁ ϝʔϧΛϝʔϧϘοΫεͷτοϓʹૠೖ͢Δ ͢Δ Q. ৑௕͞͸ͳ͍͔ʁ Q. ৘ใ͸଍Γ͍ͯΔ͔ʁ Mail͸ແͯ͘΋఻ΘΔ bottom: false͸ᐆດ mailBox.insert(mail, at: .top) ໋ྩܗʹ͢Δ enumʹͯ͠ΈΔ
  16.  string = input.message.trimmingWhitespaces() string, toAddress: "...") mail = try

    await Mailer.send( let let mailBox.insert(mail, at: .top)
  17.  message = input.message.trimmingWhitespaces() message, toAddress: "...") mail = try

    await Mailer.send( let let mailBox.insert(mail, at: .top) mailBox.insert(mail, at: .top)
  18.  sentMail = try await Mailer.send(message, toAddress: "...") message =

    input.message.trimmingWhitespaces() let let mailBox.insert(sentMail, at: .top)
  19.  sentMail = try await Mailer.send(message, toAddress: "...") message =

    input.message.trimmingWhitespaces() let let let string = input.message.trimString() let mail = try await Mailer.sendMail(string, to: "...") mailBox.update(mail: mail, bottom: false) 🤔 🙂 mailBox.insert(sentMail, at: .top)