[swift-users] Swift BMI

Erica Sadun erica at ericasadun.com
Tue Dec 15 07:30:20 CST 2015

Leaving aside typos, any discussion about the merits of BMI, and the intemperate language in the code, you do a few things that could be improved upon language-wise. Of these, the most egregious is the final "break the machine" else statement that can never be reached.

The following example does several things:

* Adds a precondition to ensure that clients have passed valid arguments
* Uses an enumeration (with a default value) instead of overloading a class function with nearly identical code
* Introduces a multiplier to address the kg/lb difference
* Establishes an upper case name for the class
* Uses a switch statement instead of cascaded ifs 
* Returns a String instead of running procedurally

-- E

> On Dec 14, 2015, at 8:20 PM, Gage Morgan via swift-users <swift-users at swift.org> wrote:
> For most languages I try out, my own version of "Hello World!" is a BMI calculator. Swift has passed, I encourage you to try it out. 
> There are two methods called towards the end of file:
> 1) bmi.pounds(weight, height) - Replace numbers inside with your own if you want to test out in US Customary units. 
> 2) bmi.kilograms(weight, height) - Replace numbers inside with your own if you want to test out in Metric units used everywhere outside the US. 
> You already get the gist, the bits can be found here:
> https://gist.github.com/anonymous/9284017644567c29c7f8 <https://gist.github.com/anonymous/9284017644567c29c7f8>
> If there's a bug please let me know, but it works in IBM's Sandbox. 
> (Yes, ALL code was written by me without help. Very close to C, just a bit laid back.)
> --MGage--
>  _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20151215/be2a5a41/attachment.html>

More information about the swift-users mailing list