Overclocking Your Brain

“How are you able to work on coding chops after a full day of programming at work? I am mentally exhausted by the time I get home and just can’t get motivated to tackle something new.” …, or something like that, is what I’ve heard from several people recently.

My answer: drugs.

Well, not exactly. I’m certainly not doing anything illegal. But, I have found some legal “nutritional supplements” that help me shake off the mental fatigue that I frequently encounter after coding my brains out at work.

Acetylcholine

Before I go any further, I feel obligated to remind you that there are no magic bullets. There is no substitution for genuine curiosity and tenacity. But, there may be things that give you a little nudge every now and then…

Enter nootropics. For the past few months, I’ve experimented with different combinations and dosages of nootropics with noticeable effect. I go to work, write the best code I know how to write, come home, spend some quality time with my family, then get back to writing code and mastering my craft. On those nights that I can’t shake the mental fog with a cup of green tea, I’ll put a little Noopept under my tongue and off I go. No kidding, it is almost like starting the day over sometimes. If I wake up feeling mentally sluggish, I might mix some Oxiracetam and CDP Choline in a glass of water. And there are lots and lots of different products available.

Again, taking supplements and sitting in front of the TV isn’t going to help you. Drinking protein shakes and sitting on your butt won’t make you muscular, either. But, if you’re pushing the limits of your mental endurance, it might be worth your time to do some investigation–there is plenty of research data available for different nootropics.

One more thing. I’m not a doctor, or in any way qualified to give medical advice. If you decide to experiment with nootropics, you need to do your own research. It might also be wise to consult with a doctor to make sure there aren’t known interactions with any other medications you are taking.

Share

Watch it.

I recently completed another technical review for The Pragmatic Bookshelf, which has produced (and continues to produce) great technical books. I jumped at the chance to review Jeff Kelley’s new book because it covered a topic that has been greatly anticipated by the Apple community: the Apple Watch.

jkwatch

Even if you have no intention of developing an app for the Apple Watch, I would recommend picking up a copy of Developing for Apple Watch: Your App on Their Wrists.

So, why would you want the book even though you don’t intend to create a Watch app?

1) Jeff gives great insight into the architecture and interactions between the watch and the phone to which it is tethered. Understanding different architectures and designs will certainly give you better intuition into why other things are the way they are in the Appleverse.

2) The code is written in both Obj-C and Swift. This is valuable because of the newness of Swift–there are lots of great libraries that have been written in Obj-C over the years that haven’t been ported to Swift, yet. Being able to make use of these libraries is essential.

3) How do you know you don’t want to write a Watch app if you don’t even know what the device can or can’t do? Learning about it may give you that one in a million idea.

It is an quick read, so if you want to give your brain a quick ++, Jeff’s book is a good choice.

Happy learning.

Share

States That Matter

IceI’ve been spending a little time reviewing design patterns recently, so I thought I would post an example I’ve been working on for the State design pattern. Essentially, the pattern makes it easy for you to change behavior of an object as its state changes.

I didn’t want to copy someone else’s example, so I hope you find this one interesting.

I’m going to create a Water class that has three different states: solid, liquid, and gas. The only difference in behaviors for the states will be their descriptions, which change as the temperature of the water changes. Of course, I don’t think anyone would ever utilize the state pattern for such a simple case, but I’m hoping that it is easy to follow.

The first class I’ll introduce is the Water class. It has an internal state that tracks the temperature and description of the water.

class Water : Printable {
    
    private var state: State! = nil
    
    init() {
        state = Liquid(temperature: 105, water: self)
    }
    
    var temperature: Double {
        get {
            return state.temperature
        }
        set(value) {
            state.temperature = value
        }
    }
    
    var description : String {
        get {
            return state.description
        }
    }
}

 
Next, we’ll need the State class. It provides scaffolding for the various states and also includes some logic that can be shared among states.

class State : Printable {
    
    var water: Water
    
    init(water: Water){
        self.water = water
    }
    
    private(set) var temperature: Double = 0.0 {
        didSet {
            stateChangeCheck()
        }
    }
    
    var description : String {
        get {
            fatalError("\(__FUNCTION__) requires override by subclass.")
        }
    }
    
    private func isSolid() -> Bool {
        return temperature <= 32
    }
    
    private func isLiquid() -> Bool {
        return temperature > 32 && temperature < 212
    }
    
    private func isGas() -> Bool {
        return temperature >= 212
    }
    
    private func stateChangeCheck() {
        fatalError("\(__FUNCTION__) requires override by subclass.")
    }
}

 
Finally, we implement the three states.

class Liquid : State {
    
    init(temperature: Double, water: Water) {
        super.init(water: water)
        self.temperature = temperature
    }
    
    override var description : String {
        get {
            return "Liquid"
        }
    }
    
    private override func stateChangeCheck() {
        if isSolid() {
            water.state = Solid(temperature: temperature, water: water)
        } else if isGas() {
            water.state = Gas(temperature: temperature, water: water)
        }
    }
}

class Solid : State {
    
    init(temperature: Double, water: Water) {
        super.init(water: water)
        self.temperature = temperature
    }
    
    override var description : String {
        get {
            return "Solid"
        }
    }
    
    private override func stateChangeCheck() {
        if isLiquid() {
            water.state = Liquid(temperature: temperature, water: water)
        } else if isGas() {
            water.state = Gas(temperature: temperature, water: water)
        }
    }
}

class Gas : State {
    
    init(temperature: Double, water: Water) {
        super.init(water: water)
        self.temperature = temperature
    }
    
    override var description : String {
        get {
            return "Gas"
        }
    }
    
    private override func stateChangeCheck() {
        if isSolid() {
            water.state = Solid(temperature: temperature, water: water)
        } else if isLiquid() {
            water.state = Liquid(temperature: temperature, water: water)
        }
    }
}

 
I’ve put all of these classes in the same file so that I can use the “private” keyword like I would use the “protected” keyword in other languages. Doing this makes it possible to expose temperature and description, without exposing things I don’t want to expose, like state. Remember, “private” is a little different in Swift than it is in other languages: it limits access to code in the same file (not the same class.)

Let’s see how it works:

var water = Water()
println(water.temperature) // 105.0
println(water.description) // Liquid

water.temperature -= 115
println(water.temperature) // -10.0
println(water.description) // Solid

water.temperature += 225
println(water.temperature) // 215.0
println(water.description) // Gas

 
And there you have it!

Share