Newton’s Third Law

Summer 2008

“Janie, you know everyone hates you here, right?”

I looked up. The boy who said this to me was sitting in the middle of the recording studio. The room went silent. All my classmates immediately stopped talking and their heads swiveled around to look at me.

I gazed at the boy. I barely knew him. I only knew his name because he missed the first week of class because his lung collapsed and he was in the emergency room. I don’t think I’d exchanged one word with him in my entire life.

Everyone’s eyes darted back and forth between me and the boy. A few people cleared out of the middle of the room. There was going to be a fight.

Janie at 13

As long as I can remember, I identified myself as a feminist. If my teacher picked a group of people with more boys than girls, I would accuse them of being sexist. I wanted to be the first woman president. I was angry because I knew I was out of step with my peers and I knew they didn’t respect me. I wanted them to respect me.

Like the good 80’s child that I am, I decided I wanted to learn karate. I wanted to learn how to be physically powerful because I wanted to go through life without worrying about losing in a fight to anyone.

My small town in rural Wisconsin did not have a karate dojo. We had an Aikido dojo. Aikido, for those who don’t know, is purely self-defensive. We didn’t learn fancy punches and kicks. We didn’t learn to flip people over. Instead, we learned how to deflect physical attacks. We learned how to direct an opponent’s energy away from us.

I thought this was bullshit.

This was stupid to wait around for someone to punch you and then to just move their arm away. What was the point of that? If someone had the audacity to come after you, they deserved to be punished. They should have their ass kicked.

I stuck with Aikido for about a year until I started attending a Catholic school out of town and I didn’t have time to keep going. Honestly, I didn’t see the point. It takes forever to advance in the belt system and you don’t learn how to beat the crap out of people, so why bother?

Sensei

My sensei, Mark Uttech, passed away recently. He fought cancer for a year and lost his battle. He was a kind, good-hearted soul. He always had a mischievous energy about him. I can’t imagine what he was like as a young man because he had an old soul. He was peaceful and accepting of everyone. He encouraged not only non-violence, but non-combativeness.

I intended to go back to the dojo back in 2013. I attended one or two classes, but this was around the time my marriage was falling apart and I had dedicated myself to programming. I deeply regret not being able to learn from him during this time.

Janie at 26

At some point when I was 26 I just got sick of being angry. I had been fighting people my whole life and none of it made any difference. I was tired of battling everyone I knew. I was tired of being pissed off and carrying around my anger. Anger is heavy. Anger is expensive. It takes energy to hate. Everything I did was ineffective. The battles I fought with people left me scarred and did nothing to affect any kind of positive change.

I began to discover Zen Buddhism. I found that I had made a logical fallacy in regards to my interactions with others.

I thought that any time I saw what I perceived to be injustice, it was my duty to go and fight over it. Not every slight needs to be fought over. Not everything is a slippery slope on our way to the Nazis taking away the Socialists. If you make an issue over everything, then really egregious behavior loses its context.

Actions and Reactions

I thought if someone did something I thought was wrong or they challenged me, that it required me to fight them. It’s Newton’s Third Law: Every action has an equal and opposite reaction. If you punch a wall with your fist, one of you must break. If you use enough force and commitment, the wall breaks. If you falter, your fist breaks. There is no other way.

Except there is.

Just because someone insults you doesn’t mean you have to fight them. I am saying this a person who has been privileged enough not to be doxxed online or forced to flee my home, so take my thoughts with a grain of salt.

Aikido is about redirecting violent energy in a way that renders it harmless. Rather than having a zero-sum game, where either the fist or the wall must break, you can take that energy and direct it in a way that hurts no one.

I know, you think if someone throws a punch at you, it’s your obligation to make sure they never hit you again. If you redirect their fist away from you and neither of you is harmed, they will just keep coming after you. Let them. Keep redirecting their energy and eventually they will stop. They tired themselves out and they don’t get the satisfaction of breaking your face. You don’t stoke their anger by provoking them back by causing them pain. They will grow tired of trying to hurt you and they will leave you alone.

I didn’t have the patience to understand this when I was younger. I was angry and I wanted to set the world on fire. I wanted to burn people. I thought that backing down from a fight made me weak or cowardly. I didn’t realize that it was braver to look an assailant in the face and to decide that you would not let them hurt you or themselves. There is more than one way to win. Winning means you walk away without harm, not that you beat your opponent.

Be the Change You Want To See In The World

I have been asked why I go to conferences. They are a lot of work and they are quite tiring. Why do I go?

I am trying to bring an energy to our community that I would like to see more of. I want everyone to love and accept one another. I want everyone to have an understanding and an empathy about where someone else is coming from.

I spent half of my life learning how to make people feel like shit. Then I spent the other half learning how to make people feel good. Making someone feel good about themselves feels better than destroying someone. It takes more care and effort to build someone up than it does to tear them down.

I want to show everyone that you can be successful without being an asshole. I have contributed a lot of negative energy to the world and I would like to show people that there is another way. There is forgiveness. There is self acceptance. There is understanding. There is peace.

Epilogue

Everyone watched us with baited breath. What would I do? Would I deny it? Would I cry? Would I appeal to them to prove that he was wrong? What would I do?

I smiled at him and said, “I know.”

He looked like I punched him in the face. He looked stunned. He shook his head and cocked it at me, thinking he must have misunderstood me. “You know?”

“Yes, I do.”

“And you don’t care?”

“No, not really.”

He stared at me. Of all the reactions he was expecting, this wasn’t one of them. He didn’t know what to do.

“Well, I just thought you should know that we all hate you.”

Everyone was disappointed. They were looking forward to a fight. They went back to what they were doing. I went back to reading my book. It’s like nothing happened.

As everyone went back about their business, I smiled to myself. I had won.

Why I am Not Moving

“Janie, why are you still in Madison?”

This is a question I was asked recently. I actually get asked this question a lot.

For the record, I don’t live in Madison. I live in Deerfield. Deerfield is a village with less than two thousand people, no grocery store, and no traffic lights.

I had a lot of frustrating conversations with people over the winter when I was freaking out about taking care of myself where I was told to order delivery or sublet my house to move closer to work. There is no delivery in a village of two thousand people, one restaurant, and six bars (this is Wisconsin, there is a five bar minimum per incorporated town).

There is no public transportation. I drive sixty miles round trip every day to go to my job. This can easily eat up two hours of my day. The closest actual grocery store is twenty minutes away, so it is rather difficult to run errands because just going to the store kills almost an hour of my increasingly limited free time.

Additionally, I have my pugs who spend way too much time alone in my house and it’s hard for me to go home, feed them, shovel food down my throat, then go back out again after working a ten hour day.

Part of me thinks it would be nice to be able to go out at 9:00 on a week night on the spur of the moment without having to drive a hundred miles round trip in one day. It would be nice to take advantage of my gym membership without feeling guilty thinking about my pugs being stuck in my house alone all day. It would be nice to join the maker space that inconveniently exists equally far away from both my job and my house.

So why don’t I move?

First off, I can’t afford it. Conservatively, it would quadruple the cost of my housing to move downtown, or even closer to my job. After my divorce I effectively have a blank slate. I don’t have any debt (besides my house and my car), but I don’t have anything saved either.

I would have to sell my house. Did I mention the whole living in the middle of nowhere, no public transportation thing? There are a half dozen houses around me that have been on the market for a while. My house is just about the right size for one person or maybe a couple, but not for a family. It’s within walking distance of the school, but in our era of helicopter parenting, that doesn’t mean what it used to.

I feel like I am in a really weird stage of my life right now. I never did the whole partying and dating thing in my twenties. I had some health and social issues and so I didn’t go out to bars and date. I married my ex-husband because we knew one another our whole lives and the dating scene terrified me because the only attention I got was from married creeps who wanted to have affairs (which isn’t that much different right now, to be honest).

Even though I didn’t have kids, I have a lot of the same issues that parents have. I have to go home to take care of my pugs. If I want to stay out after work, I have to make arrangements for someone to come take care of them. I find it incredibly selfish that people tell me that they only want to hang out with me if they can call me on a whim and ask to meet at the bar twenty miles away in ten minutes rather late on a week night. When I tell them I need to schedule something or meet earlier, they get pissy with me and tell me to move. This makes me less inclined to hang out with them because they want me to contort myself like a pretzel and they are unwilling to do one thing to accommodate me.

Last week I did an interview with the Ray Wenderlich podcast. One of the themes of the interview was them asking about all the projects I have been doing. I spoke at, I think, at least ten conferences last year. I coauthored a book. I work a full time job. I am the cohost of NSBrief. I have a lot of projects that I do. I never really thought about it because they slowly added incrementally and the deadlines for each one would come and go then another would take it’s place like Hydra.

I started to really think about what I am doing with my life and how things would be different if I lived in a city like a normal person.

If I lived downtown and I could order takeout and go out drinking at 9:00 on a week night, I would probably do it. I would develop a circle of friends who all do those things. I would probably be more social, have different hobbies and activities, and have something that resembles a normal life.

I don’t think I would find that fulfilling.

When I started programming a few years ago, I was driven by my fears of inadequacy. I was competing with people who had been programming since they were twelve. I was in my thirties and I hadn’t established a career yet. Just yesterday I was talking to a high school student who is on Ray’s tutorial team and I felt a deep sense of shame for squandering my twenties doing stupid crap.

I don’t remember where I read this, but there was a piece of writing Anthony Bourdain did where he talked about his chef career. He said that when he was a young chef, he had two options. He could either take some time really honing his skills by working for better chefs, or he could chase the money by being an okay head chef at a couple of different places. He had a heroin habit to feed, so he chose to chase the money. He talked about Grant Achatz, the chef at Alinea. He was talented enough that he could have been a head chef right away, but he wanted to be a great chef, so he paid his dues for several years by being a sous chef for great chefs so that he could learn to be a great chef. His restaurant is the only consistent 3-star Michelin restaurant in Chicago and he is considered to be one of the greatest chefs of his generation.

I consider my current job to be more than a job. I feel like it’s my do-it-yourself graduate school. I am a much better programmer now than I was when I started here almost a year ago. I have the freedom to ask stupid, esoteric questions without worrying that I am going to be fired for not knowing what a bubble sort algorithm is. I am a sous chef working under a great chef to learn how to be a better programmer (mixed metaphors are pretty).

Right now I have two options for making up for lost time. I can either spend a lot more money to engage in a lifestyle that will be fun for a few years but will do little to enhance my career, or I can accept that living in the suburbs sucks, but that I can spend all of my time stuck at home doing things that will enhance my career and help me make up for lost time by fast tracking my programming skills.

I know most people after a divorce would throw themselves back out into the dating pool and get new hobbies and do a bunch of things to feel free and alive again. It’s a tempting thing. However, I made a lot of sacrifices over the last three years to get to the point I am at now. My fire is burning, but it will go out if I don’t keep tending it. I worked my ass off to get it going after having two previous attempts not get past the kindling stage. As cool as it would be to be a “normal” person and go enjoy my youth, I think that I have a chance to do something important right now and I want to see where I can take it.

Meaningful Relationships and the Art of Networking

Family

My father and I have had arguments about family for most of my life. My father keeps insisting that now that I am an adult I should make a pilgrimage twice a year to go to his family reunions. I don’t see the point in going to these because I had many of these events from my childhood where I felt kind of like we were the Anne of Cleeves juice glass in the collection of wives of Henry VIII. We needed to be there so that there would be a complete set of my father’s siblings and family, but once we were accounted for we were kind of ignored and forgotten.

I came to terms with this at some point in my early adulthood because, honestly, what do you say to people you only see twice a year? Most of my relatives were much older than I was. My cousins were in college and getting married when I was in Jr. High. We were at different points in our lives.

The advent of Facebook and the Internet has made it much easier to connect with my relatives and keep up with what they are doing. We’re also now all adults and it’s a little easier to connect with people when you’re at the same point in your life.

Yet still I fight with my father over the necessity to go there and see people in person. We would have incredibly heated arguments over this and I could never articulate why this bothered me so much until recently, and yes, it does has something to do with networking.

Meaningful Relationships

I guess the things that bothers me about going to various family reunions is that they are perfunctory and superficial. You go, you say hi to the host, you eat your sandwich, you go home.

I observed over many years that most people’s relationships with their families were much different than my own.

I grew up in a very small town where most people had a huge network of cousins and other relatives who were all around the same age. They were thick as thieves in school and continue to voluntarily spend time together as adults. People weren’t just cousins, they were friends as well.

My cousins were not my friends. I really think that they could have been.

I remember as a kid being hauled to the family Christmas brunch where I would usually find a nice corner to sit in and read my Babysitter’s Club books while I waited for presents to be opened so that we could go back to the hotel and go swimming. One thing that happened every year was that my cousins would all go out to a movie and I was always invited to go. My dad would tell them no, I wasn’t allowed to go with them.

After a few years they stopped asking and whatever relationship I had with them withered until I became an adult and reconnected with them on social media.

My mother’s family was closer in both age and geographically and I barely saw them either. I will now go on Facebook and see every cousin of my mother’s, except her, go on a week-long camping trip and other various adventures that we are never included in because we’re not really a member of the family.

I keep fighting with my dad because I think we have different ideas of what “family” is.

My dad thinks that family is people you are related to by blood who are obligated to take you in if you are in trouble. He sees them like Social Security. You show up once a year to let them know you are alive so that if you are at a point where you might not be you can show up with your hand out and expect to be taken care of.

To me, what I observed, is that family are your social circle and your safety net. Family is people you want to spend time with and who you want to help if they’re in trouble. My dad has no interest in a meaningful relationship with any member of his or my mother’s extended family and he assumes that the blood tie is enough to fulfill this goal.

So What Does this Have to do with Networking?

I’ve noticed that some people are taking the wrong approach to networking.

Many people think that having several hundred LinkedIn connections is a network. It isn’t.

Jeremy Sherman was talking at the mini-360iDev in Greenville last year about how he networked with a journalist he hoped would feature him in a piece for his magazine. The only reason this tactic worked was because Jeremy actually made friends with this person. They liked one another and it was a real, meaningful relationship. He made it clear it wasn’t a mercenary tactic, he just really wanted to befriend the guy and that it worked out because he treated the journalist as a human being and not an opportunity.

All of the opportunities I have had have come from having meaningful relationships with people in this community.

It means a lot to me that the Kleins, the Remsiks, and the Wilkers have given me the opportunity to speak at their conferences. I like all of them as people very much. If I didn’t get to speak at 360iDev in two weeks (buy your tickets!), I would still like the Wilkers very much and would have found a way to go (but I am glad I didn’t have to).

I made friends with Chris Adamson because I liked the work that he did. We hung out at Anime Central the last two years because we were friends and we both had a mutual enjoyment of anime.

If I had gone into my friendship with him with the agenda that I was going to get something out of him, our friendship would never have materialized. People can sense users a mile away. Don’t be a user.

I am at a point right now where if I lost my job this afternoon, I have a large number of people I would go to for help, not just to find another job, but just because they’re friends who I think can give me advice and comfort about what to do. I have a lot of people in this community that I consider to be close friends and confidants and it makes my life and career richer for having a meaningful relationship with them.

Networking is about building meaningful relationships with people. It’s about seeing them as people and spending time with them even if it doesn’t immediately get you anything. It’s about finding people you genuinely like and who genuinely like you. If you go to one conference and meet someone once and never follow up with them, then I am sorry, they are not a connection in your network.

If you’re going to go to the expense and time of attending a conference, make the most of it. Meet as many people as you can. Find people who like the same things you do and keep in touch with them. You can’t pick your family, but you can pick the people you choose to be connected to.

Property Lists And User Defaults in Swift

I have written a bit about some of the work I am doing right now. Sadly, for most of this calendar year I have worked with Swift very little. I spent a lot of time working with some old open-source C libraries. Trying to yank my brain from the C precipice and trying to force it back into thinking in Swift.

My boss Brad Larson has been diving into the deep end of the Swift pool and bringing a bunch of treasures up from the bottom that I never would have considered. I am taking one of these treasures and examining how it works and what issues in our code it solves. I am hoping that by taking an elegant solution that many of us might not have thought of that we can all start thinking outside the OOP box.

The first part of this blog post is talking about some Cocoa concepts you probably are familiar with if you are not a new developer. Since I am a new developer, this stuff was new to me and interesting, so if you’ve been programming a while, you can probably skim this first section.

NSCoding, Property Lists, and Objective-C

The main piece of software we utilize at my company is SonoGuide. This is a piece of software that controls our robotics systems. Our systems print very minute amounts of liquid on a very small scale. Our positioner systems must be very precise. Our systems are rated to be accurate down to five microns. For those who haven’t worked with measurements for a while, that is 0.005 millimeters. You know, those super tiny increments on the ruler that are smaller than the tip of your pencil. So we are dealing with things that are incredibly small.

One of the pieces of functionality that we want our users to have is to be able to save coordinates. Since our systems are so precise and measure such small distances, it would be rather annoying for them to have to remember where their solution wells are in relation to the substrate they are printing on. We would like these positions to persist even if the user turns off the computer for the day and comes back next week.

It makes the most sense to keep these locations in the NSUserDefaults, which is a Property List. Property Lists, or plists, are NSDictionaries that contain different types of data. They can include arrays, strings, numbers, and even other NSDictionaries.

So we need an array of coordinate objects that we want to store in the NSUserDefaults that we are able to store and retrieve when necessary.

The way we, and everyone else, accomplished this back in the before time was as follows:

Serialization and NSCoder

While we’re still in the Way Back machine, do you remember that there was a period of time where if you looked at crash logs, you got weird hexadecimal values for where your app crashed rather than a nice, neat, human readable line of code?

The process of turning that value into a line of code is called “serialization.” The best mental explanation I have for serialization is the explanation that Mike Teevee uses to explain what television is in Willy Wonka and the Chocolate Factory:

You photograph something, and then the photograph is split up into millions of tiny pieces, and they go whizzing through the air down to your TV set where they’re all put together again in the right order.

Except instead of a photograph, you are using data and instead of it whizzing through the air, it’s whizzing through your hard drive in an area where it can be persisted and reproduced accurately.

We used the archiving functionality built into Cocoa to do our data serialization. Our persistence needs were not large enough to justify using Core Data and I think it’s possible that Core Data didn’t exist when the project was created. For a good background on choosing how to persist data, read Mattt Thompson’s post on the topic.

Archiving functionality has a few pieces. It has NSKeyedArchiver and NSKeyedUnarchiver for pushing and pulling the data. It also has NSCoder, which is where we, as the programmer, specify how to encode and decode our data.

NSCoding is a protocol that requires two methods to be written to conform to the protocol: encodeWithCoder and initWithCoder.

We implement the initialization protocol in our Coordinate class:

#pragma mark -
#pragma mark NSCoding protocol methods

- (void)encodeWithCoder:(NSCoder *)coder;
{
    if (name != nil) {
        [coder encodeObject:name forKey:@"name"];
        [coder encodeInteger:x forKey:@"x"];
        [coder encodeInteger:y forKey:@"y"];
        [coder encodeInteger:z forKey:@"z"];
    }
}

- (id)initWithCoder:(NSCoder *)coder;
{
    self = [self initWithX:[coder decodeIntForKey:@"x"] 
                      andY:[coder decodeIntForKey:@"y"] 
                      andZ:[coder decodeIntForKey:@"z"]];
    if (self != nil)
    {
        self.name = [coder decodeObjectForKey:@"name"];
    }

    return self;
}

With this code we are simply encoding the name of our coordinate object as a key and then adding the x, y, and z locations as values.

This takes care of our issues with serializing our coordinates. The coordinates are converted into NSData, making it nice and easy for us to move it from one place to another.

So cool, we have encoded our coordinate and we can extract it by decoding it. Huzzah.

So how do we decode our coordinate?

Our coordinate is stored as NSData. We have to reach out to our NSUserDefaults, look for anything encoded with the key “savedPositions”, decode it, and bring it back here.

NSData *dataRepresentingSavedPositions = [[NSUserDefaults standardUserDefaults]
                                           objectForKey:@"savedPositions"];
if (dataRepresentingSavedPositions != nil)
{
    NSArray *oldSavedPositions = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedPositions];
}

If you are interested in learning more about the NSCoding process, read this amazing blog post by Mike Ash.

Problems With This Approach

If you have learned anything about archiving data, this is the method you were probably taught. I did this myself while I was working through the Big Nerd Ranch guide to iOS programming. This is the standard. What is wrong with it.

Quite a bit, actually.

NSData isn’t inherently safe.

NSData is basically grey goo. It is a large clump of bits and bytes that have things fed into it and pulled out of it.

In our dataRepresentingSavedPositions we are simply pulling out any values associated with the key savedPositions. We are trusting that the data we are pulling is the kind that we want. It’s possible that our users have gone into the defaults property list and changed something. It’s possible that a hacker has gone and replaced one of our coordinates with a malicious executable file. We don’t know. There are no safety checks to ensure that none of these situations occur because when we serialized our data, we lost its type information. We’re living on a prayer here.

NSCoder can only be applied to an object inheriting from NSObject.

The way we structured our Swift code, we take advantage of the enhanced features of structs and enums. Our structs cannot comply to the NSCoder protocol because the only things that can are objects that inherit from NSObject.

In Mike’s article about implementing NSCoding, he mentions that if you want to encode a struct, you can’t encode it directly because it breaks. You need to break a struct down into its base components if you want to store and serialize them.

Since Apple has made it rather clear at this most recent WWDC that they would like us to start moving away from making everything an object, it doesn’t make a lot of sense for us to completely rewrite our code to start making our coordinates objects rather than structs.

Basically, NSCoder is build around the idea that you will only work with and encode objects, which is not how many of us program anymore.

A Swift Approach

Since we can’t really do our serialization and archiving the way we did them in the previous version of the code, how did we approach and solve this issue?

I will go over the various components of this code.

Structs

Right off the bat, we are creating our coordinate as a struct and not an object:

struct Coordinate {
    let x:Double
    let y:Double
    let z:Double

    init(_ x: Double, _ y: Double, _ z: Double) {
        self.x = x
        self.y = y
        self.z = z
    }
}

Since we know from earlier that we can’t associate NSCoder with our Coordinate struct, we need to figure out another way to deal with it. We used a protocol.

Protocols

protocol PropertyListReadable {
    func propertyListRepresentation() -> NSDictionary
    init?(propertyListRepresentation:NSDictionary?)
}

Since property lists are just NSDictionary objects, we need to make sure that anything that conforms to PropertyListReadable is able to create and take in NSDictionary objects.

Since we can now extend structs with protocols, we are able to take the PropertyListReadable protocol and extend the Coordinate struct:

extension Coordinate: PropertyListReadable {
    func propertyListRepresentation() -> NSDictionary {
        let representation:[String:AnyObject] = ["x":self.x, "y":self.y, "z":self.z]
        return representation
    }

    init?(propertyListRepresentation:NSDictionary?) {
        guard let values = propertyListRepresentation else {return nil}
        if let xCoordinate = values["x"] as? Double,
               yCoordinate = values["y"] as? Double,
               zCoordinate = values["z"] as? Double {
                   self.x = xCoordinate
                   self.y = yCoordinate
                   self.z = zCoordinate
        } else {
            return nil
        }
    }
}

The propertyListRepresentation function works under the assumption that a Coordinate instance was already able to be initialized with the correct number and types of parameters. This function goes through the Coordinate instance and sets key-value pairs for each of the axis and returns an NSDictionary. This function allows you to take any and all Coordinate instances and format them in such a way that they can be saved to the NSUserDefaults.

There are some situations where you want to go the other way. You have extracted an NSDictionary from the NSUserDefaults and you want to break it back down into a Coordinate instance. Since you can’t be certain that the data you are pulling from the defaults is in fact a completed coordinate, you need to use a failable initializer. You also need to make sure your return value is optional because if you were pulling data associated with a string you don’t want to crash your app by trying to process nonsense.

In our first line of code, we are implementing the shiny new guard statement. Since many of you may not have had time to dabble with guard, let me walk you through how we are using it and why.

Our goal with this initializer is to create a Coordinate with values we have extracted from the user defaults. Before we try to set each of our axis to a value, we want to make sure a value even exists. That is where guard comes in.

We are checking to see if our optional parameter was passed in or if we have nil. If we have nil, we don’t want to have to go through a bunch of conditional logic to verify that yes, we are doing nothing with our value. A guard statement only executes a line of code if the condition is not met. We want to return nil if we know that no parameters were passed in, so we can cut to the chase and do that immediately at the beginning of our block of code.

Once we have established that our NSDictionary was not nil, we now go through each value in the dictionary to make sure it correlates to a specific key. If at any point we encounter a key that does not have a value, we kick out of the whole thing and return nil.

By using a protocol to implement this functionality, we are able to specialize and reuse these concepts for other value types in our software. Before, we would have had to make a superclass or a delegate with methods that were required to be overridden by the subclass. By allowing structs to conform to protocols, we are able to specify functionality that we need in multiple places but with different implementation.

Map, Filter, and FlatMap

It would not be a shiny Swift blog post without the obligatory section on the functional features of Swift.

Just to clarify where we are at at this point in the code. NSUserDefaults is an NSDictionary where the key is the name you assign to label what you want to store. In our case, we want to store saved coordinates, so our key is “savedPositions”. The value is an array. The array can contain any object. Our Coordinate is not an object because it’s a struct, which is why we convert it to an NSDictionary. So, in theory, our NSUserDefaults should have a key-value pair where the key is “savedPositions” and the value is an array of NSDictionary objects that represent Coordinates. Simple, right?

Wouldn’t it be really cool to be able to filter through the array we pull from the NSUserDefaults to make sure we have an array of only the objects of the type we want? Sadly, there isn’t.

Of course there is. If there wasn’t then either that sentence wouldn’t be there, and or I would be a sadist. The jury is still out on that last conditional.

func extractValuesFromPropertyListArray(propertyListArray:[AnyObject]?) -> [T] {
    guard let encodedArray = propertyListArray as? [NSDictionary] else {return []}
    return encodedArray.map{T(propertyListRepresentation:$0)}
                       .filter{ $0 != nil }
                       .map{ $0! }
}

In the extractValuesFromPropertyListArray function, we are accepting an optional argument of an array of any object and returning an array of our generic return value. We also are specifying our generic return value must conform to the PropertyListReadable protocol.

The first thing we need to do is make sure we actually received a value. If we didn’t, there is no point in filtering an array that doesn’t exist. Our first line of code checks to see if we have a value and if that value can be cast to an array of NSDictionaries. If we don’t or we can’t, we return an empty array.

If that passes, we go ahead and filter the array. We use the .map function (which iterates through an array, applies a function to it, then creates a new array from the results) to run each value through our propertyListRepresentation initializer we created in our Coordinate extension. The propertyListRepresentation either returns an optional Coordinate or a nil.

We then apply a .filter to our results. We check to see if each item in the array is not nil. If it is nil, we throw it away. We then create a new array of just optional Coordinates.

Finally, now that we have ensured that we don’t have any possible nil values, we use another .map to unwrap each value and return and array of Coordinates (or any other structs we extend with the PropertyListProtocol).

This is really inefficient.

The function extractValuesFromPropertyListArray was refactored to the following:

func extractValuesFromPropertyListArray(propertyListArray:[AnyObject]?) -> [T] {
    guard let encodedArray = propertyListArray else {return []}
    return encodedArray.map{$0 as? NSDictionary}
                       .flatMap{T(propertyListRepresentation:$0)}
}

It was a real shame in our previous version of the code that if we had an array of mostly NSDictionaries, but if some stupid NSData object (which could be a malicious program) somehow snuck into our array, we had to throw away all of our other valid values. Since we’re going to filter through our results later anyway, we just want to verify we have an array of anything.

Since we don’t filter for NSDictionaries in our first line of code, we need to move the filter to our second line. Now our first .map statement is checking to see if each item can be cast the an optional NSDictionary. If it can, it gets added to the new array. If it can’t, it gets kicked to the curb.

Our previous code had a convoluted setup to be able to apply a function, filter out the nils, then unwrap the coordinates. This is a common enough thing that there should be a more concise way to do this. There is, and that is using .flatMap. Flat Map does exactly the same thing as our previous three chained functions in one handy dandy function call. Huzzah!

Saving to Defaults

We have one last part left to our property list loading and unloading. We’ve verified that everything we are extracting to the defaults is safe. We now have to verify that everything we are sending out to the defaults is safe.

One last little block of code:

func saveValuesToDefaults(newValues:[T], key:String) {
    let encodedValues = newValues.map{$0.propertyListRepresentation()}
    NSUserDefaults.standardUserDefaults().setObject(encodedValues, forKey:key)
}

For our parameters, we are taking an array of values that conform to T:PropertyListReadable and a string value to use for our key.

We’re creating an array of encoded values by taking the array parameter and mapping each one using the propertyListRepresentation function to turn it into an array of NSDictionaries. We then run the result through the setObject:forKey function associated with the NSUserDefaults.

Conclusion

I created a playground that contains all of the code I spoke about in the second half of this post.

When Brad showed me the extractValuesFromPropertyListArray function, it made my head explode. I saw the .flatMap() function and the guard statement, I felt stupid and inadequate. I felt despair that I would ever be able to come up with this stuff on my own. I can look at code and kind of grok what it means, but reading and understanding something are different than actually coming up with it.

After a while of struggling with the code, I realized I was asking the wrong questions about it. Rather than asking “how” it worked, I should have been asking “why” it was written that way. I talked to Brad about how we were dealing with the user defaults in the original code. He told me about the issue he had with it that I detailed in the first half of the post.

That is when a lot of this stuff began to click into place for me.

We don’t have this complicated code because we’re trying to be clever and use all the new toys in Swift. We are doing the code this way because it solves a problem that we had that could not be solved with Objective-C.

We could have used a “for-in” loop instead of the .map() function. We didn’t have to use .flatMap(), we had a longer implementation of it.

I have been approaching learning Swift all wrong. I was trying to take concepts like currying and trying to find ways of cramming them into my code so that I could understand how they worked. Our code is elegant because it had a specific set of problems it needed to solve that either were not solvable any other way, or would have resulted in more lines of code that would have taken more time to run because it wasn’t optimized.

For me, I took two lessons from this:

  1. Instead of spending more time thinking about clever ways to use shiny new features, I should spend some time thinking about what problems I had to solve.
  2. Just because my code “works” doesn’t mean it can’t be better. There might be “easy” ways to get something running, but there might be better, more elegant ways to write your code.

Yes, it’s overwhelming keeping up with all the new language changes and feature out there, but there can be great features that can make your code better that you never discover if you just decide your code is done once it compiles.

Learning How to Cook: June 2015

My ex-husband moved out of the house at the end of January of this year. We were married for just over five years. During those five years I think I only made dinner twice.

My ex-husband was a disciple of Alton Brown and he was obsessed with cooking. He bought a sous-vide. We installed a gas range in the kitchen. We regularly went to the knife shop downtown so he could get his knives sharpened and buy new kitchen gadgets.

It was also something of a living nightmare.

IMG_1888Shortly after my ex-husband turned 30, he was turned onto the Paleo diet. All of a sudden, rice, bread, and potatoes disappeared from our house. Dinner became a 12 ounce steak with three brussels sprouts. If I tried to take more than three I got yelled at for being greedy.

Food became scare around the house.

No one tells you this, but doing Paleo is an assload of work. You pretty much have to prepare a whole meal every night because a lot of the Paleo foods aren’t really designed to be eaten leftover. One night we would have lamb chops, the next I would be scavenging for food after a long day at work to be told that I could eat all the salad I wanted. Fuck that.

I started hiding food around the house.

My ex would find my food and eat it and complain to me about how having it around threw him off his diet. Once he ate a bagel that threw him off his diet, well then he was already off and why not go out and get Chinese food?

My dad has been on the opposite diet most of my life. His diet consists primarily of beans and brown rice. I remember as a teenager being told by my dad that a cheeseburger I ate on the way home was going to kill me. My dad and my ex-husband would have fights that would almost come to blows over which one of them was wrong. Both were convinced the other was going to drop dead any day because they were eating poison.

Thanksgiving was the worst.

IMG_1895My ex-husband insisted on doing all the cooking (expect for pie, which he let our mothers take responsibility for). He would spend a week brining the turkey, which had to be specially pasture-raised, making a starter for the bread, and creating his own stock. When Thanksgiving would actually happen, he would spend twelve hours stressed out of his mind trying to get a meal done that no one would eat because everyone filled up on cheese and crackers waiting for dinner to get done. I usually would steal two bottles of wine and hide in my childhood bedroom watching Doctor Who.

I decided when he moved out that I was not putting up with this shit anymore. I spent my life being told by my dad I couldn’t eat cheeseburgers and being told by my husband I couldn’t eat bread. Fuck all of this. I did not want to cook. I just wanted to live off of prepared food I found at Costco and the grocery store.

This went great for a while. I got to eat all the stuff like lasagna I hadn’t been able to have in several years. Everything was awesome.

Until it wasn’t.

I noticed after a while of doing this that I was getting tired of the food I had available to me. Even stuff that i liked tasted kind of processed.

One day a few months ago, I went to Costco to pick up my food for the next week or so. Nothing looked appealing. I was tired of everything I saw.

Then I noticed that they had boneless skinless chicken thighs. They came in six packages of four. One of the biggest things keeping me from figuring out how to cook was trying to figure out how to debone chicken. Seeing that I could get packages of about a pound of deboned chicken to include in various recipes removed an obstacle I had mentally about figuring out how to cook.

IMG_1940I picked up ingredients for food rather than food. I walked out to my car and looked in my trunk with a modicum of despair because I was pretty sure I was not going to have food for the next week or so because I wouldn’t feel like cooking anything.

Fast forward two months.

I actually have gone through most of my chicken. I had to throw one package out because I forgot to use it in time. Other than that, I actually made food.

I have done a lot better at making food for myself to have for lunch than I feared I would. I have been slower than I wish I had been. I am hoping to learn to do things faster.

One thing that always amazed me about my ex was the fact that I would look in the refrigerator and not see anything for dinner. He used to be able to look in there and say, “We have X, Y, and Z, which means I can make Foo.” I never understood how he did that.

I understand now.

I control everything that I have in my kitchen.

I have a certain number of things that I keep in stock so that I can construct meals out of them.

I have a few meals that are basically all the same ingredients but have different spices in them. I really like stewed chicken. I can make a chicken stew with a bunch of veggies and either add curry roux or some other spice mixture and have two different meals.

IMG_1924I have also figured out how much you can do with a friend egg. I never ate fried eggs until I married my ex. I always found eggs to be rather gross. I discovered that if you break the yolk over something, it makes a sauce and you have an instant meal.

I cook sweet potatoes with onions on Sunday mornings and it usually makes 2-3 servings. I reserve one serving and eat it later in the week.

I know I should probably not waste my time each week making a meal. I should spend my time working on my app or working on my conference talks. But you know what, I don’t care. For the first time in my life I can control what I eat. I think both my father and my ex were wrong about how they ate. I think food is more than just what you put in your body. I think it can feed your soul. I think sitting down to a nice meal that you enjoy that you made yourself has its own healing properties. I don’t want to spend my life thinking I shouldn’t have eaten that piece of bread because bread is bad for you. I want to think about how enjoyable it was to feel the crust crunching between my teeth and feeling the butter oozing off of it and I scarfed it down. I don’t do that every day. I don’t want to be on my deathbed and think about all the food I didn’t eat because I was worried about losing five pounds.

It’s been interesting to me to slowly expand the boundaries of what I can do. The first time I made curry it was scary because I never really worked with raw chicken before. I never cut it up or dealt with cooking it properly. To be fair, braising chicken isn’t that difficult to do. Then again, the enjoyment of a meal shouldn’t be determined by how difficult it was to prepare.

I am glad I don’t have to eat another piece of undercooked pork or unreasoned beans and rice ever again. I can control what fuel I put in my body and knowing that I have more options than I did six months ago makes me feel awesome.
IMG_1918

Fooled by Randomness

So I did the incredibly stupid thing this morning of arguing with someone on the internet. The person I argued with was Nassim Nicholas Taleb. Taleb is the author of the book “Fooled By Randomness” and “The Black Swan: The Impact of the Highly Improbable.” Taleb was commenting on the Tim Hunt controversy that happened over the last few days. If you are unfamiliar with this, you can read about it here, here, and here. Taleb posted this link on Facebook (yes, I still periodically check Facebook. I am ashamed).

Nassim Nicholas Taleb

Nassim Nicholas Taleb

For those who do not wish to dirty themselves by clicking on a Facebook link, here is the gist. Taleb argues that the firing of Sir Tim Hunt over his insensitive sexist remarks is hypocrisy because the college he was recently urged to resign from from accepts funding from Saudi Arabia. He essentially accuses the university of caving to raving social justice warriors who are unwilling to make a stand against something that is genuinely bad and accuses everyone of hypocrisy and political correctness. He called me a hypocrite in a comment that he has since deleted because I had the audacity to disagree with him.

This is complete and total bullshit.

Let me give a few reasons this is bullshit.

First off, he is presenting a logical fallacy. There is no correlation between Doctor Hunt shooting his mouth off about women in science labs and accepting funding for scientific research. The correlation is tenuous at best. I also doubt that all of the women who were on Twitter who used the hashtag #distractinglySexy were all thinking, “Let’s get this guy fired, but accepting money from a sexist country is okay.” They are two totally unrelated things.

Second, Doctor Hunt was not fired for being sexist. He was fired for making the University College London look bad. Doctor Hunt did not make these statements at home during a private dinner party with friends. He made this statements at a lunch hosted by the Korea Federation of Women’s Science and Technology Associations. This is a professional organization whose purpose is to support and promote women in science.

Let’s pretend that at this most recent WWDC a high-level Apple employee attended the WWDC Girls event and told all of the women at this event that women shouldn’t work with male programmers because they cause disruptions for the men who fall in love with them and can’t correct their code because they would cry.

How long do you think it would take before this person was terminated immediately??

Universities serve one purpose: To turn money into research. Money comes from donors, grants, and students. If students decide not to attend your university, you do not get their tuition. If female graduate students, who make up half of your cheap labor force, decide not to attend your university for their studies, you lose a massive resource that allows you to stretch that money you have to turn into research. If you have a major professor who can’t get grant funding because no one wants to be associated with him, then you have a lame duck on your pay roll.

Every single aspect of this situation hurts the purpose of the university, so they made the pragmatic decision to remove the thing making it harder for them to turn money into research.

Hey Dr. Hunt, I don't think she has the hots for you. I don't  think she's going to start crying either.

Hey Dr. Hunt, I don’t think she has the hots for you. I don’t think she’s going to start crying either.

If Doctor Hunt had grabbed the ass of one of his graduate students and she complained about it, do you think they would have fired him for that? Of course not. He’s a Nobel Laureate. He gets funding for the university. The girl would have been quietly encouraged to leave and no one would have cared. The university doesn’t give two shits about sexism. They care about getting money to turn into research.

The Saudis give them money to turn into research. I am uncertain if the committee that decides to accept the money has any moral qualms about its source, but I am sure the army of graduate students and researchers who get to have jobs because the Saudis donate money to research are happy to have it. I am sure the people whose lives that are saved because of that money don’t care where it came from. I think it’s possible for a good thing to come from a bad source. If this money is somehow tainting the research it is creating, that is another story.

Large organizations do not do the right thing because it’s the right thing. They do the right thing when it threatens their money. People like Taleb are pissed that the evil SJWs took down an innocent man making an offhand joke with their pictures of themselves in their labs wearing radiation suits, but that isn’t what happened. If Taleb really wants UCL to turn down the Saudi money, he needs to expose a damn good reason that threatens the flow of money to UCL. But we all know that isn’t his objective. He fears that one day he might be in his home and will make an off-hand joke that will be taken the wrong way and the evil hoard of SJWs will storm his castle and strip him of his things. He looks at Dr. Hunt and thinks, “There but for the grace of God go I.”

I am incredibly disappointed with this series of rants from Taleb. I read his books shortly after the financial crash in 2008 and I have drawn a lot of inspiration from him to always anticipate the unexpected. I subconsciously cringe when I hear anyone say that something will never change and continue on indefinitely. Having lost my first career to this mindset, I try to carry with me the knowledge that tomorrow everything could change. It’s sad that the man who taught me this truth is unwilling to accept it within himself that you can’t make discriminatory remarks to a room full of journalists and expect no repercussions. Welcome to the 21st century, you’re going to hate it here.

Soul Searching

January, 2014

I began my programming career in late December 2013. It started something out of necessity. Sometime around October I suffered from a nervous collapse that made it impossible for me to focus on my school work. I had dropped out. I was lying to my husband about dropping out of school because I was gambling on my ability to find a job around the time I was supposed to graduate. I was very lucky and something came through.

My only hope for my first job was that I would find something in iOS. In the Madison area, that was a pretty stiff order. Madison is a lot of enterprise Java, government, and health care.

I managed to find a job at a start-up where I was the oldest person by a decade. I was partially hired because my bosses were not old enough to drink and didn’t know any better about hiring people who have actual experience.

By January, I knew this wasn’t working. I didn’t get along with my programming partner and I knew I was going to be let go. I was okay with that. I knew it was a bad fit.

I did some soul searching at that point. I knew it was bad to lose your first job after two months and I did have some concerns with my long-term ability to be hired. I had a few choices. I could kind of fake it and find another, similar job where I would be equally uncomfortable, but possibly be able to fake it better. Or, I could acknowledge that I was in the weeds and I didn’t know what I was doing.

The people around me didn’t know what they were doing either, but I had enough training and visceral awareness to know that my programming sucked. I knew it could be better and I knew it would never be better if I continued to pretend like I knew what I was doing.

I sat down and started thinking about what I wanted from my career. There was only one thing I absolutely knew I wanted. I wanted to be Brad Larson.

Brad, for those who have not heard my enthusiastic ramblings about him, is the creator of GPUImage. Brad taught the Advanced iOS class at MATC the first year it was offered. The classes were, and are still, available to watch on iTunesU. While I was a student I heard a lot of stories about him. I heard about how Apple engineers told him that something he wanted to do with his Molecules app was physically impossible to do on the current hardware. He figured out a way around the limitation and he made it happen. He was the guy who did impossible things.

I didn’t know if it was possible or how long it would take, but I wanted that. I wanted to understand the language well enough that I could figure out a way around the limitations. I wanted to be a great programmer.

I reached out to Brad and told him that I wanted to do what he does and asked him for advice about how to get started. He was very kind and wrote a nice email back. A few weeks later, a contract opportunity became available to work with him on an extension of his Molecules app. I knew I was on my way out at my current job, so I reached out to him. I told him that I didn’t know anything but that I wanted to. I really wanted to make sure I didn’t misrepresent myself because I wanted to learn and I couldn’t do that if I pretended to know things I didn’t.

After this contract job, I spent time working on a book with Chris Adamson and doing a lot of conference talks. I was trying to get my name out there so that I could find a job. I talked about GPU programming and Metal because they were new things. I didn’t really explain them as well as they should be explained because I didn’t have enough experience with them to fully understand them. I gambled on talking about difficult and obscure technology to try and differentiate myself from all the other new programmers out there.

To my shock and surprise, Brad reached out to me about working for him as my speaking obligations were winding down. I did it. I accomplished the short term goal I made at the beginning of the year to find a programming job with a great programmer so that I could get better and be a great programmer too.

January 2015

Let’s jump ahead a year.

At the beginning of this year I initiated a divorce with my husband. Things had been difficult for a long time. My goal for a few years had been to find stable employment that would allow me to end the marriage.

I do not want to badmouth my ex-husband. Our divorce was amicable. He was a friend before we were married and he continues to be a friend. We had a lot of trouble and I honestly wish him no ill will.

I am bringing this up simply to establish that the last six months have been very difficult.

I have never taken care of myself.

I lived with my parents until I was 27 years old. I was essentially a perpetual student because I didn’t really understand how to find a job or support myself. I moved from my parent’s house to my husband’s house. The first few years I was working I had my husband manage our money because I was earning minimum wage and I did not want to know what my weekly paycheck was because I was afraid if I knew how little it was, it would be easier for me to mentally justify calling in sick when I didn’t feel like going. I was unemployed from 2012 for most of the rest of our marriage. I have never budgeted my money. I have never paid my own bills. I never lived on my own. I don’t think I filed a tax return until I got married because I earned no income.

The last six months have been difficult. It’s been hard trying to learn things that most people knew when they were 16. It’s been hard being away from my pugs so much every day. My house is still a mess because I am simply too tired and demoralized to clean it. I don’t think I’ve put my laundry away since February. My ex-husband was actually so appalled by my inability to care for myself that he cleaned my kitchen one day when he was here picking up some of this things.

The last six months have been physically and emotionally exhausting. I have had bouts of depression that I have spoken about on this blog before. It’s been rough.

Because things have been rough, I have been letting a lot of things go. One of those things has been my focus on my long-term goal of being a great programmer.

I have barely been getting by.

I have had weeks where I have barely gotten anything done. I have felt a great deal of self-hatred and fear about losing my job and independence.

At a certain point it became easy to rest on my credentials. I wrote a book. I was invited by Saul Mora to interview people for NSBrief. I had lots of people who knew who I was. I was given some cache for being a great developer because I had credentials. I didn’t want to think about how little I actually knew because it was too fucking demoralizing to deal with. So I pretended. I even convinced myself that I was as good as I needed to be. I got careless. I stopped trying because it was just too hard to deal with.

Now

I am not doing this any more.

I am tired of what I have been doing for the last year.

The reason I told my long, rambling story at the beginning of this post is because I feel like I am back there now. I feel like I have two choices about how I can proceed from here.

I can either decide that I learned enough and keep hiding behind my credentials. I can learn fast enough to keep up with things, but I won’t ever become the developer I wanted to be. I will have to keep pretending like I know more than I do.

I don’t want to do this anymore.

I want to be very clear about a few things.

Chris wrote most of the book. My name is on the book and I wrote three chapters from the book. I wrote the Debugging, Testing, and Photos chapters. Two of those chapters didn’t really require me to add much to the code base. When I had to tackle the Photos chapter I had to fly out to CocoaConf Boston to work directly with Chris on that chapter because I was not familiar enough with the code base to write the chapter by myself. I needed help with the Core Image code.

Chris worked on the book for two years before I was brought on. I think my presence played an important role in getting the book done. I contributed to the book. But, I feel that I have received more credit for the book than I am due. I haven’t read the book all the way through. I am not comfortable with most of the concepts in that book.

I am tired of waiting around for someone to figure out that cut to the front of the line. I am going to be proactive and admit that I don’t really know that much. I have never written an app of my own. I have focused on some very obscure and esoteric low level programming. For a developer who has only been working for a year and a half, I am fairly advanced. However, I have a long way to go.

I have realized that I have been squandering my opportunity of working with Brad by being too proud and afraid to ask him for help. I have been ignoring things that I don’t understand. Yesterday I asked Brad about what provisioning was. He could have explained it in a sentence. Instead, he took an hour to show me Instruments and how it could be used to analyze one of his projects. I felt like my world went from 2D to 3D. I realized that there was a whole dimension of being a programmer that I used to be aware of and wanted to explore that I had abandoned because I was tired and demoralized and didn’t want acknowledge how little I know. I felt exhilarated and deeply, deeply ashamed.

I do not want to be afraid anymore.

I am working on my own personal application. I am going to do my best to write about that process on this blog. I will be talking about concepts that are probably covered in the book that has my name on it. I will talk about things that most people who have been programming for five years already understand.

I want to go back to the spirit this blog was created in. I created it to document my progress as a student to a professional. True to my roots, I am a perpetual student.

I am going to ask more questions. I am going to delve deeper and try to understand better. I made time for what was important for the first half of this year. Now I need to get back on the track I decided a year and a half ago. I need to stop worrying so much about my exposure or my reputation and worry more about my skills. If my reputation suffers because I want to shore up my weak points, then I probably deserve it. It’s painful to admit that you don’t measure up, but if you won’t accept reality then you can’t change it.

I hope that Brad will continue to be patient with my questions and continue to go the extra mile to expose me to things I wouldn’t have thought of otherwise. His commitment to excellence pushes me to be better and even though it is painful sometimes, it is something I asked for that is rare in this day and age.

We Didn’t Start the Fire(Wire)

I have written about this before, but at my current job my boss and I are rewriting our robotics control software in Swift. This is an excellent blog post here that explains why we are doing this.

This is the camera setup we have on our robotics systems. Cameras help with dispenser positioning and we support both video and image capture for our users.

This is the camera setup we have on our robotics systems. Cameras help with dispenser positioning and we support both video and image capture for our users.

There have been a few projects we have open sourced after implementing them in this project. This blog post details the most recent project we have completed and open sourced, which was to write a wrapper class allowing us to connect to an external camera that conforms to the IIDC standard. This project can be found here.

Cameras are an important feature in our robotics systems. Users use the camera to help position their dispensers and to capture media. Videos and images of the dispensing process have been used in papers and documentation of scientific research, so continuing to support this functionality is vitally important.

What is the Goal?

Back when the code was initially written in 2007, AV Foundation and GPUImage did not exist. There was not really an easy way to hook up an external camera to an application. Additionally, the standard for rapid data transfer at the time was Firewire.

The fact that there were no easy solutions meant that our code was overly complex. There were much easier ways to connect to a camera and run the video through a filter that we simply couldn’t implement because our code touched too many other things. We set out to simply the code in our rewrite.

One major goal of this project was to make it easier to add additional cameras while still supporting the legacy cameras out in the field.

Since this company has been around for over a decade, we do have legacy hardware out in the field that we still need to support. Currently we have three different kinds of cameras out in the field associated with our robotics systems: Unibrain, Point Grey Flea2, and Point Grey BlackFly. At some point in the next year or so we will need to support a fourth camera because our current camera, the BlackyFly, has been discontinued.

What is IIDC 1394?

IEEE 1394 is a serial bus standard for high speed, real time data transfer. USB is another serial bus standard that is more widely adopted because IEEE 1394, aka FireWire, was proprietary to Apple.

Our first camera type, the Unibrain camera

Our first camera type, the Unibrain camera

Even though FireWire ports are no longer available on Macs being sold today, there are still many cameras that conform to the IEEE 1394 standard. Our current Point Grey BlackFly cameras have a USB 3 plug but they conform to the IEEE 1394 standard.

IIDC is the FireWire data format for live video. In order to be able to interface with an IIDC compliant camera, we have to conform to their standard.

There is a library to interface with IEEE cameras, libdc 1394. We have integrated that library into our project and adapted it in order to be able to communicate with our cameras. This library’s functionality is what we are wrapping in our GPUImageIIDCCamera class.

We did not integrate the GPUImageIIDCCamera class into the primary GPUImage framework. The libdc 1394 library has less permissive public licensing than GPUImage has, so for legal reasons, the class could not be merged into GPUImage proper and must remain a separate entity.

Objective-C? Why Not Swift

Taking a legacy piece of software that integrates with hardware is something of a challenge. Since Objective-C is a superset of C, there was a lot of low level C programming that could easily be integrated into the previous iteration of the control software that now presents some challenges when we attempt to implement them in Swift.

One such challenge was figuring out how to interact with our hardware. Prior to attempting to connect and control our camera, we had to determine how to talk to our micro controller. We were able to do this within the current constraints of Swift, but there is one feature of the C language that Swift does not yet support, which is mutable function pointers.

Since this was an integral part of our process, it was necessary to write this class in Objective-C. This, for the record, is the first time in our six-month process where we encountered a problem that we could not code in Swift. This didn’t prevent us from being able to implement this feature, it simply meant that we had to finagle a few things to fully integrate the Objective-C class into our control software code.

What do we Need the Code to do?

These are several things we needed this class to accomplish:

  • Connect to the camera
  • Capture frames
  • Set up the proper video format for the camera type
  • Remap the YUV colorspace to RGB colorspace
  • Get and set camera settings for things like brightness and saturation
  • Handle camera disconnection

Challenges

One of my personal challenges was simply understanding the code. Since much of our functionality would be done differently in the new code, I couldn’t just port it over from the old version of the software. It was important to get a sense of how to wrap the IIDC functionality in such a way that it would be easy to implement new cameras into our process. It was also important to figure out what lifting would be done by GPUImage and what would be done by the IIDC camera class.

Our current camera, the Point Grey BlackFly

Our current camera, the Point Grey BlackFly

Additionally, Brad did some extra work on our version of libdc1394 and his changes had not been documented. I couldn’t use the general documentation, what little of it there was, for the code.

Initially we thought that we would not need to use any OpenGL to process the video frames. It was later determined that a shader would be necessary for finding the frame size. This was beyond my present OpenGL experience, so Brad needed to write the necessary shader to accomplish this.

We also had to deal with different video modes. There are about thirty types of video modes we have access to, but all of these boil down to one of two types: Format 7 or anything else.

Format 7 allows you to set the frame size and the colorspace. All of the other video modes specify those things in their mode name.

Point Grey Flea2 camera mounted on our Desktop system

Point Grey Flea2 camera mounted on our Desktop system

Not all cameras support Format 7. Our first camera, the Unibrain, does not support Format 7. So we needed to make sure we were able to connect and use both Format 7 and non-Format 7 cameras.

We also had to deal with the fact that we were talking to a piece of hardware. Those settings, along with brightness, saturation, and others are all set on the physical piece of hardware. We can communicate with the hardware using C functions, but the point of wrapping this class is to avoid having to touch the messy underlying C library.

Each property associated with the camera that we can set has overridden getters and setters. We override them in order to make sure the camera and the application are on the same page about what each expects the settings to be. When you drop this class into another application, it appears to work the same way for the programmer with all the nasty bits tucked away in accessor methods.

Final Thoughts

When I worked on figuring out libxml2 at the beginning of the year I thought that was the hardest thing I would work on. That was just a warm-up for this project.

This was a huge challenge for me personally. I think trying to figure this out has been the hardest thing I have done in my career so far. In addition to how difficult this has been, not working with Cocoa since 2014 has made trying to get back in the swing of Cocoa development has been a bit of a challenge.

I hope that as I progress in my career it gets easier for me to pivot from low to high level development more easily. I wish I could have done this entire thing by myself, but I understand that we have deadlines that need to be met. I am proud of the amount I was able to do here and the growth I have experienced as a programmer by pushing myself to work on something this difficult.

Why I Hate “Up”

Look! If you look quick you'll see my life fly by!

Look! If you look quick you’ll see my life fly by!

This morning I was reading this piece by John Scalzi. There are a lot of people angry about the last episode of Game of Thrones that seems to have a rape scene in it that serves no narrative purpose. As opposed to all the other rape scenes that somehow serve narrative purpose. Bygones…

For years I have been telling people, mostly men, that I hate “Up”. I am angry that the only female character in the whole movie dies within the first ten minutes. I am met with looks of disgust from these guys who all tell me that I obviously missed the point of the movie.

My annoyance has been rattling around my head for many years, but I was never really able to articulate the exact words as to why it pissed me off so badly. This quote from Scalzi’s piece really helped to crystalize in my brain why I hate Up:

I can’t specifically remember what the story idea was, but I vaguely recall it being some sort Silence of the Lambs-esque thriller, in which an investigator and a serial killer matched wits, you know, as they do. And at some point, I dragged the investigator’s wife into the story, because, as I was, like, 24 years old and didn’t know a whole hell of a lot, I thought it would be an interesting character note for the investigator, and a good plot development for the book, for the serial killer to basically rape and torture the wife.

I thought it would be an interesting character note for the investigator.

Women in Refrigerators

The idea of the wife/girlfriend/mother being attacked by the villain is not a new observation. There is an entire trope around it.

When you have something as blatant as Gwyneth Paltrow’s character being beheaded in “Seven”, it’s obvious that the character serves absolutely no purpose other than to be red shirted. No one makes the argument that her character was somehow a vital and important catalytic character in the movie when I am pretty sure she gets more screen time than Ellie does in “Up”.

I would like to argue that Ellie is not simply a woman in a refrigerator. She is part of a more pervasive trend that I have noticed in story telling where you still have flat female characters, but that somehow they have some holy purpose in that their existence saves the hero in some form or fashion, even if they can’t save themselves.

Coughing, Radiant, and Disposable

The next iteration of the Manic Pixie Dream Girl is the idea of the consumptive heroine. You have literal consumptive heroines like Nicole Kidman’s character in “Moulin Rouge” and Jessica Brown Findlay’s character in “Winter’s Tale”. This trope also bleeds over into any other instance where you have a female character dying of some kind of incurable disease whose death is used to enlighten the main character.

EllieDyingAgain, many of these films are very poorly done, so you don’t get a lot of people defending Jessica Brown Findlay’s death in either “Winter’s Tale” or “Downton Abbey.” Her deaths are more symbolic of something other than who she was as a person. In Downton it was to remind people that childbirth is treacherous and that her dad is a backwards asshole who won’t listen to the wise advice of a man of modern medicine. Her death services Lord Grantham’s character development rather than her own and acts as an After School Special about the dangers of preeclampsia.

Ellie doesn’t just die. She dies in the first ten minutes of the movie. When I looked up the film they didn’t even list the voice actress who played her at the beginning. She literally has no voice. The vast majority of her time on the screen is part of a musical montage where you see her lose her baby, get sick, and die.

Ellie for all intents and purposes dies in the cold open. If Up was an episode of CSI she would be the body they find at the beginning before the obligatory opening credits sequence blasting a song by The Who. No one would argue that the body at the beginning of the episode is the most important character in the show, yet so many people I talk to argue about how important Ellie is and how it could be argued that the movie is actually about her.

Again, this is getting closer to the heart of the issue, but it still doesn’t quite explain why “Up” bothers me so much.

Thanks for the Adventure, Now Go Have One of Your Own

This clip is what bothers me.

Here is my book of all the things I won't get to do! Huzzah!

Here is my book of all the things I won’t get to do! Huzzah!

I have already mentioned that Ellie loses her baby and eventually dies. She wanted to travel the world. She never gets to go on any adventures. This isn’t uncommon. When I was a kid I wanted to be the first female president. I know at least two people who went to college and studied rocket science because they wanted to be astronauts but never made it into space. Most people don’t wind up living the life they plan to lead. Sometimes the things we do today didn’t exist when we were children. There were no cell phones when I was a kid. The possibility of doing this didn’t even exist when I was trying to figure to what I wanted to be when I grew up. We all grow up and most of us wind up doing something different than what we thought.

It bothers me that somehow, we are supposed to have our heartstrings tugged by the fact that Ellie is satisfied with her “adventure” of being married to Carl for the last fifty years. There is nothing wrong with that. What bugs me to no end is that somehow, we’re not supposed to think that he should feel the same way.

“Thanks for the adventure. Now go have one of your own.”

So marriage was enough of an adventure for Ellie, but it isn’t good enough for Carl?

So much of pop culture revolves around the male midlife crisis. For an example, let’s look at the movie “American Beauty.”

Are you the plot device that is supposed to give my empty life meaning?

Are you the plot device that is supposed to give my empty life meaning?

The main character, played by Kevin Spacey, lives a meaningless existence. He is trapped in a sexless marriage with an anal retentive shrew who makes it clear to him that he has disappointed her. He works a dreary job that he hates where he gets no respect. Then he meets an underaged girl who gives his life meaning. He realizes he shouldn’t have settled for this dreary, miserable life. He buys a sports car, gets fired from his job, then goes to work at a fast food place so he can be stoned all day.

There are no stories about middle aged women who wake up one day and realize that they’re trapped in a marriage with a family they don’t want. That privilege is reserved only for men. Women are supposed to smile and nurture and never convey that they miss their lost youth when anything was possible. A guy can say he wishes he’d never had children but a woman who says so is a sociopath.

It bothers me that the writers of “Up” basically just made Ellie a plot device. She’s a consumptive heroine. She leads a perfect, blameless life where she is simply happy to have lived then died to teach Carl how to live.

How much more compelling would Up have been if Carl had been the one to die? Watching Ellie learning how to live after losing Carl would have been just as, if not more, compelling as watching Carl go on his adventure. Would the writer have been brave enough to have Carl leave the message to Ellie thanking her for the adventure? Do we assume that women have lower hopes and expectations for their lives than men do?

We're holding hands because this is a Disney movie and we can't show any more than this.

We’re holding hands because this is a Disney movie and we can’t show any more than this.

Pixar generally makes great movies, but I feel like they really drop the ball on thinking about female characters. We finally got a movie around a female character and we wound up with Brave, which figures that if you give a girl a bow and arrow you don’t have to give her any personality. There is no reason that every main character in most of their movies has to be male. It’s just the default.

I am very excited for “Inside Out”, the next movie that Pixar is coming out with. It is the movie I have been waiting for Pixar to do. The movie takes place inside the mind of a tweenaged girl who is going through life changes. The main emotional character is played by Amy Pohler. This movie could have had either a male or a female main character and they picked a girl. I am hopeful that this film will start to make up for all the lazy female characterizations over the last twenty years.

Death Handled Well

There is a right way to handle death as a piece of character development. The best example of this that I can point to is from the pilot of “Star Trek: Deep Space Nine.”

In DS9, Benjamin Sisco’s wife Jennifer isn’t killed as a catalyst to either hurt or save Benjamin. Her death is tragic. She is killed by an anonymous enemy that destroys thousands of people. She is also killed in the first few minutes of the episode, but her death differs in some key ways.

She isn’t some inspiration to Benjamin about how important life is and to catalyze a moment of self actualization. Benjamin is a husk of a man after he loses her. He raises their son alone while he tries to cope with the trauma of losing her. Her death doesn’t inspire him. Her death arrests him. The entire episode is about showing how he can’t move on from the moment he loses her.

I can’t find a clip of this scene, but it’s brilliant and I strongly recommend watching the whole episode just for this scene. Here’s a transcript of what is said:

SISKO: What is the point of bringing me back again to this?
JAKE: We do not bring you here.
JENNIFER: You bring us here.
TACTICAL: You exist here.
SISKO: Then give me the power to lead you somewhere else. Anywhere else.
OPAKA: We cannot give you what you deny yourself. Look for solutions from within, Commander.
SISKO: I was ready to die with her.
TACTICAL: Die? What is this?
JENNIFER: The termination of their linear existence.
(and she puts her hand on his cheek)
TACTICAL: We’ve got to go now, sir.
SISKO 2: Damn it, we just can’t leave her here. Oh, no!
SISKO: I never left this ship.
JENNIFER: You exist here.
SISKO: I exist here. I don’t know if you can understand. I see her like this every time I close my eyes. In the darkness, in the blink of an eye, I see her like this.
JENNIFER: None of your past experiences helped prepare you for this consequence.
SISKO: And I have never figured out how to live without her.
JENNIFER: So you choose to exist here. It is not linear.
SISKO: No. It’s not linear.
(and he finally starts to grieve properly)

We’re not meant to think of Jennifer as a fully fleshed out character. She is a plot device whose specter looms over the whole episode. Benjamin is stuck and can’t move on from her death. The inability to move on from a tragedy is a powerful story that all too many of us can identify with that is rarely explored in media.

Now just make sure you don't go talk to your other selves or else we will have a time paradox.

Now just make sure you don’t go talk to your other selves or else we will have a time paradox.

The difference between Jennifer and Ellie is that we are supposed to somehow feel that Ellie is a main character who had a fully fleshed out life and existence. Even though she dies in the first ten minutes, we’re meant to think that somehow because she considered her life an adventure it makes up for the fact that she conveniently disappears ten minutes in so we don’t have to worry about her pesky emotional wants and needs.

I guess I also think it’s different because I feel like Ellie was tortured before she died. She didn’t get to live out her dreams. She lost her baby. She got sick and died. She had this miserable series of misfortunes and at the end we’re supposed to feel like it’s okay because she says she had a great adventure.

I am okay with Jennifer not being a fully formed character in DS9. No one is making the argument that she is. Sisco is the only captain on Star Trek who married and had a family. Showing the story of an African-American single dad is a story we basically never see. I am not going to quibble about Jennifer getting the short straw on this because it opens up another, richer story vein. Avery Brooks shows more naked grief in this clip than Carl does in the whole movie. Any time we can show any man, let alone an African-American one, cry from grief without being shamed for not manning up is okay by me. We need more of this.

Bringing it Back Around

So what was it about John Scalzi’s piece that really crystalized these thoughts for me?

I thought it would be an interesting character note for the investigator.

Someone who came up with the story for Up decided that killing Ellie would be an interesting character note. Her character was made to be this perfect woman that any man would love to be with. Her purpose was to make Carl happy and to have no agency of her own. Someone thought that having her die would be an interesting story note for Carl. There were no other female characters in the whole movie except for Kevin, who is assumed to be male for most of the movie.

All the men who come up to me and tell me that I obviously didn’t understand what the movie was about are coming from this perspective. They put themselves in Carl’s shoes and think about how happy they would have been to have had a perfect girl save them and be with them for fifty years and think that the life they led together was enough.

Me, I saw this movie when I was unemployed and I had no idea what I was going to do with my life. Seeing this woman who was very much like me give up on everything she wanted to give up and die chilled me to the core.

I did not want that to happen to me.

I did not want to lower my expectations to the point where I would give up on ever doing anything with my life. I have fought for over five years to try and make something of myself because I knew that I would not be happy having the life that Ellie led. I dare say most women would not be happy leading that life. Some men want to believe that women don’t have any aspirations beyond getting married and having a family because it absolves them of any responsibility for situations they are unhappy about. It allows us to think that somehow men and women want different things. Men want to be free and have adventures. Women want to settle down and nest. Somehow it’s not possible for both men and women to want the same things.

I think that we are limiting ourselves by writing off the other gender as unknowable and stereotypical. Not all women want to have children and not all men want to be perpetually single. If you go through life without trying to put yourself in another person’s shoes, you will miss a lot of the things that make life amazing. You might accidentally answer a personal ad from your wife because you never thought she might like pina coladas and getting caught in the rain.

up-pixar

The Trick to Forgetting the Big Picture is to Look at Everything Close-Up

Any sufficiently advanced technology is indistinguishable from magic.
– Arthur C. Clarke

Back when I was a programming student, my teacher Eric told me that over the years he has learned and forgotten a dozen programming languages.

At the time, it was inconceivable to me that anyone could learn and forget so much. Two years later, I am shocked to discover that he was right.

Two years ago when I really doubled down on learning iOS programming, I worked on it eighty hours a week. I was working through the Big Nerd Ranch iOS book. They had a series of about five chapters putting together a table view that would display a detail view populated by a singleton.

Every day I would wake up and code this over and over again. The first time I coded the examples, they made no sense. I typed a bunch of words that didn’t set off the compiler warnings, ran them, and magic happened. The second time was not much better. But by the third for fourth time, I began to realize, “Oh, I am creating this object because later when I load this detail view, I will be showing all the stuff I am keeping in this object. This is where it comes from.”

At the time Storyboards Interface Builder wasn’t particularly good for things. If you listen to many people online, their assessment of this situation has not changed. I used .xib files for each of my view and my custom cells and did all of my transitions programmatically. It took me weeks to wrap my head around all of these moving parts to figure out how they worked together. It wasn’t enough for me to just have something work, I really wanted to understand it.

Over the last year and a half, I haven’t really worked with user interfaces much. I had a contract job where there was no UI in Interface Builder because it was a legacy project from 2008. Then I spent a bunch of time running around like a chicken with my head cut off trying to figure out shaders, which are a tiny subset of a program. Then for the last eight months I have been working on porting another legacy project to Swift. We are just now getting to the point where I am working with interfaces again. It’s been over a year since I dealt with interfaces.

I am working on my first application. It is going to be rather limited to start off with, but I have plans to add additional functionality over time, so whenever I finally get it out, no, that is not the final product, more will come later, so don’t give me crap about it.

I am working with HealthKit. In HealthKit, there is a HKHealthStore that you are only supposed to have one instance of in your entire application.

I have been trying to figure out where to make that instance. I know that it needs to be accessible through the entire application and that you’re not supposed to make a bunch of instances of the same thing. I also know you have to pass it along to a lot of different places. I know many people don’t like singletons and I don’t want to create one of those, even though I am pretty sure HKHealthStore is a singleton. I was trying to figure out how all of the controllers can know about something while minimizing global state.

I talked to Brad about this a bit and he was talking about how it should be created in the root view controller for the application because that is responsible for the views that are controlled by it. As he was talking about all this stuff, it dawned on me that he was talking about the same things I was bashing my head against two years ago.

It wasn’t like I had spent a week mucking around with this stuff. I spent eighty hours a week for MONTHS trying to piece together how all this crap worked. I can’t believe that after spending all that time and pain on these concepts that they were buried in some far corner of my brain.

It also made me wonder about all the people who are learning programming now who use storyboards because, honestly, they are easier to get things done quickly. If you just push a couple of buttons and things happen like magic, how do you get a full understanding of what is actually going on? It makes me wonder about what else I don’t know about because I came into programming relatively recently. I know that my knowledge of memory management is bad because it was never something I had to deal with. I came in around iOS5/iOS6, so we had ARC and GCD and a lot of other things that abstract out a lot of the lower level programming stuff from you. Will understanding how root view controllers own detail views go the same way? I know talking to a guy at my first job he didn’t seem to understand this concept and it drove me crazy. I guess I have gotten to the point where I don’t understand it either.

It frightens me about how vast the knowledge is of everything that happens within the iOS ecosystem and how incredibly difficult it is to remember everything because really delving into the low level stuff means that you don’t know how to get things done quickly in the abstracted level. Sticking to the abstracted level limits your ability to do anything really customizable because you don’t see how the pieces fit together.

I am hoping that over the course of the next few years I can figure out a balance that works for me. I hope I can remember enough about how things work that I can deal with the abstracted layer without fundamentally forgetting everything.