Random success story – black triangle!


Let’s say you’re plugging away at your Mac following a tutorial on getting started with Kinect 3D programming and you’re getting no where. No matter what you try you cannot get your screen to look like any of the examples as your Mac throws error after error. Hi, I’m Cliff. You’re here because you’re having a fight with your computer. You want it to draw fancy 3D imagery of your living room but it wants to remind you that it does not SEE the $99 piece of equipment that you overpaid for in 2013 just prior to the release of the newer fancier pricier identically functioning piece of equipment. I’m here because I just had a “black triangle” moment.

What does a triangle have to do with Kinect?
Anyone who has done anything with 3D programming has had at least one, what we call, “black triangle” moment. These are milestone moments where you accomplish the next big thing after spending at least an hour perusing random documentation. The term “black triangle” (coined by someone I can’t recall, my Google-fu is weak today…) comes from the typical “hello world” example you will find when learning 3D programming. Usually it involves a mild math and/or geometry lesson, along with a gentle introduction to a slew of new APIs and function calls that make little sense and it is finally climaxed by a black triangle painted on the screen of the device you are running your program on. In other words you do a whole lot of learning and work to get something really simple to show on the screen.

While the program seems trivial the door that opens as a result is huge. This is why software engineers tend to get excited over things that the average person finds ridiculous. I once tried to show my wife a needle bouncing from left to right on an analog audio app I was playing with to measure sound impulses. I digress.

My black triangle does not look triangular!
My black triangle does not look triangular!

I summoned you here today because I finally tackled an issue that consumed several weeks of my life back in 2013. I had trouble getting the Kinect model 1473 to work on OS X. This is because the OpenNI driver didn’t seem to agree with this slightly newer model of Kinect sensor. If you are playing with the Kinect you may have run across the issue as well. There are several models of Kinect sensors, the original one bundled with the Xbox 360 is the easiest to use. However the model released a few months later as a separately purchasable item (model 1473) changes somethings internally in hardware where the popular OpenNI driver can no longer detect. Then there is the Kinect sensor for the Xbox ONE, which I have yet to see/use.

My black triangle does not actually look triangular, but it’s just as exciting. I was able to get things working in just over an hour. I started by looking over all of the original tools I used in 2013 realizing none of them had changed much or received additional updates over the years. Then I finally took a leap of faith and arrived at the solution.

Getting Kinect model 1473 to work on OS X
The process is remarkably simple considering all the research I did years ago! I installed Homebrew then installed libfreenect. This entire process took about 3-5 minutes and uses only two commands.Installing Homebrew is one curl command, and installing the driver is one Homebrew command. The final step is to install the Shiffman wrapper for Processing. (You also need to install Processing itself if you haven’t done that.) This is as simple as going to “Sketch -> Import Library -> Add Library” from the menu and looking for “Open Kinect for Processing” After that I was ALMOST ready to start writing Kinect code again. There is one major issue, however.

The driver can only access the Kinect when run in a root process. I’ve logged an issue with the Open Kinect Github repo to ask about it, though the issue probably belongs on the openkinect.org site since that’s where the install instructions for OS X are lacking. (They explain how to use the Kinect as a normal user on Linux but not on OS X.) To work around the problem I had to launch Processing as a root user. I type “su” on the command line to switch to the root user account. Using Finder I right-click on show package contents and drill into the “Contents/MacOS” folder. I drag/drop the “Processing” executable from there into my terminal window and press enter in my terminal to run it. (Or you can just type the path in manually, “/Applications/Processing.app/Contents/MacOS/Processing”.) The first time you do this you’ll get a pop error with Processing complaining it can’t find the sketchbook folder. I had to make a “Documents” folder in the root user’s $HOME directory then I added a sym-link there to the “Processing” folder in my $HOME/Documents directory. only after all of that was I able to run Processing as root and get my Hello Kinect app to run. I also had to enable the root account in OS X to make it easy to add the missing sketchbook folder.

Finally, after all of the hassle I have success. In reality it should have been a simple 2 downloads, 2 installs, copy/paste a sketch and go. However things are never as straight-forward as they should be when you live in the world of computers.

Install software to make software that makes software


I’m watching my terminal as Homebrew installs during my latest episode of yak-shaving. I feel a strange urge to explore the Homebrew site, a step that has nothing to do with the reason why I’m here. Hi, I’m Cliff. You’re here as the result of an unfortunate chain of events. You probably started to wash laundry when you realized you were out of detergent. On your way out the door to get detergent you noticed the handle was slightly loose and decided it needed to be tightened. Somewhere between looking for the screwdriver to fix the door something prompted you to check your email, before long you found yourself on Facebook where my post caught your eye. That’s one possible reason why you’re here. I’m here because some four footed hoofed creature with too much hair demanded my attention.

Hairy four legged beast demanding attention
Hairy four legged beast demanding attention

Extreme Yak Shaving
Have you ever found yourself downloading an OpenGL & SDL framework while watching a tutorial when what you really needed to do was install a missing JDBC driver? Today I found myself in a similar situation where I merely wanted to change a dependency in my Android project. That got me thinking about dependency management on iOS, and hey I just learned about this cool project called CocoaPods. To install Cocoa Pods I need Ruby. I can’t install Ruby the old school way, I’m downloading RVM! RVM’s install directions use the gpg command which isn’t available on Mac OS X. I need Mac Ports or Homebrew to install the Linux package. NO!!! I refuse to get side tracked with Homebrew! I’ll skip the gpg step and just use the curl command to direct install without checking security signatures!! The RVM install completes and while I install Ruby I notice the shell is going out to Homebrew land to automate the install. Gosh, I haven’t looked into Homebrew in over a year… I should check out their webpage to see what’s new. It’s always good to get the latest scoop on long living tech like Homebrew. Yak shaving is the work of doing meta work in order to do actual work. Extreme yak shaving is the distraction that results from work of doing the meta work.

I am installing software (Homebrew) to install software (ruby) to install software (Cocoa Pods) which I’ll use to install my iOS software dependencies. I’m not even getting into the learning curve associated with this major distraction. Am I losing it? (Don’t act like it’s just me! Some of you are waiting on a Unity or gaming engine download right now because you ran out of toilet paper!) How do we get some many layers of abstraction between us and the work we actually intend to do? I’m thinking eventually things will swing in the other direction as they always do. Instead of using systems to manage the install of managers that install frameworks we use to build systems (which ultimately manage the install of managers that install…) we will obsess over slim code and vanilla iOS/Java. Languages like Go are already becoming mainstream I’m waiting for the idea to spill into mobile which has currently gone the way of EJB. Until then I’ll keep watching my progress bar fill while the next software installing software installs.

Learn 2 Code TDD Style


So it’s a Wednesday night & I won’t be able to sleep unless I get this post out. Overtime I get to talking about Agile and TDD I go overboard. Hi, I’m Cliff. You’re here because you wanna know what this TDD style learning thing is all about. I’m here because I shot off my mouth over Twitter ’bout some stuff I had no business talking about. Now I gotta make something out of my tall order claims. Here I will attempt to stretch the limits of a simple programming construct as well as the limits of your imagination in my explanation.

The approach I’m going to explain is something I learned from Dierk König in the Groovy In Action book, I take no credit for the awesome technique and I’m not sure of its origins. The technique is language agnostic, will out live your written wiki documentation and stays up to date as the code evolves. So now that I got your attention, let’s get started. (Disclaimer, though it is language agnostic I will be explaining using Java syntax. Different languages/platforms may behave differently.)

What’s an assert?

This question came up in the Twitter chat so we’ll start here. (BTW, I’m really starting to dig the #CodeNewbie chat because they ask the cutest questions like, “what’s an assert?”) The basic function of an “assert” is to check a value or an expression and its form (again in Java) is:

assert (expression) : "error message when false";

The expression can be any boolean expression and the message is whatever you want to see when the expression fails. You can have something like

assert 2+3 == 1024 : "I'm not good math";

Then when you run this code it would explode your computer sending shards of silicon in random directions. Actually it would merely fail the process and dump the error message and expression to the console:

java.lang.AssertionError: I'm not good math. Expression: ((2 + 3) == 1024)

Next you could try something like

x = 3;
assert 2+3 == 1024 : "I'm not good math";

which would dump the following error to the console:

java.lang.AssertionError: I'm not good math. Expression: ((2 + x) == 1024). Values: x = 3

The interesting thing here is the way the values are dumped to the console. I’ll explain why this is so significant in a moment but for now let’s move on. You could have something more involved like this (assuming you have a guessMyAge method that lives somewhere):

result = guessMyAge();
assert result == 39 : "I think I'm 39 years old last I checked."

If the guessMyAge function can work magic then the code would run without failing. However, if magic really doesn’t exist in my Macbook pro and it really couldn’t guessMyAge then I might see something such as:

java.lang.AssertionError: I think I'm 39 years old last I checked.. Expression: (result == 39). Values: result = 109

I would then say, “Nice try Macbook. I get to pick a prize now, right?” All dry jokes aside, you might notice a pattern here. To make it clear I’ll give another example. Pay close attention because I’m going to try to confuse you.

Let’s say you had a method named “fib” defined somewhere and I gave you the task of designing an app with this function. You might panic and be all, WTH??? What is “fib”??? Or you might try something like:

assert fib("I have bad breath") == true : "I really should not have bad breath";

You would do this because you would be asserting the sentence string you give to the fib method is a “fib” or a false statement. Furthermore you might be guessing that the fib method does some sort of lie detection so you would assume passing a fib to a fib detector would return true indicating that you do NOT actually have bad breath. Again, unless your computer has magic inside you would probably get a compile error saying that the fib method takes an int parameter and returns an int value. (This is the confusing part where my example an my dry humor jump the shark.)

Sloppy humor aside. Let’s take the fib example again. This time we know that it takes an int parameter and returns an int value. We could do something like so:

int x = fib(1);
assert x == 0 : "I really don't know what this does";

Here we are setting the value of x to the return of the “fib” method without really knowing what “fib” is or what it does. We use the value in an assert and when we run it we get:

java.lang.AssertionError: I really don't know what this does. Expression: (x == 0). Values: x = 1

If you look carefully you have a valuable piece of information, “Values: x = 1”. You Didn’t know this before. We can copy and paste the value back into the assert and change the error message:

int x = fib(1);
assert x == 1 : "fib(1) should be 1";

That’s not so interesting. but watch what happens when repeat the code with a few different parameters.

x = fib(2);
assert x == 1 : "fib(2) should be 1";

x = fib(3);
assert x == 1 : "fib(3) should be 1";

x = fib(4);
assert x == 1 : "fib(4) should be 1";

We are changing the passed parameter in each case but keeping the assertion the same. After a little experience coding you might notice that usually when a different parameter is passed to a method you get a different value returned, so what gives? Think a moment. If you run this code and repeatedly copy the errors from the console back into the assert you would end up with this:

int x = fib(1);
assert x == 1 : "fib(1) should be 1";

x = fib(2);
assert x == 2 : "fib(2) should be 2";

x = fib(3);
assert x == 3 : "fib(3) should be 3";

x = fib(4);
assert x == 5 : "fib(4) should be 5";

Everything looks sort of predictable until the last line where the number jumps to 5. A few more iterations give you:

int x = fib(1);
assert x == 1 : "fib(1) should be 1";

x = fib(2);
assert x == 2 : "fib(2) should be 2";

x = fib(3);
assert x == 3 : "fib(3) should be 3";

x = fib(4);
assert x == 5 : "fib(4) should be 5";

x = fib(5);
assert x == 8 : "fib(4) should be 8";

x = fib(6);
assert x == 13 : "fib(4) should be 13";

x = fib(7);
assert x == 21 : "fib(4) should be 21";

If you have a background in computer science or have spent a lot of time programming in your career you might recognize the series of numbers emerging as the fibonacci sequence. What you have done is explore a method without knowing if was a lie-detector or a numeric series generator and through a process of iterating with different parameters and asserts you discovered its behavior. Furthermore you have documentation in the form of sentences describing the method. You could come back weeks or months later and see what the method does without looking at any of the code inside the method.

This approach works particularly well with methods in Java but how does that help you learn anything? Well, because the assert is so incredibly simple you can use it anywhere and with any programming construct. For example what does the following left shift operation do?

int x = 32 << 3;
assert x==0 : "What does this do?";

java.lang.AssertionError: What does this do?. Expression: (x == 0). Values: x = 256

Becomes:

int x = 32 << 3;
assert x==256 : "Left shift 32 by 3 should result in 256";

But what does that that actually do? A Java programming manual would say something like binary bit shifting. You can lookup the Javadoc for the Integer class to see how numbers print in binary form. Then you can use an assert to make the entire picture clear:

assert "100000".equals(Integer.toBinaryString(32)) : "32 in binary form " + Integer.toBinaryString(32)
assert "100000000".equals(Integer.toBinaryString(32 << 3)) : "32 left shifted by 3 in binary form " + Integer.toBinaryString(32 << 3)

Clearly a left shift of 3 shows the “1” bit of the binary number 32 shifted over to the left by three positions. (It would be a little more clear if we added zero-padding but I’m keeping my examples simple.) The best part is that you get documentation as a secondary benefit of using the assert. You can print this next example and post it on your wall as a reference when learning how to bit shift. This is runnable documentation of how bit shift operators work:

assert "100".equals(Integer.toBinaryString(32 >> 3)) : "32 right shifted by 3 in binary form " + Integer.toBinaryString(32 >> 3)
assert "100000".equals(Integer.toBinaryString(32)) : "32 in binary form " + Integer.toBinaryString(32)
assert "100000000".equals(Integer.toBinaryString(32 << 3)) : "32 left shifted by 3 in binary form " + Integer.toBinaryString(32 << 3)

Living documentation
I have another disclaimer. The above examples were all snippets that I actually hacked together using Groovy so they may behave slightly different in an actual Java program but the principle still applies. The idea was to focus on the assert and how it works. I used Groovy to avoid the boiler plate surrounding class definition. Now let’s look at an actual Java program to see how far we can stretch the secondary benefit. Suppose we were given a Java jar file that included the earlier “fib” method and told to use it. We could define a static block in our program and code all of the asserts inside of it as follows:

import static com.company.Funny.fib;

public class MyMain extends Object {

static {
int x = fib(1);
assert x == 1 : "fib(1) should be 1";

x = fib(2);
assert x == 2 : "fib(2) should be 2";

x = fib(3);
assert x == 3 : "fib(3) should be 3";

x = fib(4);
assert x == 5 : "fib(4) should be 5";

x = fib(5);
assert x == 8 : "fib(4) should be 8";

x = fib(6);
assert x == 13 : "fib(4) should be 13";

x = fib(7);
assert x == 21 : "fib(4) should be 21";

}

public static void main(String[] args) {
int num = Integer.parseInt(args[0]);
System.out.println("The fibonacci of " + num + " is " + fib(num));
}
}

The static block documents what we discovered with asserts and runs as soon as the class file loads for the first time. As a result it doubles (triples?) as a sanity check. The assert has performed at least three tasks. We used it to learn the behavior of the method, it now documents what we know, and finally it works as a sanity check as the program runs. What if you get an updated copy of the jar file that contains the “fib” method and the behavior of “fib” changes? The asserts run with your code and dump the current behavior to the console. You merely repeat the process you used initially to discover the behavior and update the asserts accordingly.

How we learn programming languages
When learning a programming language you have two unknowns, syntax and runtime behavior. Learning the syntax is relatively trivial in many cases and usually the smaller part of the learning curve. Syntax is usually validated when you compile your program, and if you iterate adding small additions line by line as we did above then the compiler can almost function the same as the assert.

I find that the runtime behavior is far more tricky to learn and requires the experience of actually running variations of code. The bulk of the runtime behavior involves calling methods/functions in the language. Most languages have a set of functions and classes you must learn to achieve basic functions such as string concatenation, rounding, averaging, manipulating streams, etc. These functions make up the core of the language. In Java you have the Java API. Ruby has the Ruby API, ObjC/iOS uses the CocoaTouch API, and so forth. behave slightly different than what you would expect when you are first learning. Using the assert, you can discover the subtleties of how things behave and the end result is documentation that you can print and hang on your Christmas tree.

This post was intended to explain the power of assert. I actually stretched my examples a bit in a crude attempt to illustrate how assert can be used as a learning tool for beginners. In actually what I was really doing was a light weight no frills form of TDD. While you could start out with assert, In practice you really wouldn’t use a vanilla assert in a program this way. (at least not in Java) There a number of reasons, the first being that assert is disabled in Java and you must enable it by passing a flag on the command line. If you continue with a vanilla assert you will soon see other areas where it breaks down.

My overall intention is to introduce this new way of thinking. I use the assert as a gateway drug to unit test tools with more powerful assert abilities. It is indeed as simple and powerful as I depict in this post and it does have its place, specifically among other languages like Groovy where it is not disabled. (I particularly like the way Dierk used it in his book in lieu of traditional System.out.println statements.) If you find the approach at least somewhat interesting check back later when I attempt to use a more thorough approach to learning with TDD.

Last disclaimer: It is extremely late at night and I made only a feeble attempt at proof-reading/formatting. This is because I get super excited about the topic. There are definitely errors, misspellings/etc. so forgive me.

Learn how to code via Assert


I just got finished hanging out on the #CodeNewbie Twitter chat where I was blabbing about TDD this and assert that. I’m putting this micro post up as a place-holder for a topic I plan to explain in detail, using unit tests to explore and learn code. Hi, I’m Cliff. You’re here because you wanna know more about TDD and asserts. I’m here because I spent way too many keystrokes talking about it on the Twitter chat.

The simplest form of unit test is the “assert” which is available in most languages. The idea behind the assert is to check a value or condition. The magic behind the assert is that it echoes the condition or expression back to you. When used correctly it can be a powerful learning tool which can double as documentation.

Where does Xcode download and install its simulators?


I need to fire off a quick tip before I go to bed tonight. I was stuck on a bad build of an XCode Simulator and I wanted to reinstall it. (Sometimes the downloaded sim is corrupted. This is common in the beta/pre-release builds of XCode.) I had downloaded and installed both 8.4 and 8.1 simulators. Hi, I’m Cliff. You’re here reading this because you suck at uninstalling Simulators in Xcode.

I had the worst luck trying to remove what I’d downloaded and installed. First I tried drilling into the Xcode.app folder to find and remove the downloaded simulator here:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/PhoneSimulator8.1sdk
However, after restarting XCode it still “thought” the simulators were downloaded. I even tried deleting the cache folder here:
~/Library/Caches/com.apple.dt.Xcode
But I had the same results after restarting. The actual path to the downloaded and installed Simulators (since version 6.x) is:
/Library/Developer/CoreSimulator/Profiles/Runtimes

New Chapter


Today I open a new chapter in my career and in my life. I’ve long been a gadget guy or, as my buddy JC calls me, a “gear-head”. Today I’m starting something new. Hi, I’m Cliff. You’re here because you’re looking for me to give you another ridiculous explanation of why you are here. I’m here because of a number of choices made over the years which have lead to this huge transition.

This site is all about tech. Over the years I intentionally refrained from posting about anything other than technology on this site. Politics, current events, news, religion were all considered irrelevant. I am, however, a devoted Christian with a passion to share my experiences with others. I love sharing what I’ve been through and it’s the sole reason this site exists today. Still, I’ve always felt a strong desire to keep this blog focused exclusively on tech a if the slightest mention of anything else would be contamination. I’ve never had more than a handful of hits on my stats counter with my Auntie Sharon being one of my most devoted readers. As a result I don’t feel there Earth would move too dramatically if I make the change I am planning to make.

I’ll keep this brief. By now you probably know where I’m going with the terms, “transition”, and “new chapter”. If not let me be frank. (Actually let me continue to be Cliff, because I don’t know who Frank is or why so many people want to be him.) I will be sharing stories of my journey as I slowly begin to give myself to Jesus. I am taking my inspiration from another friend who has also been blogging about God. I will tag the posts accordingly so those who are purely interested in my walk can find them.

The reason I consider this a new chapter is because I’ve been feeling a strong desire or instinct to follow a path other than technology. T.D. Jakes explains, in his sermons “Instinct to Increase”, that we often have hidden passions or desires that don’t fit our current role. In many ways I see these budding passions of mine similar to what the Bishop explains. It is uncomfortable to follow these instincts but necessary. I am in no way planning on converting this blog to a Christian blog nor will I refrain from posting the latest tips/tricks on things like how to get your web views on Android to sing to one another. However, I will begin following my new instincts as I sprinkle inspirational stories and messages here and there. I thank all three of my devoted visitors in advance for their continued support.

How to suck at programming #441


You’re outside on a sunny day tethered to your mobile phone hotspot and limited data plan writing code using an editor that isn’t your main tool. Maybe you’re an Eclipse die-hard or maybe you live on Xcode for your day job. Whatever the case, you find yourself in something like a SublimeText, Notepad++, or TextMate for something you are prototyping. You’re mostly familiar with the hotkeys but again this is not your day job editor so you’re prone to get one or two of the sequences from your main tool mixed in on occasion. You go to quickly comment out a section of code when muscle memory kicks in and the next keystrokes invoke Ruby integration. Hi, I’m Cliff. You’re here because your Macbook is currently downloading an unknown Ruby package (or packages) over a limited mobile carrier connection and you won’t know how much is left in your monthly allotment until you finish reading this article. I’m here to share a similar experience that just happened moments prior to my writing.

Welcome to my How to suck at programming series. There’s no particular reason why I’d been listing these tips in random order, however feel free to peruse what I’d posted in the past if you really enjoy the merits of being an awful programmer. I write todays post with hesitation because I’ve been going through a low point in my career and I have been doubting my abilities. Still, this is a must-post topic and I enjoy making others feel enlightened on how to be dumber than usual.

So what happens when the stars align in a perfect arrangement capable of ruining your day/week/month? What do you do when you’ve accidentally invoked an unknown feature in a tool that you only use on occasion? What do you do when that tool spawns an unknown external process designed to gulp down potential gigabytes of data over a limited mobile data connection? Why do we even have limits on our mobile data connections??? (Curse you AT&T!!! I condemn you and your penalties per excess gigabyte!!!) Don’t they have something way better overseas?

My story actually begins in a hospital building somewhere in Mt. Holly N.J. where I shed my first tears but I’ll spare you the extra drama including the part where I finally realized I was too old to still try to bring toys to school (6th grade) and give you the abbreviated version… that is, if you’re still interested.

So there I was stuck in an unanticipated initialization sequence spawned from TextMate. “Initializing Ruby 1.8” is what the pop up window said. It was scary. Mostly because I wasn’t sure what “initializing Ruby 1.8” meant, but partly because there wasn’t a functional close/cancel button on the popup window. I know most Ruby programs usually use Ruby Gems. I know Ruby gems can be a separate download/install and I know that Ruby Gems can potentially download other Gems. I didn’t know if any of this would be part of the initialization.

What was this mean initialization dialog doing to my Mac? What was it doing to my data cap??? What was I doing downloading Ruby??? In an extreme exercise of yak-shaving, I set out to teach my daughter some more Javascript stuff. The lesson ended with my trying to recode Khan academy’s interactive design page in roughly 20 lines of vanilla Javascript because, you know, I thought it’d be cool to show her how things worked on the page where she learned to draw a turkey dinner with the line, ellipse, and image functions.

My reasoning went something like, “I’ll teach her this cool trick and ignore the fact that there is multiple layers of indirection involved. Afterwards she’ll realize how cool the code and her Dad is then we’ll finish the project this week and sell the product to eBay for $500m!” This is because everyone who is anyone knows to sell a Javascript side-project to companies like eBay, it’s just common sense. At any rate, I’m failing at recreating Khan academy, failing as a super programmer, and failing as a father when I realize my daughter has lost all interest in the twist of logic I tried to confidently demonstrate. I also realize she has to leave for a sleepover. Undeterred in my efforts as a script ninja I took my operation to the Cupertino Library where I could concentrate sans distractions.

I finally succeed at hacking together something horrible looking enough to pass as a Frankenstein Khorn-Alch-tradgedy. Proud of my tardy solution, I test it a few times while mumbling how simple the source code behind the more professional Javascript editor should actually be. Who needs You-Tube interactive voice coaching anywayz? This is when I decide to simplify my logic and comment out a few lines of code triggering the unknown consequences of a Ruby initialization.
So if you want to suck at programming, teach your child Javascript. Then get sidetracked on a complicated unrelated demonstration of irrelevance while persisting until you can actually smell your child’s lack of confidence and trust in the air. Ignore all signs of boredom from the little one and continue until they roll credits between you and your keyboard.