Monday, January 18, 2016

Refactoring in Swift - move field

Imagine classes as families. They should hold inside only what makes sense.

One does not invite neighbours to live with them, does he? Invite them to visit, maybe, but move in, no way!

So are classes. They should hold only the fields that make sense.

Friday, January 15, 2016

Refactoring in Swift - Replace method with method object

Let's continue with the same game as in Explaining Variable refactoring method.

We still have the two ships, each of them defined as a rectangle (CGRect) but today they start shooting at each other.

So other than the ships we have two projectiles, also defined as rectangles.

We also have a number of functions to manage the game.

Thursday, January 14, 2016

Refactoring in Swift - explaining variable

Swift theory

In case you are not a Swift programmer, CGRect is a structure that defines a rectangle by setting its origin and size.

For example, a CGRect of value (10, 20, 100, 150) represents a rectangle with origin at x = 10 and y = 20 and with a size of width = 100 and height = 150.


Refactoring 

Now, let's build a game. Or at least a very small part of the game.

Wednesday, January 13, 2016

Refactoring in Swift - Inline function

Refactoring by inline function is the opposite of the Extract function we analyzed in a previous post.

In this case, a function is so short and it is used only in one or two spots that is it not worth having it separately.

This refactoring is done by removing the function and replacing each call with its content.

Thursday, January 7, 2016

Refactoring in Swift - Extract function


The simplest method of refactoring and maybe the most used is performed by moving the code around.

We can move it from one function to another, from one class to another or create a new entity, in our case, a new function.

Consider the following function to validate three fields: first name, last name and address. Each variable needs to have a value.

var firstName, lastName, address : String?

firstName = "John"
lastName = "Smith"
address = "20 New Way St."

func validateForm() -> Bool {
    var valid = true
    
    if (firstName == nil) || (firstName!.characters.count == 0) {
        valid = false
    }
    if (lastName == nil) || (lastName!.characters.count == 0) {
        valid = false
    }
    if (address == nil) || (lastName!.characters.count == 0) {
        valid = false
    }
    return valid

}

Tuesday, January 5, 2016

A Little Architecture by Uncle Bob

I want to become a Software Architect.

That's a fine goal for a young software developer.

I want to lead a team and make all the important decisions about databases and frameworks and web-servers and all that stuff.

Oh. Well, then you don't want to become a Software Architect after all.
read more

Monday, January 4, 2016

Refactoring in Swift

Definition

Refactoring is the activity of cleaning the code, improving the design, after the code is written.

Refactoring is not changing the functionality of the code. If it does, it is called maintenance. [Thank you Max]

You can say that it is corresponding to the design phase, but after some piece of code was written.

While a developer can plan for the code to be nicely written, in time, the code gets messier so cleaning is required.

Just like the house cleaning, the more often the code is cleaned, the easier and more effective it is.

Refactoring does not bring any benefit right away. The functionality is not changed during this activity so there is no change for the user. The real benefit is when new features are added.

We’ll start today a series of refactoring tips in Swift, most of them adapted/stolen from Martin Fowler’s “Refactoring - Improving the design of existing code” book.