<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><span></span></div><div><div></div><div><br></div><div><br>Am 15.08.2016 um 19:05 schrieb Justin Jia via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>>:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=utf-8"><div class="">I agree that being explicit is nice and I also like to use `guard`.</div><div class=""><br class=""></div><div class="">But according to my observation, usually it is easier to make mistakes if we choose to use `guard`.</div><div class=""><br class=""></div><div class="">Let me give you a fake real world example.</div><div class=""><br class=""></div><div class="">With `guard`, you need to be really careful when you want to add new expression (people usually will add to the end of the function).</div><div class=""><br class=""></div><div class="">```</div><div class="">func updateCell(cell: Cell, data: CellData) {</div><div class=""> cell.label.text = data.title</div><div class=""> guard let imageName = data.imageName else { return }</div><div class=""> cell.sublabel.text = cell.humanize(imageName)</div><div class=""> guard let image = UIImage(named: imageName) else { return }</div><div class=""> cell.addBackgroundImage(image)</div><div class=""> // Let's say we changed the design and added a new heading that depends on image name</div><div class=""> cell.heading = String(imageName.characters.first) // This won't be called if image is nil!</div><div class="">}</div><div class="">```</div><div class=""><br class=""></div><div class="">With `if let`, it is really hard to read. This will become more complicated if we add more attributes to cell.</div><div class=""><br class=""></div><div class="">```</div><div class="">func updateCell(cell: Cell, data: CellData) {</div><div class=""> cell.label.text = data.title</div><div class=""> if let imageName = data.imageName {</div><div class=""> cell.sublabel.text = cell.humanize(imageName)</div><div class=""> if let image = UIImage(name: imageName) {</div><div class=""> cell.addBackgroundImage(image)</div><div class=""> }</div><div class=""> cell.heading = String(imageName.characters.first)</div><div class=""> }</div><div class="">}</div><div class="">```</div><div class=""><br class=""></div><div class="">With the proposed syntax:</div><div class=""><br class=""></div><div class="">```</div><div class="">func updateCell(cell: Cell, data: CellData) {</div><div class=""> cell.label.text = data.title</div><div class=""> let imageName = data.imageName // imageName is optional</div><div class=""> cell.sublabel.text = cell.humanize(imageName?)</div><div class=""> let image = UIImage(named: imageName?) // image is optional</div><div class=""> cell.addBackgroundImage(image?)</div><div class=""> cell.heading = String(imageName.characters.first?)</div><div class="">}</div><div class="">```</div><div class=""><br class=""></div><div class=""><div class="">This is really easy to read. And everything works correctly.</div></div></div></blockquote><div><br></div>It is even easier if you define the methods on Cell to take optional arguments. </div><div>Then you can write the code like in your last example and don't even need the proposed syntax:<div><br></div><div>class Cell {</div><div> let label = UILabel()</div><div> let sublabel = UILabel()</div><div> var heading: String?</div><div> func humanize(_ string: String?) -> String {...} // optional argument</div><div> func addBackgroundImage(_ image: UIImage?) // optional argument</div><div>}</div><div><br></div><div>extension UIImage {</div><div> init?(named imageName: String?) {...}</div><div>}</div><div><br></div><div><div><span style="background-color: rgba(255, 255, 255, 0);">extension String {</span></div><div><span style="background-color: rgba(255, 255, 255, 0);"> init?(named imageName: Character?) {...}</span></div><div><span style="background-color: rgba(255, 255, 255, 0);">}</span></div></div><div><br></div><div><div class=""><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);">func updateCell(cell: Cell, data: CellData) {</span></font></div><div class=""><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);"> cell.label.text = data.title</span></font></div><div class=""><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);"> let imageName = data.imageName</span></font></div><div class=""><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);"> cell.sublabel.text = cell.humanize(imageName)</span></font></div><div class=""><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);"> let image = UIImage(named: imageName)</span></font></div><div class=""><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);"> cell.addBackgroundImage(image)</span></font></div><div class=""><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);"> cell.heading = String(imageName?.characters?.first)</span></font></div><div class=""><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);">}</span></font></div></div><div><br></div><div>-Thorsten </div><div><br></div></div></body></html>