What’s an Object?

I was editing my interview with Orta Therox yesterday when I realized I mentioned asking my boss Brad Larson what an object was.

I realized when listening to this that I hadn’t given any context because I wasn’t anticipating talking about that during the interview and I come off being rather ignorant in the course of this question. So, to avoid having to turn in my nerd card, I am going to give some context and give a more nuanced explanation behind that question.

My Personal Background with Object-Oriented Programming

I started going to school in 2010 for computer programming. I was 28 and I had almost no experience whatsoever with computer programming. My background was writing HTML when I was in high school and learning how to compile “Hello, world!” in Perl. At the time I enrolled I was working at Target because I failed to find a journalism job after I graduated and I really didn’t want to work at Target for the rest of my life. Programming seemed like it would be a good fit and it didn’t seem like it was going anywhere for a while.

I got through my first semester of introductory programming okay. Starting in your second semester, you had to pick a language to study for the next three semesters. At the time I was making this decision, there were three languages to pick from: Java, PHP, and VB.Net. I decided I would pick whichever one fit into my work schedule. The one that fit was VB.Net.

Our introductory class kept getting further and further behind. By the time we got to the end of the semester, we had never spoking about object-oriented programming. It was something we were supposed to do, but we just never got around to it.

The following fall, I took the advanced class. It was a night class, which I don’t really do well with anyway. The first class period, my teacher showed me object-oriented programming for the first time. I had no idea what the fuck he was talking about.

A bunch of code was in other files! How did it work together? How does the computer know to look in these other files?? Wait, what the hell is polymorphism? Am I having a bad acid trip??

After three hours of this, the teacher said since this was all review he was never going to go over any of this ever again. That was when I realized I was kind of screwed.

I decided to drop out after I asked what a constructor was and the teacher asked me if I learned anything the previous semester. At this point I had a decent job with people that I liked. I figured I was too stupid to learn programming because I wasn’t born with an intuitive sense for it and I was tired of feeling stupid all the time.

Six months after this, I took a new job that was the most miserable job I have ever had. The plus side to this job was that I taught myself programming while I was there. I realized I hadn’t put enough effort into it and if I just programmed forty hours a week I could learn it. So I went back to school.

I finally grokked OOP when I took the Intro to Java class. They introduced objects immediately and forced you to use them throughout the whole semester so you could get used to them. I started to understand about file paths and how the program knew how to find your other classes and how you imported them and how you constructed them. I felt like after I understood this for the first time that I was a “real” programmer. I hadn’t felt that way before grasping object orientation.

OOP is Not the Only Paradigm

I went from Java to iOS/Cocoa/Objective-C programming. I had a few vague issues when I was learning Objective-C with trying to pass around things that weren’t objects. Rather than really think about it at the time, I just got annoyed and turned all of my ints and floats into NSNumber objects to shut up the compiler.

Here is a sample header file you might commonly make if you were creating an Objective-C project:

@interface MyViewController ()
 
@property (nonatomic, strong) UIImageView *backgroundImageView;
@property (nonatomic, strong) UIImageView *blurredImageView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) CGFloat screenHeight;
 
@end

In my experience with Objective-C, everything was an object. You created classes, which were objects, and you populated them with instances of other objects and in turn instantiated your new object in instances of UIViewController objects.

You had header and implementation files. The name of your class was the same as the name of your implementation file. Every file has a class. This is what programming is. It’s objects all the way down.

My first experience with understanding that this wasn’t the only way of doing things came several months ago. I was working on translating a plug-in we were using to a piece of code we kept internally. I had a weird method I was dealing with that wouldn’t compile.

After fighting with it for a while, I took it to Brad and asked him what I was doing wrong. He told me I was treating the function like a method. This statement kind of blew my mind because I thought that methods and functions were the same thing. I thought we called functions methods in Objective-C the same way that Java interfaces were the same thing as Objective-C protocols. (Yes, I am aware I am probably wrong about this as well. I am still learning.)

I didn’t know that there was a difference in Objective-C between functions and methods. Functions exist outside of classes and methods are functions that are embedded in a class. I hadn’t really thought about this or why anyone would want to do that. I simply assumed that all functions were methods and everything had to be encapsulated in a class.

This was only the tip of the iceberg.

Becoming Functional

Back in November, Brad went away for a week for a trade show. We were planning to rewrite all of our control software in Swift and he wanted me to take a look at the code he had written so far to make sure that I understood it.

What I saw completely broke my reality.

He has posted a sample project on GitHub including some of the code I looked at initially. Here is a small sample of what I saw that changed the way I think about programming:


import Foundation

// MARK: -
// MARK: Error protocols

public protocol ErrorType {}

protocol PresentableError {
    var errorTitle: String { get }
    var errorInfo: String { get }
}

// MARK: -
// MARK: Error types

enum CommunicationsError: ErrorType, Printable, Equatable {
    case ReadWriteTimeout
    case WrongByteCount(expectedByteCount:UInt, receivedByteCount:UInt)
    case CorruptedResponse(expectedResponse:[UInt8], receivedResponse:[UInt8])
    
    var description : String {
        get {
            switch (self) {
                case .ReadWriteTimeout: return ".ReadWriteTimeout"
                case let .WrongByteCount(expectedByteCount, receivedByteCount): return ".WrongByteCount(expectedByteCount:\(expectedByteCount), receivedByteCount:\(receivedByteCount)"
                case let .CorruptedResponse(expectedResponse, receivedResponse): return ".CorruptedResponse(expectedResponse:\(expectedResponse), receivedResponse:\(receivedResponse)"
            }
        }
    }
}

enum ElectronicsError: ErrorType, Printable, Equatable {
    case ElectronicsDisconnected
    case UnrecoverableCommunicationNoise
    
    var description : String {
        get {
            switch (self) {
                case .ElectronicsDisconnected: return ".ElectronicsDisconnected"
                case .UnrecoverableCommunicationNoise: return ".UnrecoverableCommunicationNoise"
            }
        }
    }
}

// MARK: -
// MARK: Equatable protocol compliance for these errors

func == (lhs: CommunicationsError, rhs: CommunicationsError) -> Bool {
    switch (lhs, rhs) {
        case let (.ReadWriteTimeout, .ReadWriteTimeout): return true
        case let (.ReadWriteTimeout, _): return false
        case let (.WrongByteCount(expectedByteCount, receivedByteCount), .WrongByteCount(expectedByteCount2, receivedByteCount2)):  return ((expectedByteCount == expectedByteCount2) && (receivedByteCount == receivedByteCount2))
        case let (.WrongByteCount, _): return false
        case let (.CorruptedResponse(expectedResponse, receivedResponse), .CorruptedResponse(expectedResponse2, receivedResponse2)): return (equal(expectedResponse, expectedResponse2) && equal(receivedResponse, receivedResponse2))
        case let (.CorruptedResponse, _): return false
    }
}

func == (lhs: ElectronicsError, rhs: ElectronicsError) -> Bool {
    switch (lhs, rhs) {
        case (.ElectronicsDisconnected, .ElectronicsDisconnected): return true
        case (.ElectronicsDisconnected, _): return false
        case (.UnrecoverableCommunicationNoise, .UnrecoverableCommunicationNoise): return true
        case (.UnrecoverableCommunicationNoise, _): return false
    }
}

The name of this file is ErrorTypes.swift. There is no ErrorTypes class. There is nothing in this file named ErrorTypes.

NOTHING IN THIS FILE IS AN OBJECT!!

We have several enums, which are not objects, that we are references for type conformity. We have UInts, which are not objects. We also have functions. Lots and lots of functions. That aren’t associated with a class. They’re just out there like llamas roaming free.

When I tried to understand this I felt like I went to a party and someone asked if I wanted to use another person’s liver. I didn’t know if was possible to call functions that were not associated with a class instance. I had never seen anything like this before. I couldn’t believe this would build and run and compile just fine. I felt like I fell down a rabbit hole and nothing made sense anymore.

Reorienting Myself with Reality

I, like many people, learned programming in the age of Imperative Programming. Java has been around for twenty years and many people learned programming with Java. A lot of us don’t know anything except the Object Oriented way of doing things. To many of us, this is what programming is.

It doesn’t have to be.

One reason I am so vocal in my defense of Swift is because this realization has completely changed my reality. I used to think there was only one way of doing things. Well, I won’t say that. There was one right way of doing things and then there was the “Dear god, what is this person thinking by having this property controlled in four different places?!” way of doing things.

Being exposed to Swift and seeing that you don’t have to put everything in a class has been a revelatory experience for me. It is forcing me to reevaluate everything I know about programming.

I never thought about what an object was before because there was no point because everything was an object. Now, I am trying to get a better understanding of what an object actually is. I wrote about the difference between structs and classes and at the time I really didn’t understand why you would want to use a struct instead of a class if they essentially do the same things. I now understand that you want to try to use structs when possible because they aren’t objects. Objects come with a lot of overhead. They let you do some more powerful things like subclassing through polymorphism, but you don’t always need to do those things. Looking at how powerful the enums are that Brad uses in his code, I am fascinated by how confined my own view was when I thought everything had to be an object and exist in a class.

So yes, I did ask what an object is. I know most programmers worth their salt can tell you the definition of what an object is, but I don’t think many of them stop to think about why we use them and if they are the best way of doing things. Or if they bother to wonder if objects are the only way of doing things.

To Make Life Important

There is a scene in the last episode of the first season of Six Feet Under that asks an incredibly important question:

Tracy Montrose Blair: Why do people have to die?
Nate Fisher: To make life important. None of us know how long we’ve got. Which is why we have to make each day matter.

I think about this a lot in my more morbid moments, which I have had more of recently.

I have spoken about being depressed somewhat openly because I think it’s important to acknowledge that people get depressed. I think we tend to stigmatize people who don’t conform to what we think of as “normal,” which includes people with depression, the transgendered community, and people who experience things most others do not. I am somewhat dismayed by our society’s uncomfortableness with others having non-normative emotions and experiences.

I saw several amazing talks by Leon Gersing, aka Ruby Buddha. Seriously, if you ever have a chance to see him speak, DO IT!!

In one of his talks, he said he spoke to people about what their goal was in life. Many people told him their goal was to be happy. He laughed at that and said, “Good luck with that! Happiness is an emotion. Happiness is transitory. Happiness only has meaning because you have misery to counter it. If you’re happy all the time, you just feel normal.”

The Neurotypical Dance of Shame!

The Neurotypical Dance of Shame!

I was at the doctor and I saw this book: “Eeyore, Be Happy”. Eeyore is the perpetually depressed character from Winnie the Pooh. I was rather disturbed by our society’s willingness to ignore his feelings because they make us uncomfortable. We have an idea about how we want people to be and we don’t want to recognize the ugly reality that life is more complicated than we care to believe.

We want Eeyore to be happy because his depression makes us feel bad. We want everyone to be happy. I see similar feelings expressed by introverts who feel like we, as a society, value extroversion so they are supposed to conform to our expectations and feel incredibly uncomfortable with trying to be themselves.

I have spoken to a lot of people who feel that their natural emotions and feelings are seen as deviant and threatening who are incredibly depressed because they feel they are not “normal.” I try to do what I can to assure them that they are not creepy or deviant, but there is only so much I can do.

I have had people approach me about going to see a psychiatrist and getting on anti-depressants to make myself feel “better.”

I respectfully decline to do so.

I do not want to numb my experiences. I am going through a rather traumatic time and I don’t think taking a pill that blunts this experience is the best thing for me. I would rather experience my sadness so that when it alleviates and I feel happy again, it will mean something.

I know our society is uncomfortable with melancholy and sadness. I know we want everyone to feel happy all the time and not talk about our various disappointments and let downs. We don’t want to hear about people’s failures even if they learned far more from a failure than they did from a success. I don’t think that’s healthy. I think for happiness to have meaning, you need something to contrast it to.

I am not saying I don’t think anyone should take anti-depressants. I know if you are thinking of taking your own life that numbness is probably preferable to thinking that there is no point in going on any longer. I am just saying that it is my choice to not take anti-depressants. I am trying to use meditation and mindfulness to deal with my depression. It is my choice to fully embrace and experience my sadness. I want to experience my feelings in my own way and everyone is entitled to choose how they deal with their own lives.

I am bothered by the fact that we have an idea that there is only one way of doing anything. I think there are many ways of doing things and what might work for 100 people might not work for you. You should be able to choose how you experience your life and no one should pressure you to do something you don’t want because it doesn’t fit in with their narrow view of what is “normal.”

I feel incredibly privileged to be in a position where I can live my life the way that I want. I know many people are not so lucky, which is why I am speaking about this. It would probably be safer for me personally not to disclose this information publicly, but I feel that it is important to talk about it because if we ever want anything to change, we need to acknowledge that a problem exists. I am sick of seeing my friends pretend to be someone else because they don’t feel like they themselves are okay. I would like to change that.

Wrapping libxml2 for Swift

I have spent the last month or so working on a project where I am wrapping a legacy C toolkit, libxml2, in Swift. I have created a sample project here where you can obtain wrapper classes written in Swift allowing you to utilize libxml2 into your code without having to touch C.

My job at SonoPlot is to write control software for our robotics systems. One of our included pieces of software is a CAD program for users to use that creates XML pattern documents that that are parsed by our control software so that it can tell the robotics how to draw a pattern.

Since some of our pattern files can be very large and complex, it was important to us to be able to use the fastest XML parsing we have available. I benchmarked three different ways of parsing XML:

  • Tree-Based Parsing using NSXMLDocument
  • Event-Based Parsing using NSXMLDocument
  • Tree-Based Parsing using libxml2

I had wanted to include event-based parsing with libxml2, but that utilizes C function pointers, which are not currently possible with Swift.

I have included the profiler in my sample code for this project, but in case you don’t feel like running it yourself, my benchmarking showed that using libxml2 was four times faster than tree-based NSXMLDocument parsing and three times faster than event-based NSXMLDocument parsing.

Clearly, there is a large difference between using libxml2 and any permutation of NSXMLDocument.

What is libxml2?

libXML2 is a toolkit for parsing XML that is written in C. Back when the first iPhones came out, libXML was highly utilized because it was incredibly fast and the first iterations of the iPhone were not particularly powerful.

As the iPhone has become more powerful and the number of frameworks has become more robust, libXML2 has fallen out of use. Since it is a C toolkit, you are taking responsibility for your memory management. It wasn’t written for a specific language, so it is rather sprawling and has a lot of obtuse documentation. It’s also rather old. When I was looking into it they had sample code written in Pascal.

Basically, as things like NSXMLParser and the use of JSON became more prevalent and easier to work with, there hasn’t really been a need for most people to bother with this difficult toolkit.

Hooking Up libXML and the Wrapper Classes

libXML is included on your machine, but it isn’t included by default in Xcode. It will need to be important and linked to your project for Xcode to be able to see it.

Go to your project and find your Build Settings in your Project. Search your Build Settings for “Header Search Paths”. In your project’s search paths, add the following:

$(SDKROOT)/usr/include/libxml2

While still in your Build Settings, search for “Other Linker Flags.” Add this line to your linker flags:

-lxml2

Now go to your Targets and find your Build Phases. Find the tab that says “Link Binaries with Library”. Click on the “+” sign to load a new library to link to the project. Search for “libxml2.” There should be two results. Add both of those results to your project.

Lastly, you will need to import an Objective-C bridging header into your project. The Objective-C bridging header is a file that exists in this project, so you just need to drag it from the sample project over to your project.

After the bridging header is added to your project, go back to your Build Settings and search for “Objective-C Bridging Header”. You will need to add a line that has your project name and the bridging header name in it. For example, in the sample project LibXMLWrapperExample, the line added to the bridging header was:

LibXMLWrapperExample/Bridging-Header.h

A really good README going over this process is here. If any of my steps aren’t clear enough or something isn’t working quite right, use this as a backup for the instructions to hook this up. It can be incredibly obtuse and easy to forget one little step and have the whole thing not work.

Also, the way that I verified this worked was to look at a project I got this working on and I searched both the Target and the Project for “xml” to see where I had added search paths and linker files. This is a good way to do a sanity check to make sure that you didn’t forget anything.

LibXMLDoc

The first wrapper class I created is the simplest one: LibXMLDoc. In XML, you have a tree that is made up of nodes. In libxml2 all of the nodes are contained in an xmldoc. You need two things from the LibXMLDoc class : a document and a root node. Once you are able to extract the root node, you are able to traverse the entire tree to find anything that you are looking for.

Since we are dealing with C, we are responsible for our own memory allocation and deallocation. I had to remember to deallocate the LibXMLDoc when we are done with it.

LibXMLNode

The second class, LibXMLNode, is where we do a lot of the heavy lifting. This class is responsible for finding and extracting values from our XML document.

The first property I needed to set up was nodeName. nodeName is going to be the primary way we are going to be accessing and dealing with each node, so this is a pretty important value to have easy access to.

Since libxml2 is a C toolkit, the type we are receiving is going to be a C string. The other thing complicating this is that libxml2 is set up to deal primarily with pointers to their node and document objects. We need to take a pointer to a C string and somehow do some alchemy on it to convert it to a Swift string. Swift strings have a method on them called “fromCString” that I was able to use. Also when dealing with C string types, you need to use unsafe mutable pointers. I also had to figure out how to navigate through a C pointer to access the actual values that they were referencing.

I was able to get this all down to one line of code that did all of these things:

return String.fromCString(UnsafePointer(self.xmlNode.memory.name))!

From there, I needed to think about what we need to do with a node. Every node we encounter is generally going to be one of two ways:

  • It will be a parent node that contains children nodes but no value.
  • It will be a child node that might contain a value but has no children.

I set up two lazy properties in the LibXMLNode class to deal with these two eventualities: nodeValue and nodeChildren.

nodeChildren takes a LibXMLNode as a parameter and iterates through that node’s children until it encounters “nil.” It then returns an array of LibXMLNode objects.

The first time I wrote this code, I was getting many more nodes than I was expecting to get. By printing out the array of node children I found that every other node was a node, which mean that if I was expecting five children nodes, I was actually receiving eleven, because the first and last nodes were nodes and every other node in between was also . This was rather inconvenient, so I found a libxml function that checks to see if the node is a node. If it is, the function returns 1. I do a check on each node and if the function returns 0, the node is added to our LibXMLNode array.

nodeValue covers the other contingency where you have a node that contains a value that needs to be extracted. Since it is possible to have empty XML tags that do not contain values, this property has to be optional. We need to extract the value and look inside to see if we have anything in there.

I used the libxml2 function “xmlNodeListGetString,” which takes a document, a node’s children, and their index as parameters and returns either a C string or nil. If there is a value, we use the code we used in the nodeName function to extract a Swift string, we free the C string, and we return the Swift string. If the function returns nil, we return nil.

Raiders of the Lost ARC

So I was all excited that I figured all of this stuff out and I was ready to start testing my classes. I wrote a bunch of tests that all failed immediately.

While looking over the tests, I realized that every time I tried to access any of the properties on the root node, they were failing because the root node didn’t exist.

I was becoming incredibly confused and frustrated when Brad had me add a println() in LibXMLDoc between when we initialize the document and when we initialize the root node. It turns out that ARC was deleting the LibXMLDoc immediately after it was being initialized because it wasn’t being held on to or referenced anywhere. D’oh!

In libxml, the document controls the memory for all of the nodes, not the nodes themselves. Basically the way I was writing the code was that I only referenced the document once, where it gets initialized. From there, since there were no other references to the document, ARC deleted it along with all of the nodes it contains.

That was a problem. Since the document contains all of our nodes, we really need it to stick around until we are finished with extracting all of the nodes and their values. We have to create a LibXMLNode instance in the LibXMLDoc class to hold the root node, we couldn’t just have the LibXMLNode class point to the LibXMLDoc class. We had to have them point to one another without creating a retain cycle, so the LibXMLNode class has a strong reference back to the LibXMLDoc class to prevent the instance from being deallocate before we are done with it. I then went back to LibXMLDoc and make the reference to the LibXMLNode a weak reference to avoid a retain cycle.

There is still some juggling that needs to be done in order to make sure we are able to prevent the document from being eliminated and that we are able to get the root node.

The solution utilized here was to replace the strongly referenced LibXMLNode root node with a private, internal weak root node and a computed property checking to see if this internal root node has been set yet. If it has, it is returned. If it hans’t, we extract the root node, set it to the internal root node, and return it. Since computed properties are basically methods that look like properties, for all intents and purposes we are replacing a strong reference with a weak reference and a method.

We are trying to resolve a few things by taking this path. First, we are trying to avoid having the root node and the document deallocate before we can use them. Second, since we are using a class and a class is a reference type, we want to make sure that we only create the root node once rather than having a bunch of instances all pointing back to the same memory location.

And this, kids, is why you still need to think about memory management and ARC even if you started coding after iOS 5, like I did.

...and knowing is half the battle!

…and knowing is half the battle!

Adding Bundled XML Files

The last part of this sample application that I want to cover is including XML files rather than accessing them from an internet URL.

I have included in my sample project a relatively simple pattern file generated by our CAD program to use as an example.

One thing you have to remember to do when you include an XML file with your program is that you have to include it in the application bundle. I have included a convenience function in the LibXMLDoc class called “bundleForResource” that takes the the resource name and returns an NSURL. This can then be passed into the parser where it asks for the URL of the resource.

You also have to make sure that your file resource shows up in “Copy Bundle Resources” in “Build Phases.” My original attempt at creating a sample project was trying to make this a command line application, but I wasn’t able to copy the bundle resources (because there was no bundle) and it generated an IO error.

The last convenience function I included in the LibXML wrapper classes is the “outputXMLTree” function. This function is recursive and it walks through the document tree checking each node to see if it has children or values. I am using this function in the App Delegate to demonstrate the the classes have in fact parsed the included document correctly.

Conclusions

Before attempting to work on this project, I decided I was going to avoid dealing with C in Swift as much as humanly possible. Considering the nature of what we do here, where we use serial communication in C with our control software, that was an incredibly stupid and wrong-headed way to approach things.

Yes, it is a little more complicated. It required some more work, some tenacity, and some help from my boss who has been around the block a few more times than I have. As much as part of me would like to not think about it, C isn’t going anywhere. I want to work with micro controllers and firmware in the future and deciding not to get C to play nicely with Swift is basically a non-starter for the projects I would like to do.

Even if you think that XML parsing is as boring as dry toast, hopefully the code will help you with figuring out how to integrate older C code into your projects or at least give you a clean way to add better XML parsing to your applications.

Again, the sample code associated with this post is here.

Hit me up on Twitter if you have any questions about it.

Functional Programming in Swift: Chapter 3 – Wrapping Core Image

**Note- I originally took this post down because I wasn’t really happy with how I organized it. I also believe that this series has a larger readership than I originally envisioned and I felt that the assumptions I made about this post were confusing. I am going to repost this post and figure out how to proceed in a way that is useful to everyone who is reading my blog. **

I am writing a series of blog posts analyzing the code and concepts from each chapter in Functional Programming in Swift. I am assuming if you are reading this that you are following along in the book, so I am not references their specific code snippets in my blog.

The purpose of this chapter is to show the reader how to do a very important thing in programming, which is wrap a framework or an API written in another language, such as C.

Being able to integrate lower level code into your applications is a really important skill to be able to master. Over the last month I have had a project where I had to write a few wrapper classes around XML-parsing functionality in the C library libxml2, which I will write about in a later blog post. Learning how to search the sparse documentation for this library and figuring out how to integrate it with Swift was a really interesting and painful learning experience. Even though a lot of times I wanted to hang my head and weep in despair, after I got it working I felt kind of like one of those douchebag programming ninjas that recruiters keep contacting me looking for.

This might not be the best sales pitch for why you should care about this stuff, so without further adieu, I will get to the content.

Core Image

Even though the focus of this chapter wasn’t specifically on learning Core Image, I wanted to take a moment to talk about it because it’s a neat little framework.

Core Image is a framework that allows you to add filters to photos in both iOS and Mac applications. Core Image is very similar to GPUImage, except GPUImage is open source, so you can go in and actually see how all of the shaders were written. Core Image has a few more filters than GPUImage has. In iOS 8, Apple opened up the ability for you to write your own Core Image filters, which wasn’t possible before.

Core Image is, as my coauthor Chris Adamson calls it, “stringly typed.” If you want to use a filter from Core Image, you have to reference it in a string. If you spell the filter name wrong then your project will fail silently. Whereas you can use auto-complete on other parts of your project to ensure that you don’t get felled by spelling errors, there is alas no auto-complete in Core Image.

If you’re interested in how to write shaders for either Core Image or GPUImage, I recently wrote an article about it for Objc-io. There are a lot of other neat photo articles in this issue too, so be sure to go check it out if you want to know more about how to use photos in your iOS/Mac applications.

But enough of my own buzz marketing, on to the actual content of the chapter…

Wrapper Type

Generally speaking, Core Image has a rather repetitive pattern. You take an image, filter it, then you take that result and feed it to the next filter you are using.

If you’ve ever used Core Animation or another framework that affects the output of what goes on your screen, you will know that the order you implement your changes in affects your output. The same thing goes for your image filters. If you add more than one filter to an image, changing the order will change what the output image looks like.

Since you are using a filter chain where you are taking the output of one operation as the input of another, it makes sense to encapsulate this functionality into its own type. In this case, the type is a function that takes a CIImage and returns a CIImage.

CIImage filters all take different types of parameters. Some filters only take an image as a parameter while other ones many more. We want to be able to customize our filter functions to take the correct number of parameters. Regardless of what kind of filter we are making or how many parameters they take, we want each of our new custom filter functions to return a function that conforms to the Filter type alias. Naming this function type makes our code cleaner and safer by assigning a name to it and removing code we would have to write over and over again.

Convenience Initializers and Computed Properties

We want to make it easier to extract the output image so that we can chain this filter’s output image to the input of the next instance of our CIFilter class. We are using a convenience initializer and a computed property to extend the class to customize it for our needs.

The class extension has three pieces we are going to look at: a convenience initializer, a computed property, and a typealias representing a dictionary that holds our Core Image parameters.

Any time we create an instance of a class or a struct, we have to handle initializing it. Back in Objective-C we had our pattern of [[NSString alloc] init] to initialize our instances. We still have to initialize our instances, but like many thing in Swift, this process has been simplified. Instead of the long, verbose way of initialization, we just use “()”, which is a void function call. This initialization function is void by default, but life wouldn’t be very interesting if we couldn’t customize things to suit our needs.

Just because the default initializer is a void function does not mean that it always must be. If we have want to pass in parameters to the initializer, we can do so. In the example used in this chapter, we are trying to customize the CIFilter class. We are doing that by extending the class to include the functionality we need.

The normal CIFilter initializer just takes the name of the filter being used. We want to initialize the filter with the dictionary of parameters along with the name. In order to make it easier and clearer what we are doing, we are typealiasing the dictionary of parameters and passing that type into our convenience initializer. Within the convenience initializer, we are calling CIFilter’s designated initializer, essentially wrapping the base CIFilter initialization within another function that allows us to do more.

The last piece of our extension is a computed property, which is our coveted output image. We are treating an outputImage property that is of type CIImage and instead of just setting it to a CIImage, we are checking the value for the key that we are using (which in this case is the name for the CIFilter we want to use) and returning a CIImage.

Computed properties are interesting. They do not store a value. Instead, they are a getter to find and set properties and values indirectly. It is my impression (which, if wrong, I would really appreciate someone correcting for me) that they are similar to functions but with slightly less overhead. Natasha The Robot says in this post that a computed property is a function disguised as a property. I am on the fence about what the difference is between a function and a computed property, so I am on the fence about how to handle them. Would like to write about them further in a future blog post, so hit me up with any thoughts you have at some point in time.

Compositing Filters

Anyone familiar with special effects or Photoshop might be familiar with compositing. Compositing is the art of layering multiple images and filters together to create a single new image. Compositing encompasses everything from making collages out of multiple images to generating large Photoshop pieces of art that have hundreds of layers.

The way we are using compositing here is that we are taking several simple image filters and we are combining them together to make a larger, more complex image filter. Many of the large, complex image filters in both Core Image and GPUImage are composites of smaller, simpler filters.

Core Image has an entire section of filters that are exclusively used to composite images. If you have a chance, it’s a lot of fun to play with compositing filters. If you work with Photoshop and you’re familiar with the various blend modes, these are represented in this category of Core Image filter. You can get some really awesome effects using compositing and blend modes in your photography and I encourage you to explore them.

The first composite filter we are creating is one that superimposes a color on top of the original image. It is made up of the CIConstantColorGenerator and CISourceOverCompositing. The Color Generator is just creating a blanket color layer whose output is not at all affected by the underlying image. The Source Over Compositing is simply taking one image and placing them over another. In our case, it is placing the Color Generator layer on top of our image we want to filter.

After we create this composite filter, we are chaining it to the output of the blur filter. So, first we are taking our image, applying the blur filter, then taking the output from the blur filter and applying our composited red filter to the top. We are specifying that the opacity on the red filter is only twenty percent so that the the second filter doesn’t completely block out the first filter or the input image.

Adding Functionality

Since being able to compose two or more filters together is an incredibly useful thing to be able to do and something we might need to do many times, it makes sense to write a custom function to simplify this process. Also, considering the number of inputs we are dealing with for this functionality, being able to simplify the code and avoid missed parens is a good use of our time.

The composeFilters() function being defined takes two filters as parameters and returns a new filter. In the code sample, we see that we are setting a variable to hold the output of this function, which is a filter. We are then creating a second variable that will hold the output of the filter we just created. Since the output from the composeFilters function is a filter, when you use the variable you are calling a Core Image function that is filtering the image being used as a parameter. When I read through this at first, it took me a little bit to parse apart the logic.

Infix Operators

Infix, Postfix, and Prefix notation is a complex way of labeling a phenomenon most programmers are familiar with: Where to place an operator.

Infix operators are placed between two operands. Which is an obtuse way of saying you write an equation like this:

X + Y

The operator, in this case the “+” sign, gets placed “in-between” the numbers it is operating on.

Prefix operators are placed, just as they sound, before the operands:

+XY

I’ll bet you can figure out how to write a postfix operator now:

XY+

What do these operators do? Why are there a bunch of them?

Think back to your Algebra class. Remember PEMDAS? That acronym helps us remember the order of operations. If you want to isolate two numbers that you want to add together before the result is multiplied, you had to put parentheses around them to ensure that the product was calculated, not just the first number. Infix, Prefix, and Postfix operators affect the order of operations in mathematical calculations. All equations can be represented and translated in each style, so theoretically you don’t lose any functionality by not understanding how to use each type of operator.

These operators are incredibly common in mathematics. Since Haskell and other functional languages evolved from Lambda Calculus, there are a lot of operators and notations that are foreign and unfamiliar to people like me who flunked calculus and grew up on imperative programming.

Swift lets you write your own custom operators. There is a list of ASCII characters that can be used to write custom operators. You have to specify what type of operator you are defining. If you are defining an infix operator, as we are doing here, you need to specify the direction of associativity. In standard mathematical equations we have associativity from left to right. You can choose left, right, or none. None, which is the default, means that you can’t place your custom operator next to another operator with the same precedence.

I am still trying to figure this part of the chapter out. I don’t know why the custom operator “>>>” was chosen. I don’t know if the character is significant or important. My understanding of the code as written is that instead of using a text label for our composeFilters function, we are using symbols. I know this is a common thing in languages like Haskell and unfortunately I haven’t delved into them enough to be able to fully answer my own questions about how infix operators are being utilized here. Again, if someone has an answer, I would appreciate a ping on Twitter.

Currying

Currying is the process of taking one function that takes multiple parameters and breaking it down into a sequence of functions that each take a single argument. For example, if you were writing a function that took two floats as a length and a width for a rectangle, you could rewrite this function to take the first parameter and then evaluates a sequence of functions, each of which take one parameter.

Currying is found in languages like Haskell. In Haskell, the language only allows you to pass one parameter into a function. Currying is a way to get around this constraint.

Side note: Both Haskell and curried functions are named for Haskell Curry. Curry was a mathematician and logician who hopefully didn’t get his lunch money stolen too often for having such a strange name.

Currying gives us some options for customizing our functions. Currying allows us to seed functions by supplying too few arguments and using it as a basis for fully implementing another function. It allows us to choose if we want to apply the function to all of our parameters.

Since this post is getting rather long, I don’t really want to write a comprehensive explanation of currying and why you want to use it. I will save that for another post. The concept of currying is introduced in this chapter, so I wanted to cover it just a little. I would also like to look over the code from this chapter to get a better grasp of currying before I try to explain it further.

Conclusion

There was a lot of stuff being touched on in this chapter. Core Image, infix operators, and currying. These are complicated frameworks and concepts, so again, don’t get frustrated if you got through this chapter not fully understanding how everything works.

Up next, we have our old functional friends Map, Filter, and Reduce. See you next time!

My Defense of Swift

**Note: I am using an example in my blog of a post written by a developer in our community. I have noticed many posts with the same thoughts expressed in them and I am in no way attacking this particular person, who I respect a great deal. I want to preface this with saying I am in no way attacking the author of this piece or trying to single him out, so I hope he will not be offended that I am offering a counterpoint to his post.**

This morning I had someone send me a link to this article and asked me what I thought about it.

Honestly? I am kind of sick of everyone crapping on Swift.

I am tired of everyone saying the language is broken and that you can’t write real applications with it because it’s half baked and unusable.

I don’t mean to keep waving my credentials around, but I am working on a project where we are rewriting our software in Swift. We are not writing the usual weather app/something that pulls data from an API. This is some heavy duty crap. We are writing this on the Mac. We are using a serial port to connect to outside hardware. This is a legacy project that is on its third rewrite that has been around for ten years. So far we have not encountered anything that we can’t do in Swift that is being done in the original software.

For the last month or so I have been bashing my head against a project where I am implementing libXML in our project (which I am going to write a blog post about shortly.) Taking a really under documented C library where the sample code is written in Pascal and trying to get it to work in Swift has been a demoralizing massive pain in the ass. I have had to deal with all kinds of horrors that I didn’t know existed or were abstracted away so that I would usually never have had to deal with them again.

However, doing those things has made me a better developer. Just because I came into Cocoa programming after ARC doesn’t mean that I have no reason to think about it or know how it works.

I started learning programming three years ago. Prior to that, I didn’t know how to write a “for” loop without looking it up online. I was one of those noobs that people hate on Stack Overflow who ask how to do incredibly simple and stupid things. Learning programming was hard. It forced me to fundamentally change the way I think about the world. It was also hard because I had a lot of people who had been doing it longer than I had who were crapping on me because I didn’t know who the Gang of Four were or why we do the MVC pattern, or even what MVC means.

There are a lot of things that we think are easy and intuitive that aren’t either but we think they are because they are familiar. My boss told me about working with a woman who had a PhD but had never used a computer before. Our structure of navigating through folders and files is “intuitive” to us because many of us have been doing this for bordering on 30 years.

If you have been doing imperative programming since you were 12, then yeah, some of this stuff is going to feel complicated and unfamiliar. If you don’t have a painful, visceral memory of the pain it took to learn programming the first time, you might think that all of this stuff is complicated and broken and wrong.

I, however, do remember how hard learning programming was. I find learning functional programming and figuring out Swift to be no more difficult than it was learning Objective-C. It’s different. It isn’t broken. Yes, there are some things that will be refined over the next few years, but Objective-C wasn’t born completed either. It evolved over a period of years as Swift will evolve.

I got into programming because I wanted to learn how to solve complex problems. I am excited about Swift because it affords an opportunity to solve a complex problem in a different way than we did before.

I am worried that people’s opinions of Swift are being influenced by a few people’s bad experiences with it. When I have tried to advocate for Swift because of the work I am doing, I get my experiences brushed off because of my boss. I am told that of course Brad Larson can use Swift to control robots. He’s a genius. Us normal people can’t use it because it’s too complicated.

Again, there is a difference between broken, complicated, and unfamiliar. If you go into Swift thinking it should be just like Objective-C, you are going to be disappointed. If you go into it knowing that it is unfamiliar and that you have to fundamentally approach things a different way, then you might be okay with it. If programming was easy, then everyone would be doing it. Just because something is different than the way that you are used to doing something doesn’t mean that it is wrong.

I hope that people who are discouraged will eventually change their mindset about how to approach Swift or some other hybrid language with functional features. Life gets boring if you just do the same thing over and over again in the same way. Programming is a young discipline and we haven’t yet reached the point where we absolutely know that the way we are doing things is best. It will continue to grow and evolve and change. And that’s a good thing.

Functional Programming in Swift: Chapter 2- Thinking Functionally

*Note: I strongly recommend following along in the book with this blog post. I am not going to be recreating the diagrams or the code here, so if you don’t have the book open when you are reading this post, you will be very confused and I would not like that.*

This first full chapter of Functional Programming in Swift introduces one of the most important new concepts in Swift: Higher Order Functions. I will get into higher order functions in a moment, but first I would like to give a little bit of context as to what the example of the chapter is about.

Battleship

The example used for this chapter is called Battleship. I, like many other people, associate Battleship with the board game. Since many programming textbooks use common games with known rules to teach programming concepts, it was understandable that many people thought that this example would be about the board game Battleship. This is not the case.

Back in 1994, the Office of Naval Research was exploring rewriting some of its defense software. Like any good agency, they didn’t want to just pick a programming language out of a hat. They wanted to benchmark which language would be the most efficient language to use for this project. They were concerned with a few factors:

- Development Time
– Lines of Code/How Concise is the Code
– Human Readability
– Maintainability

For a very long time, functional programming languages were considered to be impractical for real world applications. They were an interestring curiosity and another approach to programming, but the enforced static nature of functional languages and the lack of mutable variables seemed to doom functional languages to never be implemented in actual projects.

This study proved that a purely functional language could not only be used effectively in a real application, but also proved that this approach could be even better than an imperative approach.

If you look over a lot of the examples in this chapter, you will notice a lot things that are not present in our traditional games of Battleship:

- Checking the range of your weapons
– Avoiding engaging enemies too close to your ships
– Taking the location of friendly entities into your equations

These are not things we think about when playing the board game Battleship, but they are incredibly vital things to consider if you are a naval ship engaged in battle with actual enemy combatants. These are not small considerations. You don’t want to accidentally frag your fighter jets when you are aiming for an enemy ship. Any targeting program that you design must account for all of these considerations.

This beginning chapter takes this example and shows how Hudak and Jones were able to work around these constraints in a functional manner that was more efficient and easier to maintain than the traditional imperative way of approaching these problems.

Imperative Complexity

Our initial calculation is very simple. We are assuming our ship is at a location and we are trying to determine if another point is within range of our ship. Our range is represented by the radius of a circle and we are simply passing the location of our ship (position) and the distance we can shoot (range).

We are then using the Pythagorean Theorem to determine if our target point is within range of our ship. a squared plus b squared equals c squared. In our case, target.x squared plus target.y squared should be less than or equal to our range squared. Our first function is taking the square root of the two target coordinates and returning it only if this qualification is met.

In order to keep things clear, we are using a type alias so that we don’t have to remember that the CGPoint object is our position and the CGFloat is our distance. We will see type alias again, so don’t forget it.

So far, so good.

As the program grows in complexity, it becomes harder to continue to maintain the function. We keep adding additional parameters and constraints. I am going to honestly say at this point I developed code blindness and stopped looking at the code. Suffice it to say, this once simple function grows to a large mess of spaghetti that kind makes me weep for the future of human civilization.

I am not going to untangle the spaghetti code because we aren’t supposed to be doing this anyway and I am not going to encourage you to write unmaintainable code. Bad Swift developer!!

Going to skip ahead to the functional way to deal with these constraints.

The Functional and Rational Approach to This Problem

There is one very important thing to remember about functional programming versus imperative programming:

- Imperative programming is about telling the computer how to do something.
– Functional programming is about telling the computer what to do.

In the above spaghetti code, we were getting over wrapped up in how the conditional logic would deal with all of the various contingencies of our problem. If our point is in range but it is also too close to a friendly, what do we do? It is easy to get overwhelmed by the complexity of implementation.

So let’s take a step back and ask what are we actually trying to do, without thinking about how it will get done.

We want to know if our point is in a region where we can target it.

Remember when I told you to remember type alias? We are bringing it back here. A type alias allows you to assign a label to an existing type so that if you have multiple objects/properties/variables that are all the same type, you can assign a label to the one specific instance of that type.

We know that strings and numbers and classes are all types, but there is another thing in Swift that now qualifies as a type: a function.

Functions in functional programming are values, no different than strings or structs. They can be passed around and referenced exactly the same way you would work with any other kind of value. The best analogy I can think of for this is a string. In C we didn’t have real strings. Stings were an array of chars that were eventually, in later languages, encapsulated into their own type. As you work more with functions as their own discreet values, it should get less weird to think of them in this way.

We can create a type alias for a specific function type. In the example in the book, we are creating a type alias for a function that takes a Position as a parameter and returns a Bool.

So instead of our initial function that uses the Pythagorean theorem and returns the result of that calculation, we are instead returning a closure that performs the calculation. Closures are simply unnamed functions. Basically we are taking the logic we had before and putting a nice, shiny function wrapper around it. We are wrapping a riddle inside of an enigma.

Why would we want to do this?

Let me give you a real world example of this in practice:

I work at a company called SonoPlot. My boss, Brad Larson, wrote a blog post about our project to rewrite our robotics control software in Swift. Since we are dealing with interacting with hardware, there are a lot of things that can go sideways that you don’t have to deal with when you are working with just software. Sometimes something goes screwy when we are communicating with the hardware and if we just send the command again it will go through just fine. Other times, the hardware is disconnected.

We don’t always know which one of these situations we will be in. We don’t want to keep sending commands to our robotics if they are not connected or on. Alternatively, we don’t want to tell the user that the robotics are not connected when they actually are.

To get around this situation, we created a special function called “runCommandAndAttemptSoftRecovery()” that takes a function as a parameter. We wrap many of our hardware functions in this function. “runCommandAndAttemptSoftRecovery()” will try to run the function we are passing in and if it doesn’t pass, it will attempt run the function again. If the function fails a second time, we handle the error.

Sometimes you will have some functionality that is important to apply to many parts of your project, such as our hardware commands. In times like those, being able to pass a function into the function makes it possible to reuse a piece of code in ways that weren’t possible with Objective-C or an imperative language.

In the example in the chapter, we are trying to figure out a way around the creepy spaghetti code and conditional logic. Rather than creating one Frankenstein’s Monster of a function that has to account for all of these things, we are creating a lot of small functions that account for discreet parts of this problem.

We check if the point is within range of the circe. We check the offset. We write a function that combines known areas that are in range so that they are in one data set instead of two. Each of these functions returns a function of the same kind, so they can work together to narrow down more discreetly if a point is within range of the battleship without hitting a friendly craft.

This is a rather difficult example to explain and I will honestly say that at this point I can only explain it on a high level. I strongly recommend looking at the final code and referring back to each individual function to see how they work together. There are truly reusable functions in the final function that behave differently based on their inputs and parameters. Sometimes looking at a final piece of code and following the logic through its various rabbit holes is the best way to understand how something works. That’s why we dissected frogs in high school biology class, even though I swear some of my teachers were sadists….

Takeaways

This is an introductory chapter. It isn’t expected for you to genuinely grok all nuances of functional programming from this.

Here are the points I would take away from this chapter:

  • Start thinking about what you want your code to do rather than how you are going to implement it.
  • Start trying to mentally break things down into smaller pieces. Instead of having some big, scary function full of state and conditional logic, think about what you are trying to do in smaller steps.
  • Get used to the idea that functions can be passed around like variables. Think of functions as things that take one thing and turn it into something else. If you have a piece of wood, that can be turned into many different things. It can be a table, a desk, or a bookshelf. If you think of a function that takes a piece of wood and returns a piece of furniture and think about how each piece of furniture can be different, this might give you a better idea about how to conceptualize passing functions in Swift.
  • Don’t get discouraged!! This is a different way of thinking. It isn’t going to be intuitive. I have been working on this for nine months and I have barely gotten to the point where this chapter makes sense. That is one reason I am doing these posts. This is just to get your feet wet with some functional concepts. You are not expected to fully understand everything at this point in time. If you feel dumb right now, join the club. You’re not the only one. Just keep working at it and eventually something will click.

I wanted to recommend some resources I am using to try and work through this book to get these concepts to make more sense:

I will try to add to my list of resources as I encounter new ones. If people have sources they particularly like and want to make me aware of them, please ping me at @redqueencoder

Again, if a lot of this is still super confusing, that’s fine. You’re in the right place. We’re all trying to figure out how to please our new functional overlords. It will take a while and no blog post anyone is going to write is going to make this immediately intuitive. Think back to when you learned programming for the first time. It took a while, didn’t it? Learning how to think takes some time and some work. Don’t get down on yourself. Keep at it.

Up next, we are tackling some Core Image. Stay tuned!!

Doctor Who: Series One- Dalek

EXTERMINATE!!

EXTERMINATE!!

Ah yes, the inevitable Dalek episode. It was always coming. It would be like having a Star Trek series without Klingons. It just wasn’t going to happen.

It’s hard to remember a time when the Daleks hadn’t been run into the ground. After half a season of new villains, it’s kind of nice to see a familiar face. We are also treated to a few breadcrumbs about the Time War that destroyed the Time Lords.

The Dalek

I do have to admit, I was a little more thrilled at seeing the Dalek than I thought I would be. Even though the species has been retconned and run into the ground, the moment when the lights come on and the Dalek starts yelling “EXTERMINATE!!” is still incredibly exciting. The reveal was done incredibly well and this has helped this episode age pretty well.

It can be hard figuring out a good way to reintroduce an old character to audiences who may not be familiar with the source material. Seeing how genuinely terrified Eccleston looks after handily dealing with a bunch of crises over the last few episodes is one of the reasons this episode is still so effective.

I realized that nearly every Dalek episode from New Who follows the same theme of them finding a Dalek that seems to be different than the other Daleks, one that has feelings and is capable of growth. The companion tries to convince The Doctor that this one is a special, unique snowflake. The Doctor gives the Dalek a chance, only to find out he was right all along. It’s really repetitive. It’s like watching Lucy snatch away the football from Charlie Brown.

I haven’t watched any Old Who, so not certain if this repetitive theme was actually a new idea back in 2005.
DalekEccleston

The Doctor

Eccleston does a tremendous job of selling The Doctor as someone who can be genuinely menacing. He spends a lot of time in the series being cuddly and adventurous, but this episode really drives home the point that The Doctor is a powerful being who has killed people in the past. He is the sole survivor of a war that wiped out two races of people, one of which was his own. The first few episodes sell him as the lonely god, the last of his kind. This is the episode where you realize that he is the one responsible for him being the last of his kind. His confrontation with the other last member of a wiped-out species is incredibly tragic.

On one hand, you can point to him and say that he made choices. He pulled the trigger that wiped out two species and he survived. What does he have to complain about?

It’s incredibly difficult to make the hard choice and to live with its consequences.

The Doctor’s interaction with the Dalek force him to process and deal with something he’s been running from since “The End of the World.” He doesn’t want to think about how he is responsible for a mass genocide. He wants to heal and explore. He didn’t want to be put in the position of destroying his own people. He wants to forget. This theme is explored in much greater detail during the 50th anniversary special, but it’s bones are established here, in this episode.

Facing uncomfortable reality.

Facing uncomfortable reality.


The scenes watching him torture the helpless and ridiculous looking Dalek are kind of disturbing. We’re used to this idea that The Doctor saves people and does impossible things. Watching him sink to a level of torturing a helpless creature and enjoying it is terrible.

I am actually interested in how many times The Doctor does terrible things. I know he’s done many over the course of the new series, but every time we encounter one it’s always disturbing.

On some level we expect The Doctor to be better than us. He’s not a human. He has lived for a thousand years. We expect him to be above all of our petty, meaningless concerns, to be more than we are. Watching him behave like a beast who enjoys tormenting the Dalek is terrible, not just because the act itself is terrible, but because the person doing it should be beyond such behaviors.

Adam Mitchell

We’re going to get into him a little more in the next episode, just wanted to mention that we are going to see him again.

Henry Van Statten

It’s nice seeing what the British think about Americans, but all things considered, if the Daleks were real I could totally see some asshole one percenter being the person who collects extra terrestrial artifacts.
roseDalek

Conclusion

This was an important episode in that it brought an iconic villain back to the Whoverse. I guess I didn’t have as strong of a reaction as I thought I might all things considered. The episode was good, but its impact has been blunted over the years by seeing the same Dalek story over and over again. Supposedly this Dalek and The Doctor are the last of their kind, yet the Daleks keep coming back over and over again. Why is it that the Daleks can keep coming back over and over again, yet the Timelords are irretrievably lost in time and space and meaning? Hell, we found out a year ago that they are still out there and not a peep about them in the current season. Well, except for The Master, but let’s not get into his own problems right now…

Up next, we have “The Long Game,” in which we find out what happens when The Doctor picks up someone who isn’t really meant to be a companion.

Keep Calm and Solder On

I think it’s painfully obvious to anyone who comes here to read my blog that I have a lot of hobbies and interests. I consider myself something of a renaissance woman. Any time I see something cool or eat something interesting my brain starts ruminating about how I can make this thing myself.

I have slowly come to the realization that I will not be able to do all of the things that interest me before I die. I probably won’t be fluently bilingual, so sorry Julia, my friend in Austria who keeps wanting me to learn the Austrian dialect of the German. I am probably not going to get a black belt in some kind of martial art. Heck, at this point I am pleasantly surprised that I have taken up and enjoy running, considering that I spent the last year in bed with my pugs hiding from the cold and working from home.

My little electronics helper.

My little electronics helper.

One of my multitude of hobbies/interests is working with electronics. I have always been fascinated by electronics and one reason I really got into programming was because I was interested in telling a machine what to do. Electronics, like a lot of other things such as C, is something I have been told I don’t really need to understand to be a great programmer. Actually, I think the more accurate advice was I didn’t need to understand it to know enough programming to find a job. There is a difference.

I have discovered the longer I am working with computers that there is always a lower level than the one I am currently working at. I went into programming thinking C was the lowest you could go. Then I discovered assembly language (which no, I have not succumbed to yet). Now I am discovering electronics.

Electronics, like programming, has something of a learning curve. There are lots of little pieces and components like transistors and DIPs that can be rather daunting to the uninitiated. Additionally, at least with programming you are dealing with text. You can look at a computer program without knowing and programming language and at least recognize that this is something human readable. Look at a circuit board and all you see is the language that Superman’s people used on Krypton. To make things even harder, you can’t debug a circuit in the compiler like you can with a program. Your circuit either works or it doesn’t. These things are incredibly daunting, which is exactly why I am incredibly obsessed with this right now.

Yes, this looks scary. It is at first, but it gets better.

Yes, this looks scary. It is at first, but it gets better.

I am a masochist. If I get to a point where I feel proficient with something, I feel uncomfortable and uneasy. My brain starts overheating and spinning itself into butter, so I have to find something harder to throw at it to keep myself from having a panic attack. It’s hard having a balancing act of giving my brain enough to do that it doesn’t overwork itself over nothing as opposed to trying to do too much and cracking under the weight.

My current hobby to do in my free time right now is to work on learning electronics. I specifically would like to figure out how to build electronic instruments. I would like to design and build a modular synthesizer in the basement. Eventually I would like to apply the knowledge I have about that to a piece of software to eventually release, but right now I am trying to thread that needle between doing too much and not doing enough.

Do I need to understand electronics to be a great programmer? No. Will knowing electronics help me make more money in the future? Probably not. Could I spend my free time writing an app that could conceivably earn me more money and advance my career? Probably.

So why do this?

Here’s why:

  • Because it’s fun. I love doing things with my hands and I honestly find working on software to be a bit soulless sometimes. I feel like nothing I do is real because it exists in the ether. Holding a real, physical component and soldering them together is fulfilling in a way that software development just isn’t.
  • You know what all this crap does? Me neither. Let's find out.

    You know what all this crap does? Me neither. Let’s find out.

  • I work with robotics. I didn’t have to write the firmware or the interface software to deal with the robotics initially, but it bothers me that I don’t quite grok how we are talking to the robotics. I know enough about the code to deal with it, but on a conceptual level there is still a lot of magic black box voodoo that is happening that I don’t fully understand. I don’t like working with things that work without me understanding what they do. I don’t “need” to understand this stuff to do my job, but I would personally feel more comfortable if I worked through a robotics project on my own and have a mental model for what it is that we are doing.
  • I don’t like looking at a circuit board and not understanding how it works. I think a lot of people feel very uneasy around things like programming because they see something that isn’t familiar to them and it frightens them away. I know there has to be a way of understanding how a circuit board works because other people design and build them for a living. Our entire society is built on integrated circuits and I want to know how they work and how to design them.
  • My analog synth kit that I am saving until I feel more comfortable tackling something this complex.

    My analog synth kit that I am saving until I feel more comfortable tackling something this complex.

  • It’s never been easier to self teach yourself electronics. Arduino and Raspberry Pi have made working with electronics and programming affordable and accessible to nearly everyone. In the last three or four years the MAKE series from O’Reilly has produced books on wearable electronics, analog synthesizers, and sensors. Sites like AdaFruit make it possible to buy projects designed by other electronics hobbyists. I have received an analog synthesizer kit that I am looking forward to assembling in my basement. I bought conductive thread and smart LEDs so that I can create a wearable electronics project. I don’t have kids and I have disposable time and money. Instead of spending a day playing Nintendogs on my Nintendo DS, I can go to my basement and build a robot that I can spend another weekend hacking.

If you are interested in electronics, here is what I have done:

  • Go to O’Reilly and pick up books like this one or this one. O’Reilly has great deals on ebooks if you buy two or more of them. Many of the books in the MAKE series have correlating kits on Maker Shed, which are also somewhat available at Radio Shack, if you like brick and mortar stores. Just doing a kit is not going to teach you electronics, but it will get you comfortable with soldering and putting pieces together. The physical aspect of feeling comfortable touching and manipulating small parts is vitally important and I have personally found when I am trying to learn new things that just following directions and putting something together is a good first step to introduce you to something unfamiliar.
  • You're going to snip and solder a lot of these little bastards. Get used to it.

    You’re going to snip and solder a lot of these little bastards. Get used to it.

  • Invest in a good soldering iron. In fact, don’t stop at the soldering iron. Any time you buy tools, invest in good tools. You are going to be snipping a lot of wires and fighting with your snips when you are trying to make a nice, even back is no fun. It also bothers your wrists and for people coming from a programming background, that is never a good thing.
  • Practice soldering! I have done cross stitch for over two decades, so I personally found soldering to be somewhat easy to pick up. I am used to patiently sorting through threads and inserting thin filaments through holes. Let there be no ambiguity on this point: Working with your hands is a skill. You can cultivate a skill by using it a lot. Just because you may not have a preexisting aptitude for manipulating small pieces does not mean that you should give up on doing electronics. I suggest buying a bunch of small soldering kits from Maker Shed and that you just get comfortable with touching and soldering pieces. My first project was a noise maker that goes in an Altoid tin. There weren’t a lot of pieces and it wasn’t super complicated, but just the process of sorting the components and touching the pieces and trying to figure out how they worked together was an invaluable introduction to electronics that made me eager to learn more and to bigger projects.
  • Organize your parts!!

    Organize your parts!!

  • Organize your work space. This is the single most important piece of advice I can give to anyone undertaking a hobby with lots of small physical bits. Again, I have a background doing cross stitch. I never got a project done until I started learning to organize myself. I separate each color thread, put them on an index card, and I catalog what each color is. I make a slot for each color along with its symbol before I need them. I got relentlessly organized with my threads and my tools and once I did that I was able to get things done. Keep your space clean and organized. Take care of your tools. Buy as many parts bins as you need and label each and every drawer. If you can, have a dedicated space for your electronics. I have a table in the basement that has my soldering iron and my tools. The projects are all in one place and everything is tidy.
  • Be patient with yourself. You might have a natural aptitude for this or you might not. I have found a lot of things I thought I had no natural aptitude for that I was able to learn through tenacity and stubbornness. If you want to learn something because you want to learn it, then don’t worry about how long it takes. If you can accomplish something you thought was difficult after failing several times, just think about how awesome you will feel.

One of my projects was putting together an Arduino shield with a bunch of attachments to things like motors and sensors. I need to spend some time figuring out how to get the Arduino to talk to the board and to make it do things. I got past my initial skittishness around touching and putting together the components, now I need to take the next step of figuring out how to make them work together.

Again, this is a hobby I picked up a few months ago. I would like to spend a lot of time over the next year pushing the boundaries of what I can do with this. I will try to be better about writing up each of my projects on this blog in case anyone finds this interesting.

My "completed" Arduino shield I need to learn how to hack.

My “completed” Arduino shield I need to learn how to hack.


One reason I decided to do this was because someone told me I had to have an electrical engineering degree to understand how a circuit board works. I don’t believe that. I believe I can learn anything I want to. I think there are a multitude of sources available out there for people with the time and patience to work with them and who have a little bit of money to invest in their hobbies.

I also think there is value in learning how to do something just to do it. I spent a lot of time learning how to cross stitch because I wanted to know how to do it. So far I have not earned a dime from my textile work, but the things I did to discipline myself to learn it have come in handy down the road while learning programming.

I love the smell of solder in the morning. It smells like lead poisoning!

I love the smell of solder in the morning. It smells like lead poisoning!

Let’s pretend that you want to master making pie. Pie crust is hard to master. You need to develop a good feel for when the dough is too dry or it has been over mixed. My mom has spent forty years mastering how to make pie and she is still experimenting with different things. Mom doesn’t sell her pies and will never make money off of them, but the process of making them and the joy they bring people make it worth it to her to keep trying to make them better.

I think that there is a lot of pressure to spend all of our time doing things that will make money or bolster our careers. I think its important to spend some time cultivating those things, but I am also very Zen in that I think people should do things for no reason other than that it makes them happy. You can’t spend all of your time worrying about whether you are learning the right things. Sometimes you have to follow a passion and do something that makes you happy and not worry about what the future will bring.

Functional Programming in Swift: A Chapter by Chapter Analysis

I preordered the book ”Functional Programming in Swift” shortly after it was announced. I got a chance to meet Chris Eidhof at 360|iDev, which was a really awesome experience. He really knows his stuff and he is a wonderful person.

However, I have found it has been a little difficult to get into the Functional Swift book. I know there is a lot of really important content in here that I think is vitally important for people to be aware of, but I feel like it is being presented in a slightly obscure manner. This is not a beginning programming book and it can be a little difficult to break into.

I am trying to write more technical posts on my blog. I wasn’t doing this for a while because I was working on my book ”iOS 8 SDK Development.” I was so stressed trying to learn all the stuff we were writing for the book that the thought of going and writing technical posts for my blog was a little discouraging.

Now that the book is pretty much completed and I don’t have any conference talks to prepare for a while, I would like to spend more time writing technical blog posts. One series of posts I would like to do is analyzing the Functional Programming in Swift book.

I was a beginner not too long ago and I know how incredibly daunting it can be to be confronted with a bunch of technical stuff you don’t quite grok yet. I know that Chris is passionate about functional programming and I hope that he will not be offended by my efforts to spread the word about his work and to try to sugar coat it a little for people who are not as far along as the target audience for his book was.

I am going to try to tackle a chapter every week or two. If I am slacking on this, please harass me on Twitter so that I know that people are actually reading and getting some use out of my posts.

If people have things they would like me to write about, please hit me up on Twitter about it. I am happy to go out and figure something out that people want to learn more about.

I hope that doing this analysis will open new people up to things in Swift that weren’t possible in Objective-C. I know that right now it is very hip to hate on Swift. I was rather unhappy about Swift when it was first announced because I was under the impression that many things we do were going to be abstracted away. I didn’t like Ruby on Rails because you could write three lines of code and have a functioning website without understanding anything going on under the hood. I am an incredibly curious person who wants to have a deep understanding of how things work. When I was able to shift my perspective on Swift I became very excited about learning a new way of doing things. I hope that other people will do the same when they become familiar with how to effectively write Swift.

If you want to know what my current long-term project is, check out this blog post from my boss, Brad Larson. So far, we have not found anything we have wanted to do that could not be done more efficiently in Swift. We have had to use a few work arounds with the compiler, but we have not encountered any show stoppers.

I have been disappointed with all of the people who are claiming that Swift is broken and that it shouldn’t be used for serious projects. If Brad and I can use Swift on the Mac to communicate with an electronics board through a serial port, you can figure out how to use Swift to communicate with an API. It is just a question of working with the language, not fighting against it.

I hope over the next few months I can help others learn how to work with Swift. I think that is is a promising language and I hope that people can learn to open their minds to new ways of thinking about how to program.