Feeds:
Posts
Comments

Archive for August, 2016

One of the challenges in learning a new language, be it human or computer, is learning to think in the language. Learning Swift is no different.

I’ve been spending the past several months working on the first iOS (iPhone) app that I’ll be releasing through the Apple app store. I’m writing it entirely in Swift 3, the latest version of the language. Yesterday, I was looking over the code to see if there were any places where I’d been thinking in a non-Swift fashion. This moment of reflection was triggered by my viewing of a very recently released class in Swift from one of the major online education sites. I was disappointed that the instructor was using an idiom that I knew was easily simplified in Swift. My disappointment made me wonder if I had similar issues.

The following is actual code from my upcoming app. I had originally created it’s core in C as an extensible example for teaching that language. The setup is as follows:

  • the map is an array of connections from each room
  • the number of connections from each room is fixed
  • we are explicitly tracking the number of a given type of hazard

We want a function that returns true if there is hazard in one of the connecting rooms. Here’s the original code:

var hazardNear = false
        
if hazardCount > 0
{
    for nextRoom in 0 ..< numberOfConnections
    {
        if hazardRooms.contains(exits[room][nextRoom])
        {
            hazardNear = true
                    
            break
        }
    }
}
        
return hazardNear

Pretty conventional C-esque stuff. The first thing that jumped out at me was the reliance on knowing the number of connections. Swift arrays are true collections. Let’s treat them as such.

var hazardNear = false
        
if hazardCount > 0
{
    for nextRoom in exits[room]
    {
        if hazardRooms.contains(nextRoom)
        {
            hazardNear = true
                    
            break
        }
    }
}
        
return hazardNear

When viewed this way, it’s obvious that we’re just filtering the collection based on a condition. So, just do that.

var hazardNear = false
        
if hazardCount > 0
{
    hazardNear = exits[room].filter{hazardRooms.contains($0)}.count != 0
}
        
return hazardNear

We’re filtering the array of rooms for ones that contain the hazard and checking for a non-zero count. But this is still a bit clunky. Let’s examine what we’re really asking.

var hazardNear = false
        
if hazardCount > 0
{
    hazardNear = !exits[room].filter{hazardRooms.contains($0)}.isEmpty
}
        
return hazardNear

It’s far more clear to simply ask if the filtered array is non-empty directly. What’s left is one final simplification.

return hazardCount > 0 && !exits[room].filter{hazardRooms.contains($0)}.isEmpty

One might ask whether having an explicit count is necessary as the hazard array has a count. True. Although the hazard array is built based on the number of hazards, so I still need it to be around.

Overall, I fairly pleased with the results of the exercise. In the end, the code is much clearer in intent.

Read Full Post »

In the dim days, dinosaurs roamed the Earth. Okay, not that far back.

In the American West of the late 19th century, if you wanted the latest technological wonder needed to make your life easier, there was one place you’d go … the blacksmith. Why? Well, the smith had both the equipment and the skills to produce just about anything out of metal. (Our expectations of technology was a bit lower by  today’s standards.)

On the face of it, the things you needed to set yourself up as a blacksmith were fairly minimal.

  • Fire
  • Metal
  • Water
  • Tongs
  • Hammers
  • Chisels
  • Anvil

Of course all the tools in the world a pointless without the skills needed to use them effectively and efficiently.

Of the items on the list, the outlier is the anvil. It is the foundational piece of equipment second only to fire in importance.

Interestingly, this fairly humble object is used to the production of stunning pieces of metalwork.

For some time after the arrival of Europeans to North America, anvils were shipped from the Old World. Eventually, the infrastructure was sufficient to allow the production of anvils locally. Bringing the cost down significantly. Still, they were expensive. Likewise, if you wanted a blacksmith, you brought one over.

To become a blacksmith, you apprenticed with a blacksmith. At the end of your apprenticeship, you would be given either money or tools. With your tools and skills and after purchasing your own anvil, you could now go and ply your trade making everything from spoons to fences to lanterns.

For me this history has a special significance. When I started on my journey of working with software development, computers were million dollar investments. Access to them was restricted to a few highly trained individuals. I had the opportunity to study under one of those who was present at the beginning of the computer era. I watched the development of the computer from big iron to something you wear on your wrist.

Throughout there has always been the computer and keyboard with which I craft software. Long gone are the days of stacks of dollar bill sized, hole-ridden cards. Now my anvil could be lost in a stack of magazines. Strangely, that US$20 anvil of 1850 would cost about US$700 in 2016, about what you’d pay for a run-of-the-mill laptop or the iPad I’m composing this post on.

Like that anvil, I can take my tools with me anywhere. Like the blacksmith, the quality of my  work and the words of my employers and colleagues speak for me. Likewise, my tools and skills need constant sharpening and care.

I have seen many fads come and go. Throughout the basic skills are always applicable. When teaching, I try to emphasize that there are no shortcuts to mastery. There seems to be a great willingness to build software today by assembling large chunks of other people’s work. While this may lead to a functional product, it does not make you a better developer. To become a better developer you must understand how the pieces work. I’m not saying that you must build everything yourself, merely that given time that you could. This is the distinction between a technician and a technologist. The former can be trained in short order by the latter.

It seems that in an age when we want everything on a global scale, we believe that the quality of the craftsman a price that must be paid. In reality, scale need not sacrifice quality. The missing piece of the equation is time. The old adage of fast, cheap, good; pick two; is quite real. Quality takes time. If you’re seeking quality, keep you’re eye out for those who have their own anvil.

Read Full Post »