Manage Your Context Switches to be More Productive

You’re in the middle of patching a bug, and a colleague taps you on the shoulder asking for your opinion on something. We’ve all been in this position. Whether or not you’re a programmer, this situation happens all the time — a phone call interrupts our planning session, a push notification distracts you from finishing your report — and it’s highly destructive to your throughput as a worker.

I decided to share some techniques I’ve started to employ that have really worked for me. I’ll start with ways to avoid distractions in the first place, and then go over what to do in the event that a context switch is inevitable.

1. Turn off all non-essential push notifications
What is an essential push notification? It depends on your engagement level with the rest of the world, but for me it’s limited to just SMS messages. Instagram, GMail, Snapchat, FB messenger (which I don’t use)… shut ’em all off.

2. Batch tasks (like email) into pre-defined chunks

For mindless tasks like email, I typically tend to reserve a part of my least productive hour: 4-5pm.  I only check and respond to email during this allotment. Only coding gets my precious morning hours, when I’m most alert.    Make sure your pre-defined time allotment has a end time. Set a countdown timer. You can even use Siri:

  “Hey Siri, set a countdown timer for 30 minutes.”
  “Okay, 30 minutes and counting!”

Great – now you have a defined end to a task that used to interweave itself into your whole day.

2. Work in DND (Do not Disturb) for at least 4 hours each day
Both your Mac and iPhone can be configured to run in DND mode. It doesn’t necessarily have to be 4 hours, but try experimenting for what works best for you.  For me, I set a goal of a number of tickets I want to accomplish before allowing myself to be “disturbed.”

3. Be choosy about the music you listen to while working
Everybody is different, but we share about 99.9% of the same DNA. And study after study has shown that music with lyrics is distracting for human beings who are doing cognitively intensive tasks such as coding.  For me, headphones are often necessary to escape the sounds of workplace like the clacking of keyboards and sniffling, but instead of listening to music, often I choose the ambient sound of a coffee shop.  To the Spotify/Pandora streamers out there: Pay for the darn subscription plan so you aren’t subject to distracting ads.

4. Buy a pen & paper notepad
They can be purchased at any office store for $0.99. Use this as your scratch buffer for things that need to be done throughout the day. Never do something as soon as it comes up.  Here’s an example: let’s say I think of a new feature that needs to be implemented…. rather than signing into Jira and filing the tickets right away, I just put a 5-word reminder on my scratchpad which I can then reference when I go to do Jira tickets in batch at the end of the day.  Same thing with email — don’t send that email right away… put a note on the scratchpad and wait until your dedicated email batch time allotment to write the full draft.

5. Don’t get dragged into non-problems
Engineers in particular love to solve everybody else’s problems but their own.  I’m no exception.  I hear about some intern using a c4.8xlarge to run a webserver, and I immediately stop what I’m doing to optimize our EC2 allocations. That tangent then turns into a sweeping overhaul of the company’s AWS spot-pricing and IAM strategy.

In part 2/2 I’ll go over how to save your state in the event that you have to switch contexts.

 

Swift 3 Extension to UIImage to quickly retrieve image from a URL

I spent too long looking for a quick, non-deprecated solution to this simple problem this morning.  Here’s what I came up with. Maybe it will save you a few minutes of searching.  This is valid for Swift 3.

Since I have tableViewCells that need to set images from a URL string, I figured it would be best to make an extension that could be called on the cell configuration:

The extension:

extension UIImageView {
    public func image(fromUrl urlString: String) {
        guard let url = URL(string: urlString) else {
            log.warning("Couldn't create URL from \(urlString)")
            return
        }
        let theTask = URLSession.shared.dataTask(with: url) {
            data, response, error in
            if let response = data {
                DispatchQueue.main.async {
                    self.image = UIImage(data: response)
                }
            }
        }
        theTask.resume()
    }
}

Usage is a one-liner
cell.productImage.image(fromUrl: order.product.imageUrl)

Turn off iMessage link previews in iOS 10+

There is currently no way to turn off link previews in iOS 10.  This “feature” is a half-baked attempt to add slack-like features, where GIFs, YouTube links, pictures, etc show up in-line in your iMessage conversation thread. I really wish Apple would have added a way to turn this off because it slows down the interface on my older iPhone 6, and also presents a security/privacy risk. If I don’t accept the privacy policy of a website, I used to have the choice to not click on the link go to that website.  Further, by going to that site, I reveal all sorts of information including my IP (and possibly even my location). With iOS 10, the messages app does this automatically, whether you wanted to load an image (on cell data) or not.

There is a way that you can turn off this “feature” when you’re sending links to people: Don’t put the link as the first thing in the message… I use the “.” character to disable this on links that I send.   You can (and should) also send a feedback report to Apple asking them to add in a user setting to disable the “feature.”

I did:

 

ios-turn-off-link-preview

Debugging MongoDB’s 2dsphere geoIndex

When I first learned about MongoDB’s geospatial capabilities similar to PostGIS, I was beyond excited because it meant I would not have to write helper functions to find data along geohash borders.  Currently my data is indexed by geohash, which has great ordering properties, but is a little unwieldy for the “find me x near y”-type queries.

Unfortunately, it seems as though the 2dsphere index doesn’t cover geoWithin queries.  As my collection of geoJSON documents grew from GBs to TBs,  I noticed my tools taking extraordinary amounts of time to perform find queries, which caused me to investigate what was happening under the hood.

First, I double-checked that my geospatial index was present:

mongos> db.my_collection.getIndexes()
[
  {
    "v" : 1,
    "key" : {
      "_id" : 1
    },
    "name" : "_id_",
    "ns" : "telemetry.********"
  },
  {
    "v" : 1,
    "key" : {
      "value.avg_location" : "2dsphere"
    },
    "name" : "geoindex",
    "ns" : "telemetry.*******",
    "2dsphereIndexVersion" : 2
  }
]

So, the index was clearly there.  I also verified that its size was appropriate — it was around 50mb, which seemed fine for this amount of test data.

Next, I ran .explain() on a query to see if it was hitting the index:

Continue reading Debugging MongoDB’s 2dsphere geoIndex