Skip to content

Dictation Is Not Writing

I like to think I’m a decent writer: able to convey information clearly, be evocative – maybe even provocative – while the clickety-clacks make the words slide onto the screen. I’ve been doing an experiment lately in an effort to be a *fast* writer: I’m dictating lots of my writing using OSX’s built-in speech-to-text, and Google’s Voice Keyboard on my Android phone. And it’s been going okay all in all – I’m not able to speak faster than I can type, and I lose even more time having to go back and correct words and especially punctuation. But I think that’s to be expected. It’s not good enough to use instead of a keyboard, but it’s good enough to feel like it’s the future, and if I can just get better at being understood by these machine, my speed will eventually surpass my typing speed. The dictation seems better on Android, which is why I’ve started using it to dictate emails as well. I can dictate a few words at a time in almost real-time, as long as I’m keeping it short. It’s a real boon to productivity, especially when driving or I’m busy with the kids. But I’ve decided to *stop…

Trouble Recognizing Second Monitor Since Yosemite

I’ve been having issues getting my 2011 MBP to recognize my second monitor ever since the Yosemite update. Even the Detect Displays trick didn’t help. But, finally, I found one thing that worked for me every time: Command+F2 typed on the laptop keyboard (doesn’t work on external keyboard FYI) runs an apparently superior “Detect Displays” command, and like magic the second screen is recognized and starts working again. Found this trick on the end of this 6-year-old forum:[settings][page]=4#topic-reply-list UPDATE After this worked flawlessly for a week, it stopped working altogether. Now, I can’t get the monitor to be recognized no matter what voodoo I try.

Refactoring to Metaphors: Wiring Harnesses

If you have trouble starting to write testable code, you’re not alone. So many of the concepts are written about by brilliant coders (like Michael Feathers and Joshua Kerievsky), but they tend to write for other advanced developers looking to improve their work. If you’re just trying to get started down the road of testability, reusable code, and reliable changes, it can be daunting.

PHP 4 Constructors and PHP5 Namespaces Don’t Mix

Using a class defined in a namespace will disable a legacy Constructor. All classes in namespaces *must use* __construct. You might run into this problem trying to convert an old library to be PSR-2 (autoloader) compliant. If you want to use a legacy library in Laravel or Symfony, you’ll have to watch out for this.

Batching Inserts by Combining Queries

Obligatory warning: Always escape your inputs! This assumes you are migrating known safe data! I see lots of legacy code (especially bulk data migrations) that perform database operations inside a loop. Like this:

This results in a series of queries:

but we can do better. SQL inserts can be stacked into one query:

This still inserts all 3 records, in just one transaction. And it’s easy to retrofit our existing code:

A little more code of course, but this will speed up your inserts dramatically.

Tired of dealing with Leap Years? Use DateTime::diff()

Doing this the old-fashioned way was pretty complex. It was bad enough when a normal February would mess up your math, you also had to take leap years into account. Using the DateTime functions in php >= 5.3 lets you think about your business case, not calendars.

Don’t take my word for it! **Test it**!

For more info see the php docs:

Fake Multiple Inheritance

Leveraging code reuse in a world without mixins First, we need our interface. All the other objects conform to this interface, so they are all polymorphic.

The DiscountCart is the most primitive of the Discount types. It gives the customer a 15% discount on their entire cart. Inheritance.

The VolumeDiscount inherits from the DiscountCart, and overrides with its own discount based on the volume of each product in the cart. Inheritance with a method override.

There is another discount which adds free shipping. Inheritance with a property override.

Now, there is a very special customer type: the Frequent Customer. We give them a Volume discount AND they get free shipping all the time. If we had mixins, we could just “mix in” the free shipping, but since this is php (or C# for that matter) I do it this way:

This lets the FreeShippingDiscountCart be compatible with any IDiscountCart polymorphism and leverage inherited traits from its private objects. That way, if Free Shipping gets complicated (ex: drop-shipped or sale items are not eligible) I don’t have to re-implement the exceptions.

Add page slug to class list using wp_list_pages()

Add this code to your functions.php file.

Ordinal Suffixes in PHP

Continue reading Ordinal Suffixes in PHP