[swift-users] Swift BMI

Erica Sadun erica at ericasadun.com
Tue Dec 15 08:20:41 CST 2015


In fact, there's no really good reason to use any construct, whether struct, class, or enum, for this at all since the BMI isn't a "thing" per se.  A global function might be a better fit.

http://swiftstub.com/443052056

-- E

> On Dec 15, 2015, at 7:10 AM, Eimantas Vaiciunas via swift-users <swift-users at swift.org> wrote:
> 
> I exchanged a class to an enum since the BMI values are a finite set. This eliminates a need for valid value check since the switch that is used to initialise the enum value is exhaustive.
> 
> http://swiftstub.com/242291010/ <http://swiftstub.com/242291010/>
> 
> On Tue, Dec 15, 2015 at 3:30 PM, Erica Sadun via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
> 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:
> http://swiftstub.com/358512527 <http://swiftstub.com/358512527>
> 
> * 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 <mailto: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 <mailto:swift-users at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>
> 
> 
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org <mailto:swift-users at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>
> 
> 
>  _______________________________________________
> swift-users mailing list
> swift-users at swift.org <mailto:swift-users at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-users <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/520d780b/attachment.html>


More information about the swift-users mailing list