What does a good code sample look like?

Here’s an example of a good code sample for entry-level programmers.

I review a lot of code samples, both for work and for pleasure. Grady Booch once tweeted that good coders read code, and I think he is absolutely correct. How can we improve our skills as developers if we don’t look around at other code to learn what’s new and helpful? So, I read samples, examples, tutorials, production code, research code, test cases, code in languages I don’t know, code from “top coder challenges,” and any other type of code I can find. I also review code samples from job applicants.

Often, the responses I receive when I ask for a code sample are “Really?” or “I don’t have a code sample. Can we skip to the interview?” I wish that was the worst of it: Over half of my job applicants ghost me when I request the code sample. This is especially funny since my job posts include a special section that says “We will request a code sample from you.” I believe this happens because people are busy balancing priorities or don’t know what a good code sample looks like. So here are my suggestions for writing a good code sample:

Distribute only code you’re allowed to share. Never give someone a sample of proprietary code that you wrote for work. Don’t even suggest it. Take a half hour or hour to write a good sample, push it to GitHub, and then finish watching Future Man, Star Trek: Discovery, or the ballgame that you had on while you slung it together. On the other hand, if you work on open-source projects for a living, feel free to submit one of your open-source codes and suggest reviewers run Gitstats or something to see your contributions.

Include five or more classes that demonstrate a good design philosophy. Show off how well you understand object-oriented concepts such as inheritance, realization, and delegation by creating a simple-but-thorough design with distinct classes. This shows not only how well you understand the concepts but also that you can design things well. Plus it demonstrates that you know basic things like how to call functions.

Do some I/O. Showing some input and output operations in your code demonstrates that you know not only how to handle those all-important functions but also how to use logic and loops. Both are very important language constructs that you will almost always be asked about.

Show some tests. Whether or not the job will require a lot of software testing, showing how much you know about testing only makes you look awesome. If you don’t know about testing, then “treat yo’ self” to the Wikipedia articles on software testing and unit testing.

Use Git. I don’t know how many teams use Git these days, but I’m willing to bet most of them do. As with testing, this is a technology you want to show you know something about regardless of whether or not the team uses it, but especially if they do.

Include a build system. This might not seem like the most obvious thing to include in your code sample, but it’s important because I need to know that you know how to build your code. To steal and adapt a line from the movie “Three Amigos:”

Well, you told me your code sample has a build system. And I just would like to know if you know what a build system is. I would not like to think that a person would tell someone he has a build system, and then find out that that person has no idea what it means to have a build system.

Is it ok to use an auto-generated build system from an IDE? If it can be used without starting the IDE, like a Makefile or Maven script, then yes. If not, no.

Document your sample. The single biggest problem I see with code samples is that there is no documentation. I don’t care how “self-documenting” you think your code is, I don’t have a clue what it’s supposed to do and you won’t in five years either. More importantly, API-level documentation and skills with Javadoc and Doxygen are crucial in modern development shops. No user off the street is going to know how to use char ** getVal(int a, const int & a0, const double & a1) const; correctly! Furthermore, did you include a README.txt or README.md file to tell me what functionality your code offers, who wrote it, and how to contact the author?

Make it pretty. Presentation matters. You’re probably worried about being dressed nicely to make a good first impression—shouldn’t your code also be “dressed nicely” to make a good first impression? Clean code is more readable than messy code and makes the review process easier.

Be transparent. Don’t lie or hide things about your code sample; put them out in the open, be transparent, and own it. If reviewers don’t like something in your code sample, adopt a growth mentality and ask them if they’ll let you fix it and submit it for re-evaluation. If you do something in your code sample that you know is wrong, document why you did it with something such as “A realistic implementation would replace this version that scales a random number from rand(), but this is sufficient for my code sample.”

I hope this helps you write a better code sample. I wrote a simple code sample that you can check out on the ORNL Training GitHub page. If you have questions or want to complain about either this article or the samples, drop me a Tweet at @jayjaybillings.