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

Af64bc38c0ffcfcabdf430759ee491ce?s=47 Elvis Shi
October 30, 2019

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

Af64bc38c0ffcfcabdf430759ee491ce?s=128

Elvis Shi

October 30, 2019
Tweet

Transcript

  1. ίʔυͷฏ४ԽΛ໨ࢦͯ͠ for ୈճ)","5"TXJGU

  2. } 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 {
  3. J045FDI-FBE5FBNͷ࢓ࣄ ⠸

  4. J045FDI-FBE5FBNͷ࢓ࣄ w ϓϩδΣΫτԣஅͷ13ϨϏϡʔ w νʔϜԣஅͷٕज़ࢧԉ΍$*$%ͷಋೖࢧԉ w ։ൃඪ४Խʗฏ४Խͷ໛ࡧ

  5. ։ൃඪ४Խʗฏ४Խ ˣ ଐਓԽΛ๷͙

  6. IUUQTOMBCJUNFEJBDPKQOMBSUJDMFTOFXTIUNM

  7. ༗څऔΓ์୊੍౓ w ͍ͭͰ΋ɺ͍ͭ·Ͱ΋ɺ༗څ͕औΕΔ w ࣗ෼ͷইප w Ո଒ͷհޢҭࣇ w ϦϑϨογϡ w

    FUD w ͍ͭͰ΋ɺ୭Ͱ΋ɺٸʹٳΉՄೳੑ͕͋Δ w ಛఆͷਓ͕͍ͳ͍ͱ࢓ࣄ͕ճΒͳ͔ͬͨΒΞ΢τ w ඞͣ୭͔୅ΘΓʹͳΔਓ͕͍ͳͯ͘͸ͳΒͳ͍
  8. ։ൃඪ४Խʗฏ४Խ

  9. w ඪ४Խ w ։ൃ؀ڥ΍ϚχϡΞϧΛڞ௨Խ͢Δ w ಉ͡ϥΠϒϥϦʔΛར༻͢Δ w ಉ͡ϑΝΠϧΛྲྀ༻͢Δ w ಉ͡ઃܭΛద༻͢Δ

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

  11. ୭Ͱ΋͙͢ʹಡΊΔίʔυΛॻ͘ • Ϧʔμϒϧίʔυʂ • ໊લͱ࣮૷ΛҰகͤ͞Δ • ᐆດͳදݱΛආ͚Δ • ڊେͳ࣮૷Λ෼ׂ͢Δ •

    etc...
  12. let searchEngineURL = "https://www.google.com" 63-ͱݴͬͯ·͕͢ த਎͸4USJOH ໊લͱ࣮૷ΛҰகͤ͞Δ

  13. let searchEngineURLString = "https://www.google.com" ໊લΛ63-4USJOHʹ௚͢ ໊લͱ࣮૷ΛҰகͤ͞Δ

  14. extension String { func isZeroPrefix() -> Bool { return !hasPrefix("0")

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

    } } ໊લΛͰ࢝·͍ͬͯͳ͍ʹ௚͢ ໊લͱ࣮૷ΛҰகͤ͞Δ
  16. func isForceUpdateVersion() -> Bool ࣗ෼ͷόʔδϣϯ͕ڧ੍Ξοϓσʔτର৅ͳͷʁ ࠓ഑৴தͷόʔδϣϯ͕ڧ੍Ξοϓσʔτόʔδϣϯͳͷʁ ᐆດͳදݱΛආ͚Δ

  17. func isForceUpdateRequired() -> Bool ͱʹ͔͘ڧ੍όʔδϣϯΞοϓ͕ඞཁͩͱࣔ͢ ᐆດͳදݱΛආ͚Δ

  18. IUUQTXXXBNB[PODPKQEQ

  19. IUUQTTQFBLFSEFDLDPNMPWFFTFMGEPDVNFOUJOHGBMTFTVTVNF

  20. 5*14 w %BOHFSʷ4XJGU-JOUͰϑΥʔϚοτͷ੔͑ͨ ಡΈ΍͍͢ίʔυΛ w ໋໊͸ίʔυ͕ԿΛ΍ͬͯΔ͔Ͱ͸ͳ͘ɺ ͋ͳ͕ͨԿΛ͍͔ͨ͠ w ໊͍͍લ͕ࢥ͍͔ͭͳ͔ͬͨͱ͖ w

    (PPHMF຋༁ w 8FCMJP࿨ӳࣙయ w (JU)VC୯ޠݕࡧ w DPEJDKQ
  21. ୭Ͱ΋͙͢ʹอकͰ͖ΔίʔυΛॻ͘ • ςετʂʂʂ • ৽نͷίʔυ͸ͱΓ͋͑ͣςετΛॻ͘ • ςετ͕ॻ͚ΔΑ͏ʹ෭࡞༻౳Λࣗવʹ஫ҙͰ͖Δ • طଘͷίʔυʹςετΛ௥Ճ͍ͯ͘͠ •

    ςετॻ͚Δ΋ͷ͔ΒͰ͍͍ͷͰͱΓ͋͑ͣॻ͘ • ಺༰͕׬ᘳʹͰ͖ͳͯ͘΋ͱΓ͋͑ͣॻ͚Δ΋ͷΛॻ͘ • ςετίʔυॻ͍ͯΔ͚ͩͰؾ෇͚Δόά΋͋Δ
  22. None
  23. None
  24. None
  25. IUUQTXXXBNB[PODPKQEQ

  26. IUUQTQFBLTDDJ04@UFTUJOH

  27. ୭Ͱ΋͙͢ʹ௥Ճ࣮૷Ͱ͖ΔίʔυΛॻ͘ • ֤छݪଇΛकΔʂʂʂ • SOLID • CQS • DRY •

    KISS • PIE • etc...
  28. 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ͷมߋཧ༝ʢ੹຿ʣ͸ɺ ࣗ਎ͷσʔλఏڙ͚ͩͰͳ͘ɺ ը໘ͷඳը·Ͱؚ·Εͯ͠·͏
  29. 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$ͷ੹຿ʹด͡Δ
  30. %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ͷ࣮૷ʹ ґଘ͍ͯ͠Δ
  31. %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.Ͱ͸ந৅Խͨ͠ ϓϩτίϧΛద߹͢Δ͚ͩ
  32. $PNNBOE2VFSZ4FQBSBUJPO ϝιου͸ɺΞΫγϣϯΛ࣮ߦ͢Δ͔ɺσʔλΛฦ͔͢ͷ͍ͣΕ͔ยํͰ͋Δ΂͖ class DataStore { var data: [Int] = []

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

    func fetchLatestData() { // αʔό͔Β࠷৽σʔλΛऔಘ data = latestData } func getLatestData() -> [Int] { return data } } ࠷৽ͷσʔλΛऔಘͯ͠ ஋Ληοτ͢Δ͚ͩͷίϚϯυ ݱࡏͷσʔλͷ஋Λฦ͚ͩ͢ͷΫΤϦ
  34. IUUQTTXJGUPSHEPDVNFOUBUJPOBQJEFTJHOHVJEFMJOFTVTFUFSNJOPMPHZXFMM

  35. %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 } } ಉ͡ϥϕϧͷςΩετඳըઃఆ͕ Օॴʹ෼͔Ε͍ͯΔ
  36. %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 } } ϥϕϧͷςΩετඳըઃఆΛ ௚઀ݺͼग़ͯ͠ॏෳΛආ͚Δ
  37. %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) } } ιʔείʔυͷ಺༰͸ಉ͕ͩ͡ɺ ஌ࣝ͸ҧ͏
  38. %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) } } ஌ࣝʹԠͨ͡੾Γସ͑Λఆٛ ॏෳίʔυΛ࡟ݮ
  39. IUUQTRJJUBDPNMPWFFJUFNTEDFDFCFC

  40. ʲએ఻ʳ

  41. גࣜձࣾΏΊΈɺΤϯδχΞઈࢍืूத IUUQSFDSVJUZVNFNJDPKQ 䱰⾂ ؽ٨ضٌ ✌؜猳

  42. IUUQTIBLBUBTXJGUDPOOQBTTDPNFWFOU