The Hiltmon

On walkabout in life and technology

Lose the WWW

I have finally removed the www from hiltmon.com. The 1990’s have passed and www no longer makes sense in URL’s. Maybe it’s time you did the same to your site too.

What triggered this change was that, as of 14 October 2014, Google Analytics started throwing an error if you had links to www.yoursite.com and yoursite.com. This is because it treated links with www and without as different.

Removing the www was easy. I used Global Search and Replace (⇧⌘F in TextMate 2) to find //www.hiltmon and replace it with //hiltmon. Then regenerated the site.

But we are still faced with the problem that remote links to the site may still include the www in their URL’s. The solution is a 301 redirect. Setting that up depends on your hosting provider. For Dreamhost1, it required clicking one radio button:

Welcome to hiltmon.com.

Follow the author as @hiltmon on Twitter.


  1. Warning: Affiliate link.

Simple C++ Testing With Catch in Xcode

Catch is a simple, open-source, dependency-free unit testing framework for C++ projects. In this post I show you how to use it in a Simple C++ Project from Xcode.

Why Catch?

I am writing fast C++ libraries for work and need to wrap them in unit tests to ensure that they continue to operate and perform as they evolve. My number one constraint is that these libraries must be dependency-free. The only tools that I may use to compile or install these libraries on any platform (in my case OS X and Linux) is a C++11 compiler and a C++ standard library. Nothing else.

This dependency-free requirement precludes the use of excellent popular testing frameworks such as Boost Test (a dependency), cppunit (not up to C++11 yet), google-test (uses a dylib or library depending on the platform) or XCTest (Xcode only and not C++).

Catch is a single header only, dependency-free, lightweight testing framework that’s perfect for my needs. It’s simple to add to a project, simple to create tests cases and scenarios and simple to run. It also allows the user to run only a subset of tests using tags, which is really cool.

And that’s why I chose it.

Install Catch

Just include catch.hpp in a target and you’re halfway there.

The catch.hpp file can be downloaded from http://builds.catch-lib.net/. You can find the full source at https://github.com/philsquared/Catch.

Sample Tests

Catch is well documented. I recommend you follow the tutorial for simple functional unit tests and how to use the product. Since there is no point in me repeating how to write simple tests, go ahead and read through the tutorial, and I’ll wait until you return.

PLAYS ON-HOLD MUSAK

Ok, back? Good. You have seen how simple it is to spin up a TEST_CASE, break it down into several SECTION elements and test results using CHECK and REQUIRE.

Lets do something more complex, lets test a sample class. This sample project is what I used to test Catch when I first saw it and helped me choose it.

First, the tests:

test_sample_class.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "catch.hpp"

#include "SampleClass.h"

TEST_CASE("Testing Sample Class") {
  SampleClass sc;

  SECTION("setting the str") {
    INFO("Using TestStr") // Only appears on a FAIL
    sc.setStr("TestStr");
    CAPTURE(sc.getStr()); // Displays this variable on a FAIL

    CHECK(sc.getStr() == "TestStr");
  }

  SECTION("test bigStr") {
    sc.setStr("TestStr");
    REQUIRE(sc.bigStr() == "TestStr : 7");
  }

  SECTION("Test doubles") {
    sc.setD(1);
    CHECK(sc.getD() == 1);
    sc.setD(1.0/3.0);
    CHECK(sc.getD() == Approx(0.33333)); // Nice
  }
}

In this example, we have a single TEST_CASE and three sections. Inside the TEST_CASE we instantiate the object and each SECTION tests a function in the sample class. Note that in the example, I arbitrarily switch between CHECK (no stop test) and REQUIRE (stop test), whereas in a real testing environment, I use them appropriately.

Here’s the sample class that we’re testing’s header, it really is quite a useless class but gives us a range of things to try:

sample_class.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>

class SampleClass {

public:
  std::string getStr() { return _str; };
  void setStr(const std::string& s) { _str = s; };

  double getD() { return _d; };
  void setD(double x) { _d = x; };

  std::string bigStr();

private:
  std::string _str;
  double _d;
};

And its body:

sample_class.cpp
1
2
3
4
5
6
7
8
9
10
#include "SampleClass.h"

#include <sstream>

std::string SampleClass::bigStr()
{
  std::stringstream ss;
  ss << _str << " : " << _str.length();
  return ss.str();
}

Testing In Xcode

I setup testing in Xcode using a separate tests folder and a second tests target and scheme. In this sample case, the main project code is in the SampleProject group (usually a library project) and the test target code is in the TestSampleProject group. This group (and its matching file system folder) are automatically created when you add an Xcode command-line target.

To create this target, select the project at the top and click + or choose File \ New \ Target from the menu, choose OS X \ Application \ Command Line Tool, choose C++ as the language and give it a name.

I then add catch.hpp and edit the main.cpp file. The main.cpp in the test target uses the default Catch main function, there is no point in writing my own:

main.cpp
1
2
#define CATCH_CONFIG_MAIN  // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"

I also add the following command-line options to the test target scheme and toggle when needed:

  • -r console sets up readable console output (not necessary but a good reminder)
  • -d yes shows the time taken on each test (usually disabled)
  • s verbosely displays tests (usually disabled)
  • You could also add any test tags you want to run as an option, but I find most of my test projects run sufficiently fast that I usually leave this out and run all tests. It also protects me from forgetting to run the full test suite.

I then start to add .cpp files for each test case and write my tests.

Finally, you also need to add each class that is being tested to the Test target as well or it will not compile.

Running the test target delivers the following console output on success:

1
2
===============================================================================
All tests passed (8 assertions in 2 test cases)

If any test cases fail, you get:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
TestSampleProject is a Catch v1.0 b53 host application.
Run with -? for options

-------------------------------------------------------------------------------
Testing Sample Class
  test bigStr
-------------------------------------------------------------------------------
/Users/Hiltmon/Projects/Spikes/TestCatch/TestSampleProject/testSampleClass.cpp:13
...............................................................................

/Users/Hiltmon/Projects/Spikes/TestCatch/TestSampleProject/testSampleClass.cpp:26: FAILED:
  REQUIRE( sc.bigStr() == "TestStr : 8" )
with expansion:
  "TestStr : 7" == "TestStr : 8"

===============================================================================
test cases: 2 | 1 passed | 1 failed
assertions: 8 | 7 passed | 1 failed

Correct the failures. Then add more tests. Run. Iterate, iterate, iterate. You know how to unit test.

Sample Project

You can download the sample project to see how it works from here.

Experience

I have been using Catch for several weeks now in multiple C++ projects and it’s been wonderful. Setup is easy, adding test cases is simple, running tests is a keystroke away and evolving my core dependency-free libraries is easier and more reliable than ever.

My thanks to Phil Nash (@phil_nash) for creating and maintaining this wonderful tool.

Follow the author as @hiltmon on Twitter.

Gist TextMate Bundle Updated for Yosemite

Thanks to Michael Sheets, the Gist TextMate bundle now works in TextMate 2 on Yosemite. The issue was that the UI code in TextMate 2 relied on Ruby 1.8, and Ruby 1.8 is deprecated and no longer installed in OS X 10.10.

Michael created a shim to fake Ruby 1.8 until such time as the code base moves to Ruby 2.0, implemented the change in the Gist bundle and it’s working now.

You can get the source at GitHub or just wait until it gets propagates via TextMate 2’s bundle update process.

Follow the author as @hiltmon on Twitter.

Create a Bootable OS X Installer Drive

Dan Frakes (@DanFrakes), previously of MacWorld fame, provides easy to follow instructions on how to create a bootable OS X 10.10 Yosemite installer USB drive on his personal blog. I’ve always done this and I recommend you do too.

Link: How to make a bootable OS X 10.10 Yosemite installer drive

Yep, mine is a R2D2 8GB USB Drive (called a Star Wars MIMOBOT) I found at the checkout at Kinokuniya in New York several years ago.

Follow the author as @hiltmon on Twitter.

TimeToCall Removed From App Store

I have removed TimeToCall from the App Store effective today, for several reasons:

  1. I did not update it for iOS 7 (or iOS 8) and it’s starting to look shabby.
  2. I never made any worthwhile money from it or received blog pageviews from it.
  3. There are newer and better products out there.
  4. I do not want to clutter the App Store with yet another abandoned product.
  5. It no longer showcases my development capabilities, they are far better now.

To those of you who purchased and used the product, thank you.

Follow the author as @hiltmon on Twitter and @hiltmon on App.Net. Mute #xpost on one.

Any Hint of Humanity Is Valued

For decades we have had corporatization, mass-production, and everything becoming the same, having personality drained from it. That became what was common and what was the norm. And then in the last, you know, 5, 10 years, this concept of like the artisanal, the special, the hand-made, like this is coming back …

[interruption edited out]

… I think the reason why everyone loves all this hand-made crap today is because they are starved for authenticity after decades of mass-market personality-less crap, and they are starved for uniqueness, they are starved for personality and they are starved for authenticity and for people to talk to them like, you know, talked to like an adult please.

Lets not, you know like, my first job like out of college, my first job was an internship in college. I worked a summer at Nationwide Insurance, just like doing computer crap for them. And somebody, somebody fucked up at some point and my boss said, and my boss made some comment that indicated that this person had fucked up in some kind of much more nice way.

And I said “Oh, is he going to get in trouble for that?”

And she said “Well, it’s a coaching opportunity.”

And I was like “What? So does that mean he is going to get in trouble?”

And she just kept saying “It’s a coaching opportunity.”

And she wouldn’t, she couldn’t say he’s getting in trouble.

And this has seeped in so far in business culture and everywhere in our culture that any hint of humanity in something is valued and its like everyone is starving for humanity and being talked to with respect like a real person, like an adult, and having some kind of sign that humans are behind this wall, that its not just a corporate machine transferring you to people who can’t help you on the phone. Like some hint that there’s a person behind this who is, who respects you and not in some token mission statement way, a person who actually respects you.

Marco Arment Debug Podcast 43 (10 July 2014)

I think Marco makes an amazing point here. All we use and deal with is corporatized, personality-less, robotiscized horror and so when we do actually get a person who helps, we grab on with both hands.

I use Hover because of this. I bank with HSBC because of this. I even pay American Express for his. And I use Apple products because of this.

Just look at Apple. Its products are full of personality. And you can meet real people in real stores to get real help at the genius bar. It’s humanity in action.

Compare that to AT&T, Verizon, utilities, and other banks with their remote call centers and robot answering services and recorded messages on how important you are and useless drones reading wordy scripts and not listening to you and not helping at all.

Who would you rather deal with?

Any hint of humanity in a product or service is valued, and those that recognize this and act on this are valued and loved and people, people like me, are prepared to pay a premium to have access to them.

Note: Marco quotes transcribed manually by me without permission, punctuation added, all errors are my own.

Follow the author as @hiltmon on Twitter and @hiltmon on App.Net. Mute #xpost on one.

Standard Markdown

Looks like Jeff Atwood (@CodingHorror), John MacFarlane, David Greenspan of Meteor, and folks from StackExchange, Github and Reddit have put their money where their mouths are and released the first version of Standard Markdown at http://standardmarkdown.com.

I believe this will be the future of John Gruber’s (@daringfireball) Markdown.

Up until now, I have relied on Fletcher Penney’s (@multimarkdown) MultiMarkdown on the desktop for all my documents because of its consistency, ubiquity and wonderful extensions for writers (including tables and footnotes). I use rdiscount for this blog for the same reason (especially it’s speed).

I look forward to seeing Standard Markdown being integrated into these tools to remove inconsistencies, but rely too heavily on the extensions that are missing from Standard Markdown to consider a switch just yet.

I will, however, be watching the development of this very closely.

Update Sep 4th, 2014

It seems John Gruber, the creator of Markdown, was not in the loop on this:

Not cool.

Knowing this now, I’ll wait to see if and how he responds first.

Follow the author as @hiltmon on Twitter.

The Quick Code Cleanup

The quick code cleanup is a process whereby you run through some or all of the files in a software project and tidy them up. Think about it as the regular tidying of your desk (or room) and filing things away – versus the rearranging and rewiring of a full code refactor. You can get a lot more done on a tidy desk.

Quick code cleanups can and do include some minor refactoring, but they are mostly about renaming items, reformatting code and commenting on or cleaning up messy sections. No features are added or changed.

So why do quick code cleanups? Because they save tremendous time as the project progresses. And the best part is, you do not need to do much thinking or need the zone (See the Four Hour Rule). For example, I just spent the last few hours doing a quick code cleanup while watching the US Open tennis!

Here are some of the things to do in a quick code cleanup:

Fix Bad Names

Naming classes, functions and variables is hard. Knowing how they may be used or evolve is almost impossible beforehand. When you name new classes, functions or variables, you have an idea of how you think they will be used. But as the program evolves, these old names may no longer be sufficiently explanatory or correct.

In a quick code cleanup, you revisit naming after-the-fact, but close enough to remember your thinking, knowing how the elements are now being used. You will find that you have a much easier time finding and choosing better names in cleanup. It also means that, when writing new code, you can relax a bit knowing you’ll come back and rename the items you are unsure of.

It makes the code more understandable for later.

Fix Lazy Formatting

While coding, we often forget to format our code properly. Or the IDE (I’m glaring at you, Xcode) helpfully generates its own code layouts for you.

In a quick code cleanup, you reformat the code to whatever standard you prefer, add white space, and rearrange the functions in files to make more sense. For example, I like to have functions that call each other closer to each other in the file so it’s easier to see them on the same screen. At coding time, I may not have written them or know which, at cleanup time I do. I also like to separate paragraphs of code with white space and comments, which I add in cleanup.

It makes the code a lot more readable and browsable for later.

Explain Some Decisions

If you have ever been in a code review you’ll know that the most common question is “WTF were you thinking?”. We know what we were thinking when coding, and we remember that thinking for a short while. But we all forget that thinking over time as other thoughts take over.

In a quick code cleanup, you add comments where necessary to explain your thinking. Why did you design a class this way, or write something that way. Or how does this complex calculation work? What was I thinking at the time?

It makes the code a lot more maintainable for later.

Early Cruft Removal

We all create classes and functions, use them for a while, and then replace them. We do not always remember to remove unreferenced classes or comment them out from the code base. Later on, when searching for how something went badly wrong, we get confused by this extra code. Is this the function called or another? This, to me, is how cruft starts.

In a quick code cleanup, while your mind is fresh, remove all classes and functions that are not being used. If you need them again later, you can get them back from source code control. If unsure whether something is being used, use search in your IDE to see if the function or class is referenced (or just comment it out and compile – if the compile passes, it is cruft).

It makes code smaller, simpler and less confusing for later.

Basic Refactoring

Other than renaming classes and variables, a quick code cleanup also usually entails:

  • Pulling up common and duplicated code into functions, or replacing long, complex, deep if clauses with function calls
  • Adding standard file headers (if necessary)
  • Hiding public variables behind setters and getters (one or both)
  • Commenting out or deleting surplus debug statements
  • Adding missing asserts to document your assumptions

It reduces the number of potential bugs for later.

When to run a quick code cleanup?

I try to do this every few weeks on current projects, when the code is still fresh in my mind and when I have some time. This often coincides with a minor release or the few hours at the end of the week when I know I will not have enough time to enter the zone and work on a new feature.

The quick code cleanup may not seem to be a productive use of your time, but it pays off tremendously as the software grows in size and complexity. As the code-base expands, it yet remains understandable, readable, maintainable, simpler and with fewer bugs, enabling you to spend more time crafting new code and less time figuring out what the hell happened in the past and untangling a knotted mess.

You can start doing quick code cleanups anytime.

Follow the author as @hiltmon on Twitter and @hiltmon on App.Net. Mute #xpost on one.

Aral on Spyware 2.0

That word is spyware.

Let me state it plainly: Google is a spyware company. Facebook is a spyware company. Any company whose products spy on you is a spyware company.

The first step in understanding and defeating something is to call it what it is.

Read Here

Follow the author as @hiltmon on Twitter and @hiltmon on App.Net. Mute #xpost on one.

Quote of the Day

In sum and once again: Amazon is not your friend. Neither is any other corporation. It and they do what they do for their own interest and are more than willing to try to make you try believe that what they do for their own benefit is in fact for yours.

Replace Amazon with any company name of your choice and this remains 100% true.

Follow the author as @hiltmon on Twitter and @hiltmon on App.Net. Mute #xpost on one.