bitwix

Tangential comments about Software Development

Monday, November 30, 2015

On Blank Lines

It's a curious thing that writing code in an unfamiliar language and IDE leads to code that looks scrappy. There are so many things to worry about getting it to work, code layout falls off the bottom of the list.

I always feel that the blank line is a useful tool. Inside a function it shows that one thing is done, now we're on to the next thing.

In the refactoring below, the blank line count went from 13 to 1. And I like the one that was left.

Here's the Before version

func displayResultsAlamo() {
        
        self.clearResults()
        
        guard let baseUrl =  txtBaseURL.text else
        {
            lblResults.text = "Need to enter a base URL"
            return
        }
        
        guard let post = txtArguments.text else
        {
            lblResults.text = "Need to enter a postcode"
            return
        }
        
        
        lblResults.text =  "Fetching Results"
        
        self.activityIndicator.hidden = false;
        
        activityIndicator.startAnimating();
        
        let ph = PostCodeHelper()
        ph.makeGetRequestWithAlamo(baseUrl, urlArgs:post, ResultHandler: { (postcodeResult:PostCodeResult?, error:String?) -> Void in
            
            /* refresh UI code start here alamo gurantees this callback is run on UI thread*/
            
            self.activityIndicator.stopAnimating();
            self.activityIndicator.hidden = true;
            
            if (error != nil){
                self.lblResults.text = error
                return
            }
            
            guard let postresult = postcodeResult else
            {
                self.lblResults.text = error
                return
            }
            self.lblResults.text = "Found"
            self.lblLatitude.text = postresult.Latitude.description
            self.lblLongitude.text = postresult.Longitude.description
            
        })
        
    }

and here's the after version

    func displayResultsAlamo() {
        self.clearResults()
        guard let baseUrl =  txtBaseURL.text else {
            lblResults.text = "Need to enter a base URL"
            return
        }
        guard let post = txtArguments.text else {
            lblResults.text = "Need to enter a postcode"
            return
        }
        lblResults.text =  "Fetching Results"
        atStartOfProcess()
        
        let ph = PostCodeHelper()
        ph.makeGetRequestWithAlamo(baseUrl, urlArgs:post, ResultHandler: { (result:LatLongPair?, error:String?) -> Void in
            /* refresh UI code start here alamo gurantees this callback is run on UI thread*/
            self.atEndOfProcess()
            self.displayPostcodeOrError( result, error : error)
        })
    }