What would be considered best practice use values directly from UITextField or assign to a variable

What would be considered best practice, or what is most common, use values directly from UITextField throughout the program or assign the value to a variable and use the variable throughout your program?


Let's pretend that we need to do some calculations based on a number diplayed in a textField that came from a UIPickerView selection, what would you do, access the value directly from the textField or assign the value to a variable and use that variable?


Case 1 or case 2 from the code below?


CASE 1: Use values directly from UITextField to do your calculations


class ViewController{
    let percentages = ["10","20","30","40","50"]// etc.
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        let selectedPercentage = percentages[row]
        myTextField.text = selectedPercentage
    }

    func tip(total:Double)->Double{
        let tipAmount = total * Double(myTextField.text!)! / 100 // using value directly from textField
        return tipAmount
    }
}


CASE 2: Assign value to a variable and use variable for your calcualtion


class ViewController{
    let percentages = ["10","20","30","40","50"]// etc.
    var percentage:Double = 0 // variable
   
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        let selectedPercentage = percentages[row]
        myTextField.text = selectedPercentage
        percentage = selectedPercentage // assign selected value to variable
    }

    func tip(total:Double)->Double{
        let tipAmount = total * percentage / 100 // using variable
        return tipAmount 
    }
}


What's considered better approach or more common, assuming the application will grow and the value from the textField will be used in multiple parts of the program.

Accepted Reply

The second case is better. It allows for more modularity by decoupling presentation of the data from the storage of the data. See the model view controller software design pattern for more information.


For example, since you are working with percentages, you may want to consider storing your percentages as Ints and only converting them to strings when displaying them. That would allow you to take locale into account when formatting the number for display.

Replies

Separating the presentation of your data from the business logic is the more common and considered better approach.

@***** Could you please elaborate a little bit more on your satement? Are you saying that non of the cases above are ideal or that the second case is better?

The second case is better. It allows for more modularity by decoupling presentation of the data from the storage of the data. See the model view controller software design pattern for more information.


For example, since you are working with percentages, you may want to consider storing your percentages as Ints and only converting them to strings when displaying them. That would allow you to take locale into account when formatting the number for display.

Thank you for the clarification, make sense.


As far as your suggestion about storing percentages as Ints. Excuse my ignorance but how would I multiply Ints and Doubles?


The following example gives me an error.


ERROR at line 4, col 24: binary operator '*' cannot be applied to operands of type 'Int' and 'Double'


let percentage:Int = 10
let amount:Double = 100
let total = percentage * amount
print(total)


Thanks a lot for your help, this helps me a lot.

You need to convert the Int to a Double. In Swift, you use Double(anInt). Here is an example:

let total = Double(percentage) * amount

@TheCD - But couldn't we just make it a double from the begining like so...?


let percentages:Double = [10,20,30,40,50]
let total = percentage * amount


Is there a difference between what you proposed and the example above?

There's not much of a difference. Declare your variables whatever type makes sense and convert if necessary.


Example:

Say you have some buckets of apples, and you want to find the average number of apples in each bucket.


let applesInBuckets = [2, 3, 5]
var totalApples = 0 
for bucket in applesInBuckets {
   totalApples += bucket
}
let average = Double(totalApples) / Double(applesInBuckets.count)


It doesn't make sense to declare applesInBuckets as an array of Doubles because we're not counting fractional apples.

However, it makes sense that the average could be a fractional number.


I hope this helps.

I see, thank you for your help!