Many times, when coding, we use different data structures: stacks, queues, trees.
These structures have the elements linked between them, allowing parsing and searching.
Usually when we start coding such a structure we consider a unidirectional link between the elements, mostly to save memory.
Let's consider the following class:
Saturday, January 30, 2016
Friday, January 29, 2016
Refactoring - Introduce new method
Consider you have a function that perform an activity on an object a certain class in multiple places across the class and the project.
Sometimes, it makes sense to add that functionality directly to the class.
Swift makes this very simple using extensions. They allow to add new functionality to an existing class, structure or enumeration even if you do not have access to its code.
Let's take the following example:
Sometimes, it makes sense to add that functionality directly to the class.
Swift makes this very simple using extensions. They allow to add new functionality to an existing class, structure or enumeration even if you do not have access to its code.
Let's take the following example:
Thursday, January 28, 2016
Refactoring - Inline temp
This refactoring is the reverse of the Explaining variable.
You added a new variable to make the code clearer. But after few changes the variable is not really useful.
Consider the following piece of code:
You added a new variable to make the code clearer. But after few changes the variable is not really useful.
Consider the following piece of code:
Wednesday, January 27, 2016
Refactoring in Swift - Inline class
This method of refactoring is just the opposite of Extract class.
Think of a class that became so small and so unused that it does not justify its existence.
Consider an application that has the following classes:
Think of a class that became so small and so unused that it does not justify its existence.
Consider an application that has the following classes:
Tuesday, January 26, 2016
Refactoring in Swift - Replace Array with an Object
This refactoring applies to the case when different kinds of information are stored in an array.
For example, we have an array storing the first name, the last name and the age as follows:
For example, we have an array storing the first name, the last name and the age as follows:
Monday, January 25, 2016
Refactoring in Swift - substitute algorithm
In previous post (Simplify nested conditionals with returns) we arrived at the following code:
func bonusForSalary(salary:Float, numberOfKids: Int) -> Float {
if numberOfKids == 0 {
return salary * 0.2
}
if numberOfKids == 1 {
return salary * 0.3
}
if numberOfKids == 2 {
return salary * 0.4
}
if numberOfKids == 3 {
return salary * 0.5
}
return salary * 0.6
}
Refactoring in Swift - simplify nested conditionals with returns
Consider the case when a function needs to calculate the result using a complicated set of logic statements based on the input.
We would need to store the result in a variable and return it at the end. Or do we?
We would need to store the result in a variable and return it at the end. Or do we?
Subscribe to:
Posts (Atom)