Understanding the word “semantics” in the context of programming

tl;dr- It’s usually safe to substitute the phrase behaviors and guarantees into a sentence where you see the word “semantics”–and the discussion is about programming.

Longer version: New programmers often come across the word semantics, and wonder what it means. Pretty much every explanation they will read points out the distinction between syntax (form) and semantics (meaning). This is easy to grasp, but not useful for understanding the word in the context of a sentence like: The stylistic choices should typically be driven by a desire to clearly communicate the semantics of the program fragment.

Go ahead and substitute the word “meaning” there. It isn’t much help unless you’re already an experience developer.

So to that end, new programmers… if ever you come across this word, it’s generally safe to substitute the phrase behaviors and guarantees in its place. This may help you understand the semantic intent (ha!) of the writer a little more.

Recursion as sophisticated GOTO?

Suppose you write a simple program to play a number guessing game: “I’m thinking of a whole number between X and Y…” where the user attempts to guess the number in order to win. Failures that require feedback to the user come in three main flavors:

  1. The user guessed incorrectly
  2. The user’s guess was out of range
  3. The user did something silly, like enter a non-integer value

The do..while form

You can do this with a loop, of course. A do..while loop seems like a natural choice here. What I don’t like is the validation and user feedback in the while clause. You’d call out to a method that hides all of the validation logic, and returns true or false. This is OK, but that boolean method will have a side effect, i.e. feedback to the user.

do {
    //increment guess count
    //ask for input
    //read in input
} while (
    //input is wrong

The recursive form

An alternative implementation might use recursion:

static void elicitGuess() {
    //increment guess count
    //ask for input
    //read in the input
    //validate input inline
            //display relevant feedback
        //else return

I find this second way to be more natural and readable. But it also feels like the wrong thing to do, though I can’t articulate why. Maybe it’s because any time I’ve seen code that followed this kind of pattern, I’ve seen it written using loops.


Memory access patterns in high-level languages

Like many developers that work in high-level languages, I think don’t spend a lot of time thinking about memory access patterns. This is probably a good thing… for the most part, worrying about this is premature optimization. But there are times when it matters, and the compiler won’t magically “optimize” it away for you, even if you have optimizations turned on:


class MemoryAccessPatterns
    static void Main(string[] args)
        var timer = new Stopwatch();
        for (var i = 0; i < 25; i++)
        Console.WriteLine("Fast array: {0}", timer.ElapsedMilliseconds);
        for (var i = 0; i < 25; i++)
        Console.WriteLine("Slow array: {0}", timer.ElapsedMilliseconds);
    private static void doFast()
        var fast = new int[5000, 5000];
        for (var i = 0; i < 5000; i++)
            for (var j = 0; j < 5000; j++)
                fast[i, j]++;
    private static void doSlow()
        var slow = new int[5000, 5000];
        for (var i = 0; i < 5000; i++)
            for (var j = 0; j < 5000; j++)
                slow[j, i]++;


Release x86, optimizations on:
Fast array: 3829
Slow array: 8495

Release x86, optimizations off:
Fast array: 4357
Slow array: 8675

Release x64, optimizations on:
Fast array: 5074
Slow array: 10445

Release x64, optimizations off:
Fast array: 5954
Slow array: 10781

I tried this out after seeing this discussion thread on Quora this morning.

How to fix broken iCloud photostream sync on Windows


  • Your iPhone is set to back up your photos to iCloud
  • iCloud on your Windows machine is configured to download your photos
  • iCloud isn’t downloading your photo stream.


  1. Open the Task Manager by hitting Ctrl+Shift+Esc
  2. Click the Processes tab
  3. Click Name to sort the processes by name
  4. Find the Apple Photostreams Uploader and Apple Photostreams Downloader processes. End both of them.
    • In Windows 7, these will be called ApplePhotostreamsUploader.exe and ApplePhotostreamsDownloader.exe
  5. Hold down your Windows key, and hit R to open a Run prompt
  6. Type %appdata% and hit Enter
  7. Open Apple Computer > MediaStream
  8. Delete everything in the directory
  9. Log out of your Windows account, and log back in (or just reboot, if you find that easier)
  10. Once you’ve logged back into your Windows account, open the iCloud control panel again
  11. If the Photos checkbox is empty, check it
  12. Click Options, and make sure the photo options are configured how you want them
  13. Click Apply

In a few moments, your photos should start downloading.


iCloud isn’t very smart about a great many things. Here are a few:

  • If you changed the location of your downloaded photos, it will redownload what it can, creating duplicates.
  • In the iCloud 2.x days, your downloads and uploads were usually split into a Downloads and Uploads directory, and you could change the directories if you wanted. That’s not true anymore. Instead, iCloud 3.x creates a “My Photo Stream” directory, and sticks your downloads in there. Anything you’ve shared with other people, or that other people have shared with you goes into “Shared”. If you want to push a photo from your computer to iCloud, put it into Uploads

If you found this post useful, please consider donating $2

How to get Medicare to reimburse you for seeing a non-Medicare doctor

This post explains how to get reimbursed by Medicare directly, and is applicable to people who have Part B coverage. I have included the exact forms and letters that I used to get reimbursed by Medicare directly; you simply need to fill in the blanks. These forms can also be used if patients see their provider “virtually” (i.e. by phone or video conference).

The problem

I have a family member that gets reimbursed directly by Medicare for out-of-pocket costs that she pays herself. Several unusual criteria apply to our case… This family member:

  • Has a health care provider that does not accept Medicare
  • Lives in a rural, health professional shortage area (HPSA)
  • “Sees” her provider virtually. Medicare refers to these types of visits as telehealth, and they are reimbursed at the same rate as normal, in-person visits; there are no special telehealth billing codes. To be eligible for telehealth reimbursement, you must meet certain criteria.
  • Uses me as her Social Security Payee, which makes her my legal dependent. (I have to do everything on her behalf.)

Despite all of these special circumstances, we’re able to get reimbursed by Medicare directly, which is $3-4,000 a year. We had to appeal twice on our first attempt at getting reimbursed. The language and documents I have distilled below reflect this. It helps to very, very clear about why your request is valid.

Appeal letter: Word version | Google document

Need an example of an appeal letter? Post in the comments below, and I’ll clean mine up.

Supporting documentation

  1. Fill out a “Patient request for medical payment” form
  2. A signed, dated letter from your provider that states that they refuse to submit claims to Medicare, and that they are not (and don’t wish to become) a Medicare provider (Microsoft Word | Google Doc)
  3. A copy of an itemized bill for services rendered in the last however many months. This bill should contain the date of service, cost, and the service rendered. Once you’ve got the reimbursement process down, you should submit a reimbursement request every 3 months or so. (Microsoft Excel | Google Spreadsheet)
  4. A request to be reimbursed that references the pieces of documentation above. (Microsoft Word | Google Doc)
    • For telehealth reimbursement only: A letter stating that there are no suitable providers in a 500 mile radius of the patient’s address, or that the patient is in an HPSA-designated region. I included this information in the reimbursement request itself (#3). By the way: telehealth visits are generally only applicable to mental health services.

You do NOT need to include diagnostic information.

Ways you may need to change the included sample letters

  • Change the billing codes so they’re relevant for your case.
  • Change the provider’s name and credential information.
  • Look for stuff in the letters that looks <Like this>, and replace it with information relevant to your case.
  • If you are doing this for yourself or for someone who is not a dependent: Each of the sample letters is written by me on behalf of a legal dependent. If this doesn’t apply to you, modify each document so it’s written from the perspective of the patient themselves: use “I”, “me”, etc., and have them sign it.
  • If you are not trying for telehealth reimbursement, remove the sections that talk about being in a health professional shortage area and/or telehealth references.
  • Make sure the Conclusions section in the request letter is relevant. For example: my version includes comments about telehealth; yours may not, so delete them. Make sure they make sense for you.

Send the paperwork certified mail

You won’t be sending your 3-4 pieces of documentation to Medicare. You’re going to be sending them to a Medicare Carrier, which varies by state. Find yours here.

We did not send our first appeal certified mail, and it got lost. This cost us three months. Save yourself the time and aggravation, and send your documentation certified mail.

Be prepared to wait

It took me over a year to finally get reimbursed by Medicare. It took three incomplete attempts, and two appeals before I won my case. You may not succeed the first time… but keep at it. If you can push through the bureaucratic apathy, you will probably succeed.

Good luck! Ask any questions in the comments below, and I’ll do my best to answer them. Getting Medicare to reimburse you for covered expenses is NOT easy.

Update 2014-09-06: The second round of reimbursements went off without a hitch. No appeals, no additional documentation. Just a large check a little over a month after the reimbursement request was filed!

redis: Sorted Sets as iterable key-value work queues

I’m in the process of building an internal search engine at work, and first on the block are our network share drives. During a typical week, we have about 46,000 unique documents that are written to in our Lexington office. This number only represents things like Word docs, Excel spreadsheets, PowerPoint presentations, PDFs, and so forth. (A count of all touches is 3-4x higher.)

This presents some challenges: crawling the network shares at regular intervals is slow, inefficient, and at any given time, a large portion of the search index may be out of date, which is bad for the user experience. So I decided an incremental approach would be better: if I re-index each document as it’s touched, it eliminates unnecessary network and disk IO, and the search index is updated in close to realtime.


My second-level cache/work queue is a redis instance that I interact with using Booksleeve. The keys are paths that have changed, and the values are serialized message objects stored as byte arrays that contain information about the change. The key-value queue structure is important, because the only write operation that matters is the one that happened most recently. (Why try to index a document if it gets deleted a moment later?)

This is great in theory, but somewhere along the way, I naively assumed it was possible to iterate over redis keys… but you can’t. Not easily or efficiently, anyway. (Using keys in a production environment is dangerous, and should be avoided.)

Faking iteration

The solution was relatively simple, however, and like all problems in software development, it was solved by adding another level of indirection: using the redis Sorted Set data type.

For most use cases, the main feature that differentiates a Set from a Sorted Set is the notion of a score. But with a Sorted Set, you can also return a specific number of elements from the set. In my case, each element returned is the key to a key-value pair representing some work to be done.

Implementing this is as easy as writing the path to the Sorted Set at the same time as the key-value work item is added, which can be done transactionally:

using (var transaction = connection.CreateTransaction())
    int i = 0;  //dummy value representing some "work"
    foreach (var word in WORDS)
        transaction.SortedSets.Add(REDIS_DB, set, word, SCORE);
        //Set the key => message (int i) value
        transaction.Strings.Set(REDIS_DB, word, i.ToString());

Downstream, my consumer fills its L1 cache by reading n elements from the Sorted Set:

var pairs = new Dictionary<string, string>();
using (var transaction = connection.CreateTransaction())
    //Get n keys from the set into the Dictionary
    var keyList = connection.Wait(connection.SortedSets.RangeString(REDIS_DB, set, 0, LIMIT));
    foreach (var key in keyList)
        var value = Encoding.Default.GetString(connection.Strings.Get(REDIS_DB, key.Key).Result);
        pairs.Add(key.Key, value);
        //Remove the key from the SortedSet
        transaction.SortedSets.Remove(REDIS_DB, set, key.Key);
        //Remove the key from the Keys
        transaction.Keys.Remove(REDIS_DB, key.Key);

And there we have fake key “iteration” in redis.