<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">When writing library code, what method of parameter validation would be suggested?<div class=""><br class=""></div><div class=""><b class="">Assert</b></div><div class=""><br class=""></div><div class=""><div class=""><font face="Menlo" class="">func greetUser(user: String, times: Int) {</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>assert(user.count &gt; 0)</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>assert(times &gt; 0)</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>for _ in 0..&lt;times {</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>print(“Hello \(user)”)</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div><div class=""><font face="Menlo" class="">}</font></div></div><div class=""><br class=""></div><div class=""><u class="">Pros</u></div><div class="">* Easy to use</div><div class=""><br class=""></div><div class=""><u class="">Cons</u></div><div class="">* Disabled in release</div><div class="">* Not unit-testable</div><div class=""><br class=""></div><div class=""><b class="">Error-Handling</b></div><div class=""><br class=""></div><div class=""><u class="">Pros</u></div><div class="">* Unit-testable</div><div class=""><br class=""></div><div class=""><u class="">Cons</u></div><div class="">* Quickly litters all calling code with try's</div><div class=""><br class=""></div><div class="">In the case Error-Handling is used, what kind of ErrorType would you suggest?</div><div class=""><b class=""><br class=""></b></div><div class=""><div class=""><font face="Menlo" class="">enum GreetUserError : ErrorType {</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>case&nbsp;UserEmpty</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>case TimesInvalid</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">func greetUser(user: String, times: Int) throws {</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>guard user.count &gt; 0 else {</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>throw GreetUserError.UserEmpty</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>guard times &gt; 0 else {</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>throw GreetUserError.TimesInvalid</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>for _ in 0..&lt;times {</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>print(“Hello \(user)”)</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>}</font></div><div class=""><font face="Menlo" class="">}</font></div></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class="">Or should more generic re-usable errors be used? But less descriptive errors except if messages are provided every times:</div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><div class=""><font face="Menlo" class="">enum ParameterError : ErrorType {</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>case&nbsp;Empty(String)</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>case NotInRange(String)</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">func greetUser(user: String, times: Int) throws {</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>guard user.count &gt; 0 else {</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>throw&nbsp;ParameterError.Empty(“user")</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>}</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>guard times &gt; 0 else {</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>throw&nbsp;ParameterError.NotInRange(“times")</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>}</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>for _ in 0..&lt;times {</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>print(“Hello \(user)”)</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>}</font></div><div class=""><font face="Menlo" class="">}</font></div></div></body></html>