Soylent Code

Soylent Code is People! It's peeeeople!

The Case Of The Lost Commits

Recently at my gig we converted a large project from HG to Git. There are a lot of developers on this project; many who have never worked on Git so I expected a few bumps. So far it’s actually been pretty smooth sailing but yesterday I was contacted by a dev with a Git problem I had never seen.

The day before he had made a commit and pushed it to the server but now the content of the commit was gone. Not reverted mind you, just gone, like it had never happened even though the commit was clearly still in history.

To be clear, looking at the history of the entire repo showed the commit and it’s changes as something that happened. But looking at any of the individual files in the commits didn’t show the commit at all. WTF?!

This turned out to be the result of a bad merge by another developer. I was able to recreate the scenario, take a look at the weird history:

ryber$ git log --graph --oneline --all
*   1b4cd92 Bad merge by dev B
|\  
| * e879eb6 This is the missing commit by dev A
* | 93933b9 commit by dev B
|/  
* 6baed99 root commit

Here we can see that e879eb6 is in history. You can see that part of that commit was a change to foo:

ryber$ git whatchanged e879eb6
commit e879eb6007ddef2a955a71651bcf31a25727b510
Author: ryber
Date:   Sat Mar 22 16:37:28 2014 -0500

    This is the missing commit by dev A

:100644 100644 eb314de... e3525a0... M  baz
:100644 100644 ae3cab0... cf561bd... M  foo

Yet, if we look at the history of foo that e879eb6 is missing!

ryber$ git log --pretty=oneline --abbrev-commit -- foo
6baed99 root commit

What happened here? Where did e879eb6 go in the history of foo? I can understand if the change was reverted but shouldn't we see some history of that revert? This is where we get into the bad merge

You may have notice that the missing commit includes another change to the “baz” file. It turns out that the second dev also changed baz in 93933b9 and was forced to go into a merge conflict when he pulled. To someone new to git the merge process might be a bit shocking. This is because you see all of the changes impacted by the merge. This includes your own changes as well as all of the changes to files in the tree you are merging in that happened after your last common ancestor. Developer B was presented with something like this when he was merging:

ryber$ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 1 different commit each, respectively.
  (use "git pull" to merge the remote branch into yours)

You have unmerged paths.
  (fix conflicts and run "git commit")

Changes to be committed:

	modified:   foo

Unmerged paths:
  (use "git add <file>..." to mark resolution)

	both modified:      baz

You might think “What the hell? I didn’t change foo?! Why is foo here?”. You might even attempt to get rid of the foo changes … which is exactly what happened. It’s actually kind of hard to do from the shell but fairly easy to do from some gui tools like SourceTree. From the shell you just have to issue a checkout of a previous version like this:

ryber$ git checkout HEAD^ -- foo

Then an add, and a commit and voilà! The file has now been reverted to it’s previous state as part of a merge and it’s individual content history no longer contains the missing commit.

You may be wondering how we got out of this mess? We simply cherry-picked the commits back into the head. Not very subtle but it worked.

ryber$ git cherry-pick e879eb6

Music For Coding

A short list of my favorite coding music:

Information Society Hack

In the late 80’s and early 90’s as I was getting into hacker culture InSoc was a major influence for me and my friends. They have everything: songs inspired by William Gibson’s cyberpunk novels, love songs to Nikola Tesla and IBM, samples from Star Trek, and a sound that gave the impression it was made on an Amega at 3:00 am after they left a rave. Best of all they regularly encoded messages and hacker challenges into analog data-sound tracks at the end of their albums. The minute a band has you wiring your CD player into an old handset modem they’ve won.

Kraftwerk: The Mix The Mix

It may be cliche for a programmer to love Kraftwerk, but I don’t care. More than just a best-of “The Mix” is rare in the world of electronic re-mix albums in that it’s mostly better than the originals. I particularly find the version of “Computer Love” to be the best out there.

Robyn: Body Talk Body Talk

Don’t chalk up Robyn as just another disposable dance club act. The electronic music she produces is impeccably produced and well crafted. It has numerous interesting patterns and layers that pay back on repeated plays. The Body Talk series is by far the best. For something even more abstract check out her work with Röyksopp.

Santigold Santigold

Whenever I listen to Santigold I imagine this is the music the the rastafarian hackers from space station Zion are listening to in Neuromancer.



The Soundtrack to Conan the Barbarian A man, wearing nothing but a loincloth and a horned helm, strides forth, holding a sword aloft in his left hand. A blond woman kneels in front of him, holding a curved blade with both hands.

The soundtrack was made first, and then the movie was basically a giant music video for a remix of Carmina Burana. Perfect background music for epic coding.

A few others

  • Gary Numan
  • The KLF aka The Justified Ancients of Mu Mu aka The Timelords
  • The Orb
  • Aphex Twin
  • Brian Eno
  • Daivd Bowie’s Berlin trilogy (Low, Heroes, Lodger)

Get Off My Lawn

Back when I first learned to program we didn’t have fancy “IDEs” or “text editors” that let you scroll through “files”. Hell we didn’t even have files. We wrote our code directly into a terminal one line at a time and if we needed to edit that line we rewrote it entirely. We could only see about 20 lines of our programs at any given time and that was all we needed. We didn’t have a dozen other windows for “file explorers” or “unit test runners” or any of the other useless crap kids these days fit on their screens.

The reason we didn’t have any of that is because our computers could only do one thing and that was run a BASIC interpreter. We didn’t have “hard drives” and only the fancy pants kids had disk drives of any kind. On my block you turned on your computer, you programmed it to do something, and then you turned it off, and when you did your program went away. I used to program games and then keep my computer running for weeks so I could keep playing it. Eventually I did get a cassette tape recorder that could save data. It saved my games sometimes and would load the ones it did save...sometimes. I’d say it had about a 30% success rate at doing anything successfully. The rest of the time you just re-wrote the program again.

Back in those days we didn’t have the “internet”, so there was no “Stack Overflow” or “GitHub” where you could find out how to code. You had to subscribe to a magazine like Byte that had programs written into the back of it. You would copy the entire program by hand and then spend several hours trying to figure out where you copied it wrong. Then once you finally had something that worked you could experiment by changing lines. That's how we learned.

If you did want to program anything more complicated than printing your name over and over you had to plan how many lines you thought a section would take beforehand. You see we didn’t have “classes” or “functions”. We just had blocks of code that took up a section of numbered lines. You thought “I think the code necessary to draw this sprite will take up 10 lines”. So you set aside lines 500-599 because you knew you were always wrong my at least a factor of 10. And if you needed more after that you had to keep the rest of the lines somewhere else because 600 was probably already taken. You couldn’t just lazily hit return 20 times and have everything move for you. It just didn’t work that way and besides, we couldn’t waste valuable bytes on luxuries like carriage returns!

You always kept a notebook next to you where you documented where the lines of various subroutines were because the computer sure as hell wasn’t going to help you. All you could do was list the program, or list a range of line numbers. “But why don’t you just pipe it through grep” you say? Because I ain’t some damn college professor that’s why! These are home computers dag-nabbit not some million dollar Unix server from Ma Bell. We didn’t have “grep” or “sed” or “pipes”. We had LIST...and we liked it!

TI 99/4a homescreenMy first computer was a TI 99/4a. It had a 16 bit 3 MHz processor and came with 256 bytes of RAM and 16kb of extended VRAM. That was more than enough for any of the programs I wrote at home. I made my own knock offs of games like Centipede, Pong and Bezerk. Just let that sink in for a moment. Centipede, written in BASIC, in under 16kb of RAM. When Bill Gates supposedly said that nobody would ever need more than 640kb of ram we all believed him because that was an INSANE amount of memory. At the time I couldn’t even fathom what I would possibly use that much memory for. You can only run one program at a time anyway.

We take for granted how luxurious software development is today. We have endless supplies of ram and disk. It’s easy to forget that not long ago we had almost nothing and yet we did amazing things, and made amazing messes of our code. Some things never change. Happy New Year and GET OFF MY LAWN!

What Developers Want

The other day I got a email from a recruiter. Now, I get stuff from recruiters all the time but this one stuck out a bit because it didn’t follow the standard line. I hope she doesn’t mind but I decided I wanted to answer her questions here on my blog because I think they are important.

Her main question was: “What do developers really want out of professional opportunities that they might not be getting with their current employers? To put it another way, what are the things that entice you guys?”

That’s an interesting question. What DO we want? Why do some employers have a bad rap in town while others seem to have their pick of the top developers?

First off I want to dispel one myth. The recruiter followed her question up with “My inclination is that money is a secondary factor when seeking out new grounds for professional growth”. Yes and no. Money is not the ONLY factor but it certainly is a large factor and it can easily disqualify an employer from a job search. Especially for the top talent in town who may be accustomed to a certain lifestyle. We all live in this same economy. We have families and children we want to send to college. We want to take that trip to New Zealand. We want a nice home. Money is a factor. Your job as an employer is to make sure that it’s not a negative when I’m considering you. It’s quite insulting and sad when you meet someone who thinks a beer fridge and a foosball table somehow makes up for low salaries. It doesn’t.

That said, while pay is a factor for why we might NOT take a job it’s not an indicator of why I might take a job, or stay at a job. As long as the pay is in a competitive range the other factors come to the front. So what are those factors?

  1. “Self-Determination”. People want to feel like they are contributing to solutions. They want to bring value to their clients. The absolute last thing they want is to feel like some cog responsible for implementing someone else's design. This is why top-down architecture is so detrimental to good teams. I want some amount of influence over the design, the architecture, and the tools. I want to feel like when I come to work people want me to be there because they value my contributions, opinions and the code I write. Most importantly, if something is in my way, or is inefficient, and I have a better way to solve it, I want the power to solve it. Nothing is more frustrating than being told. “Yea, we know it sucks, but we have to do it that way because [corporate policy, Joffrey the “Architect” said so, we always did it that way, derp].”

  2. Customer feedback: I want to know that the work I’m doing matters. Make sure developers have the opportunity to interact with customers, even if that is part of market research or trade conferences. Ideally I want constant feedback from a product owner who works with the development staff every day and makes sure we are working on the right things.

  3. A cool project with cool technologies: As developers we like cool things and cool toys. I realize sometimes you’re an insurance company and there is little you can do to make your app more exciting. That just means you need to get creative. Sorry but nobody wants to work on a legacy struts app running on Websphere.

  4. XP: I can’t speak for everyone on this but I think I speak for a growing majority. I will not consider a job where XP practices are not followed and embraced. Particularly BDD/TDD and CI. I’m also cool if you’re not there yet and you want me to help you get there. Just don’t be wishy washy about it. XP is the one exception to rule #1. Everyone needs to get on board. We are professionals, act like it and take your craft seriously.

ATDD vs xUnit Frameworks

I’ve been in many debates about if acceptance testing should be done in a unit test framework or a “proper” ATDD tool like Cucumber or Fitnesse. Developers will change a unit test to do what they want in a heartbeat. They are more reluctant to change an acceptance test where the criteria is separated from the code. This is a very important distinction. I think a lot of developers think of unit tests as belonging to “them” and acceptance tests as belonging to “the business”.

So yes, those test frameworks do suck, but they give you something that unit test frameworks just aren’t designed for. Whenever I see “acceptance tests” written in a unit test framework they just look like really poorly written unit tests, So take the extra time and use the right tool for the right job.

I have seen one exception to this. At Agile 2012 I attended a talk from Liz Keogh about writing BDD tests in a domain specific syntax from within a unit test framework. I thought it was a excellent idea and I have used the style on my own personal projects. I want to emphasize that what Liz has done is not entirely dissimilar to traditional ATDD frameworks. Her data, and criteria are absolutely separate from the underlying code. Even going so far as to abstract away top level controllers and domain objects behind fixtures.

Here is an example of the "Keogh Style" testing from the unit test class. Note that those are all static methods of an external fixture class. In many ways it is no different than the text file side of Cucumber. All of the work of dealing with the underlying classes is behind the fixture leaving the jUnit methods with nothing but simple asserts.

@Test
public void canCountRegistrations() {
    givenCourse("abc", "Underwater Basketweaving");

    registerUserForCourse("abc", "barry");
    registerUserForCourse("abc", "gary");

    assertEquals(2, getRegistrationCount("abc"));

    registerUserForCourse("abc", "larry");
    registerUserForCourse("abc", "larry");

    assertEquals(3, getRegistrationCount("abc"));
}

You can find more examples in this project.

Joda Time Presentation

Recently I did a little presentation on Joda Time after converting a codebase to use it. Here is the deck:

Hacking For Health

Lately quite a lot of people have been asking me about my weight. I lost over 35 lbs in about 4 months and I’ve been a little surprised at how many people have thought it was a big deal and wanted details on my plan. I don’t think anything I've done is extreme or weird. It’s mostly just lifestyle changes. I told a few people I would blog the specifics so here it is.

Impetus

6 months ago my father passed away from a sudden heart attack. While it was a shock, it was not surprising given his lifestyle choices. I decided that I couldn’t change my genetics so whatever else happens, I was not going to contribute to an early death the way my dad did. I didn't go into this specifically to lose weight, I did it for a healthy life and losing weight has been a side effect of that.

Work

My sit-n-stand deskI’m a computer programmer. Not exactly the kid of job that encourages a healthy lifestyle. So I came up with a few rules:

  • Don’t sit for more than 20 minutes. After that you have to go do something or at least change position.
  • If you need to use more than 1 sentence to answer a email and the person is within walking distance then go visit them. Email sucks as a communication tool anyway.
  • If you’re stuck on a problem go for a walk. Sometimes just getting the blood going helps you think of the answer.
  • At my office we have sit-n-stand desks. I highly recommend these. if you don’t have them available, start talking about it with your boss because sitting can kill you. I probably stand at least half the day. Also, when your tired, sit down, because standing too much can kill you too.

Exercise

I “work out” 30 minutes a day, every day. “Working out” can be defined as pretty much any physical activity that raises the heart rate. So a long afternoon of yard work counts. When I was in the middle of dropping weight, rather than just maintaining like I do now, I was doing 60 minutes a day broken into two 30 minute chunks.

The most important part of this was at night. My old ritual was to put the kids to bed and then have a big glass of wine (or two) with my ass on the couch. That’s a lot of calories to consume and then sleep on. My new ritual was to cut out the nighttime wine (I still have a glass with dinner), and to not eat anything within 3 hours of bed. I started out riding an exercise bike after the kids went down. The bike has some nice variety of 30 minute programs which I could do while watching Dr. Who.

I also joined a gym. I picked a Planet Fitness that’s close to my work. It’s super cheap at only $10 a month. I go there for a quick 30 minute work out before lunch or in the morning. They have this thing called “The Arc” which became my favorite. I also do strength training. You can burn more calories with more muscle mass so don’t skimp on the weights and do nothing but cardio.

Keep in mind I’m not planning on doing a marathon or anything. I’m not pushing my body to it’s limits on this stuff most of the time. I’m just raising my heat rate to a point and getting a good sweat going. That’s all it has to be. Make it a part of your life, like brushing your teeth.

Diet

I have not been following any particular diet. I kind of pick and choose what I want. The main goal has been to simply make healthy choices and do so all the time. If there has been one diet that I’ve followed more than any other it would be the “mediterranean diet”. This is pretty easy for me because I love greek and middle eastern food. I also allow myself to eat as many raw fruits and vegetables as I want. You can’t get fat on raw apples...trust me. Here are some of the guidelines:

Things that are “out”

  • Large Portions: I think this is the biggest thing. American portions are huge. A good rule is, don't ever go back for seconds. If you're still hungry after the first then eat an Apple or some other raw fruit or vegetable.
  • Meat: I’m pretty much a vegetarian (often vegan) before dinner. Even then we probably only eat meat 2-3 times a week and red meat is even more rare.
  • Carbs: I’ve swapped out american bread for whole grain tortillas, pita bread and flat breads.
  • Potatoes: This used to be a staple of our diet. Now we eat them rarely.
  • Ice Cream: nope. Well, maybe a spoonful when I give the kids some.
  • Cheese: drastically cut back and eating more low fat and fresh cheeses than things like cheddar.
  • Fried food: Get a good grill instead.
  • Pop: If there was just one thing I would tell you to quit entirely it’s pop/soda/coke. It has zero nutritional benefit. The diet crap is no better for you...actually it’s probably worse. Just quit it all together, you’ll feel better.
  • Crap in your coffee: same as pop.
  • Cakes & Candy: hey if your going to your niece's birthday party, go ahead and have some cake. If you're at the grocery store and eyeing the zingers, then cut that shit out.

Things that are “in”

  • Breakfast: It’s very important to get your metabolism moving in the morning. I have a bowl of regular Cheerios with some blueberries or a banana.
  • Hummus: I freakin love this stuff. Make or buy it made with olive oil.
  • Greek Yogurt: Get the good stuff like Chobani. It’s 0% fat, 0% cholesterol, almost pure protein and is indistinguishable from a stiff sour cream. You can use it in cooking to replace all kinds of stuff like sour cream, heavy cream or mayonnaise. The fruit versions are great for lunch or breakfast and the kids love it.
  • Nuts: You do still need fat to live and nuts are a great source of good fats. Peanut butter is a must but also just raw nuts. Planters makes a good snack pack of “heart healthy” nuts you can get.
  • Raw Fruits and Vegetables: You should eat some at every meal. We go through apples by the sack. We also eat a ton of pears, baby carrots, and celery. Your system needs lots of fiber and this is where you should get it.
  • Avocados: Another great source of “good fats” and all kind of other good things for you. I’ll eat them raw, made into guacamole and eaten with pita bread or tortillas, or on top of pizza or salad.

A note on buying fruits and vegetables: We have four kids and with this diet we go through a lot of produce. If we were shopping at the regular grocery store we would go broke quickly. I highly suggest that if you want to do this to go join someplace like Costco or Sam's Club. You can get a lot of produce for not much money.

Good Luck!

Color Themes Under Glare

There was a spirited discussion today about the effect of bright light glare on application color themes. A lot of discussion about solorization and saturations and such. My point in the discussion was that all of that is irrelevant because the main factor is not the sun on the colors. It's the color's impact on the glass screens reflection of the light. Dark themes turn screens into mirrors. Ever noticed that you can see your face reflecting back at you in a dark screen but not a light one? Well the same happens with the sun.

Here is my unscientific experiment. Same screen with Intellij put into a dark and light screen and angled such that a light is reflecting back at the camera:

Dark

dark theme

Light

dark theme

Partial Class Syndrome

The other day I ran into a smelly code scenario. Not only was it smelly, it was eerily familiar. I've run into this several times in this particular codebase and I finally put a name on it. "Partial Class Syndrome".

Now anyone familiar with C# knows that you can split one class over several different files by using the partial keyword. It's really a pretty horrible thing to do there is a very very limited scope for it being a good idea. Generated code like web service stubs are often partial so you can add to them without extending them. Other than that partials are super crappy. They make code hard to read and understand and they encourage classes to get way too big. In fact a partial sometimes shows up in code when a class gets so big that people want a quick and dirty way to make it look smaller.

The Boys

Anyway this project is Java and so can't do partials. At least you would hope not. Yet there I was looking at three classes. We can call them Larry, Moe and Curly. These three were all basically the same class with some different methods. They had the same dependencies, they took and returned the same classes, did similar things and even had similar names. On top of that they were all held by a big model class that used them interchangeably calling one and passing it's data into the others.

So here I was working in Moe and finding that I needed the functionality of Curly. I was also getting confused about which class did what due to the similarity.

The "fix" of course was to:

  1. Merge them together. This resulted in a pretty huge class which I was uncomfortable with but at least it solved the ambiguous confusion.

  2. Simplify the model's use of the code to just let the new big class handle the back and forth with it's own methods. This actually resulted in a lot of methods being removed or made private.

  3. Extract smaller specialty classes that deal with unique things. I'm still doing this step. This is always the hard part but if you look carefully you can find the classes hidden in there. Pay particular attention to feature envy.

I ended up with a single class that is smaller than the three from before and something that's easier to read and understand. It’s still too big for my taste but It’s better than what was there before...at least for now.