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

コードの平準化を目指して / To the world of Heijunka

Elvis Shi
October 30, 2019

コードの平準化を目指して / To the world of Heijunka

Elvis Shi

October 30, 2019
Tweet

More Decks by Elvis Shi

Other Decks in Programming

Transcript

  1. } var employedBy = "YUMEMI Inc." var job = "iOS

    Tech Lead" var favoriteLanguage = "Swift" var twitter = "@lovee" var qiita = "lovee" var github = "el-hoshino" var additionalInfo = """ ࡓ͑Χϊөը࠷ߴʹྑ͔ͬͨ """ final class Me: Developable, Talkable {
  2. ༗څऔΓ์୊੍౓ w ͍ͭͰ΋ɺ͍ͭ·Ͱ΋ɺ༗څ͕औΕΔ w ࣗ෼ͷইප w Ո଒ͷհޢҭࣇ w ϦϑϨογϡ w

    FUD w ͍ͭͰ΋ɺ୭Ͱ΋ɺٸʹٳΉՄೳੑ͕͋Δ w ಛఆͷਓ͕͍ͳ͍ͱ࢓ࣄ͕ճΒͳ͔ͬͨΒΞ΢τ w ඞͣ୭͔୅ΘΓʹͳΔਓ͕͍ͳͯ͘͸ͳΒͳ͍
  3. w ඪ४Խ w ։ൃ؀ڥ΍ϚχϡΞϧΛڞ௨Խ͢Δ w ಉ͡ϥΠϒϥϦʔΛར༻͢Δ w ಉ͡ϑΝΠϧΛྲྀ༻͢Δ w ಉ͡ઃܭΛద༻͢Δ

    w ΫϥΠΞϯτͷ౎߹͕͋Γɺݶք͕͋Δ w ฏ४Խ w ΤϯδχΞಉ࢜ͷ͹Β͖ͭΛ཈੍͢Δ  w ୭Ͱ΋͙͢ʹಡΊΔίʔυΛॻ͘ w ୭Ͱ΋͙͢ʹอकͰ͖ΔίʔυΛॻ͘ w ୭Ͱ΋͙͢ʹ௥Ճ࣮૷Ͱ͖ΔίʔυΛॻ͘ w ΫϥΠΞϯτͷ౎߹ʹΑΒͣɺશͯͷϓϩδΣΫτʹ࢖͑Δ ɿ੡଄ۀʹ͓͚Δʮฏ४Խʯͷҙຯͱ͸ͪΐͬͱҧ͏͕ɺʮ͹Β͖ͭΛ཈੍ʯͱ͍͏ҙຯͰ͸ಉ͡ΞϓϩʔνͰ͋Δ
  4. extension String { func isZeroPrefix() -> Bool { return !hasPrefix("0")

    } } ;FSPͰ࢝·ΔͬͯݴͬͯΔ͕ ઌ಄ͷจࣈ͸Ͱͳ͍ͱฦͯ͠Δ ໊લͱ࣮૷ΛҰகͤ͞Δ
  5. extension String { func isNotBeginningWith0() -> Bool { return !hasPrefix("0")

    } } ໊લΛͰ࢝·͍ͬͯͳ͍ʹ௚͢ ໊લͱ࣮૷ΛҰகͤ͞Δ
  6. ୭Ͱ΋͙͢ʹอकͰ͖ΔίʔυΛॻ͘ • ςετʂʂʂ • ৽نͷίʔυ͸ͱΓ͋͑ͣςετΛॻ͘ • ςετ͕ॻ͚ΔΑ͏ʹ෭࡞༻౳Λࣗવʹ஫ҙͰ͖Δ • طଘͷίʔυʹςετΛ௥Ճ͍ͯ͘͠ •

    ςετॻ͚Δ΋ͷ͔ΒͰ͍͍ͷͰͱΓ͋͑ͣॻ͘ • ಺༰͕׬ᘳʹͰ͖ͳͯ͘΋ͱΓ͋͑ͣॻ͚Δ΋ͷΛॻ͘ • ςετίʔυॻ͍ͯΔ͚ͩͰؾ෇͚Δόά΋͋Δ
  7. 4JOHMF3FTQPOTJCJMJUZ1SJODJQMF Ϋϥεʹมߋ͕ى͜Δཧ༝͸ɺҰ͚ͭͩͰͳ͚Ε͹ͳΒͳ͍ class ViewController: UIViewController { let tableView: UITableView var

    viewModel: ViewModel! @IBAction func fetch(sender: UIButton) { viewModel.fetchData() } } class ViewModel { unowned let viewController: ViewController func fetchData() { // Fetch Data viewController.tableView.reloadData() } } 7JFX.PEFMͷมߋཧ༝ʢ੹຿ʣ͸ɺ ࣗ਎ͷσʔλఏڙ͚ͩͰͳ͘ɺ ը໘ͷඳը·Ͱؚ·Εͯ͠·͏
  8. 4JOHMF3FTQPOTJCJMJUZ1SJODJQMF Ϋϥεʹมߋ͕ى͜Δཧ༝͸ɺҰ͚ͭͩͰͳ͚Ε͹ͳΒͳ͍ class ViewController: UIViewController { let tableView: UITableView var

    viewModel: ViewModel! override func viewDidLoad() { // ... NotificationCenter.default.addObserver(/* */) } @IBAction func fetch(sender: UIButton) { viewModel.fetchData() } @objc func onDidFetchData(sender: Notification) { tableView.reloadData() } } class ViewModel { func fetchData() { // Fetch Data NotificationCenter.default.post(/* */) } } σʔλऔಘऴྃͷ ௨஌͚ͩग़͢ σʔλऔಘޙͷඳը͸ 7$ͷ੹຿ʹด͡Δ
  9. %FQFOEFODZ*OWFSTJPO1SJODJQMF ্ҐϞδϡʔϧ͸ԼҐϞδϡʔϧʹґଘͯ͠͸ͳΒͳ͍ɺ྆ํந৅ʹґଘ͢΂͖ class ViewController: UIViewController { var viewModel: ViewModel! @IBAction

    func fetch(sender: UIButton) { viewModel.fetchData() } } class ViewModel { func fetchData() { // Fetch Data } } ্Ґͷ7JFX$POUSPMMFS͸ɺ ԼҐͷ7JFX.PEFMͷ࣮૷ʹ ґଘ͍ͯ͠Δ
  10. %FQFOEFODZ*OWFSTJPO1SJODJQMF ্ҐϞδϡʔϧ͸ԼҐϞδϡʔϧʹґଘͯ͠͸ͳΒͳ͍ɺ྆ํந৅ʹґଘ͢΂͖ // ViewController protocol DataViewModelProtocol: AnyObject { func fetchData()

    } class ViewController: UIViewController { var dataViewModel: DataViewModelProtocol! @IBAction func fetch(sender: UIButton) { dataViewModel.fetchData() } } // ViewModel class ViewModel { } extension ViewModel: DataViewModelProtocol { func fetchData() { // Fetch Data } } 7$Ͱ͸ඞཁͱ͢Δ 7.ͷػೳ͚ͩநग़ͨ͠ ந৅Խʹґଘ 7.Ͱ͸ந৅Խͨ͠ ϓϩτίϧΛద߹͢Δ͚ͩ
  11. $PNNBOE2VFSZ4FQBSBUJPO ϝιου͸ɺΞΫγϣϯΛ࣮ߦ͢Δ͔ɺσʔλΛฦ͔͢ͷ͍ͣΕ͔ยํͰ͋Δ΂͖ class DataStore { var data: [Int] = []

    func getLatestData() -> [Int] { // αʔό͔Β࠷৽σʔλΛऔಘ data = latestData return data } } HFU-BUFTU%BUB͸໭Γ஋Λฦ͢ ΫΤϦͰ͋Γͳ͕Β ࠷৽σʔλΛऔಘͯ͠ ஋Ληοτ͢ΔίϚϯυͰ΋͋Δ
  12. $PNNBOE2VFSZ4FQBSBUJPO ϝιου͸ɺΞΫγϣϯΛ࣮ߦ͢Δ͔ɺσʔλΛฦ͔͢ͷ͍ͣΕ͔ยํͰ͋Δ΂͖ class DataStore { var data: [Int] = []

    func fetchLatestData() { // αʔό͔Β࠷৽σʔλΛऔಘ data = latestData } func getLatestData() -> [Int] { return data } } ࠷৽ͷσʔλΛऔಘͯ͠ ஋Ληοτ͢Δ͚ͩͷίϚϯυ ݱࡏͷσʔλͷ஋Λฦ͚ͩ͢ͷΫΤϦ
  13. %POU3FQFBU:PVSTFMG ஌ࣝ͸γεςϜʹ͓͍ͯ୯ҰͰɺᐆດ͕͞ͳ͘ɺ৴པͰ͖ΔදݱͰͳͯ͘͸ͳΒͳ͍ class ViewController: UIViewController { override func viewDidLoad() {

    // ... titleLabel.font = UIFont.systemFont(ofSize: UIFont.labelFontSize) titleLabel.textColor = UIColor.label authorLabel.font = UIFont.systemFont(ofSize: UIFont.labelFontSize) authorLabel.textColor = UIColor.label } @IBAction func resetLabelTextVisual() { titleLabel.font = UIFont.systemFont(ofSize: UIFont.labelFontSize) titleLabel.textColor = UIColor.label authorLabel.font = UIFont.systemFont(ofSize: UIFont.labelFontSize) authorLabel.textColor = UIColor.label } } ಉ͡ϥϕϧͷςΩετඳըઃఆ͕ Օॴʹ෼͔Ε͍ͯΔ
  14. %POU3FQFBU:PVSTFMG ஌ࣝ͸γεςϜʹ͓͍ͯ୯ҰͰɺᐆດ͕͞ͳ͘ɺ৴པͰ͖ΔදݱͰͳͯ͘͸ͳΒͳ͍ class ViewController: UIViewController { override func viewDidLoad() {

    // ... resetLabelTextVisual(sender: self) } @IBAction func resetLabelTextVisual(sender: Any) { titleLabel.font = UIFont.systemFont(ofSize: UIFont.labelFontSize) titleLabel.textColor = UIColor.label authorLabel.font = UIFont.systemFont(ofSize: UIFont.labelFontSize) authorLabel.textColor = UIColor.label } } ϥϕϧͷςΩετඳըઃఆΛ ௚઀ݺͼग़ͯ͠ॏෳΛආ͚Δ
  15. %POU3FQFBU:PVSTFMG ஌ࣝ͸γεςϜʹ͓͍ͯ୯ҰͰɺᐆດ͕͞ͳ͘ɺ৴པͰ͖ΔදݱͰͳͯ͘͸ͳΒͳ͍ class ViewController: UIViewController { override func viewDidLoad() {

    // ... resetLabelTextVisual(sender: self) } func setTitleLabelTextVisual(on label: UILabel) { label.font = UIFont.systemFont(ofSize: UIFont.labelFontSize) label.textColor = UIColor.label } @IBAction func resetLabelTextVisual(sender: Any) { setTitleLabelTextVisual(on: titleLabel) setTitleLabelTextVisual(on: authorLabel) } } ιʔείʔυͷ಺༰͸ಉ͕ͩ͡ɺ ஌ࣝ͸ҧ͏
  16. %POU3FQFBU:PVSTFMG ஌ࣝ͸γεςϜʹ͓͍ͯ୯ҰͰɺᐆດ͕͞ͳ͘ɺ৴པͰ͖ΔදݱͰͳͯ͘͸ͳΒͳ͍ class ViewController: UIViewController { enum LabelType { case

    title case author var font: UIFont { /* */ } var textColor: UIColor { /* */ } } func setLabelTextVisual(on label: UILabel, as type: LabelType) { label.font = type.font label.textColor = type.textColor } @IBAction func resetLabelTextVisual(sender: Any) { setLabelTextVisual(on: titleLabel, as: .title) setLabelTextVisual(on: authorLabel, as: .author) } } ஌ࣝʹԠͨ͡੾Γସ͑Λఆٛ ॏෳίʔυΛ࡟ݮ