Wednesday, April 18, 2012

Continuous Integration of Better Practices

Q: What’s the worst code I’ve seen recently?
A: My own.

Jeff Atwood posted this question in his 2009 blog post, “Nobody Hates Software More Than Software Developers.” After this semester at the Virginia Ball Center, I immediately see the value in his statement.

As I look back at the code that I wrote those few short months ago it seems foreign. One example of this is the following method:

public double calculateNeededAverage(double desiredGPA, StudentRecord record) {
double knownCreditsEarned = 0;
double unknownHoursAttempted = 0;
double totalCreditsAttempted = 0;
int unknownClassCount = 0;

for (ClassGrade x : record.getClassList()) {
if (x.getGrade() != Grade.UNKNOWN) {
knownCreditsEarned += x.getCreditPoints();
} else {
unknownHoursAttempted += x.getHours();
totalCreditsAttempted += x.getHours();

if (unknownClassCount > 0) {
double neededAverage = ((desiredGPA * totalCreditsAttempted) - knownCreditsEarned)
/ unknownHoursAttempted;
// Check is desired GPA cannot be reached
if (neededAverage > 4) {
log.log(Level.SEVERE, "The desired GPA cannot be reached, "
+ "throwing IllegalArgumentException");
throw new IllegalArgumentException(
"Desired GPA cannot be reached");
} else if (neededAverage < 0) {
// Any grade will meet desired GPA
return 0;
} else {
return neededAverage;
} else {
log.log(Level.SEVERE, "No unknown classes, throwing IllegalArgumentException");
throw new IllegalArgumentException("No unknown classes");

At the time, it was the best I knew how to do but now that I know better it looks awkward and clunky. Here is an example of code that I wrote very recently:

private void ProcessPictureAttempt ()
if (_photoAlbum.IsFull ()) {
PlayCameraClickErrorSound ();
NotifyListenersOfFullAlbum ();
} else {
ProcessPictureAttemptWithPartiallyEmptyAlbum ();

The new method looks much better. The individual tasks have been refactored out into their own methods and the code is therefore much easier to read. Still, I know that it is probably far from perfect. This isn’t a bad thing. On the contrary, I think that this single truth is the best thing that has come out of this semester.

So what has changed in these last few months to bring about this change in myself? It’s simple, really - I know more about the 
better practices of industry than I did then. Throughout the semester, the Computer Science team has been reading Robert Martin’s Clean Code. His book contains tips for becoming a craftsman of code by using better principles, patterns, and practices of writing code. A result of this is that your code becomes easier to read, more maintainable, and generally more robust. As I read through this book, I was able to immediately apply its lessons one by one and see an immediate improvement in my coding style. It feels very empowering to look back at week-old code and say, “This is not clean, and here is why.” Each day, I see my code improve incrementally with each chapter I read in the same way that I see the game improve with each consecutive release.

I’ve come so far this semester, but I am still not satisfied with my ability because I know that as I read more literature on the topic my ability will continue to improve. I’m currently working through The Pragmatic Programmer and will soon move on to Effective C#. The books that follow will be based on the ideas i discover in these texts. As I read these, code that I write today will feel outdated, yet this is a good thing; it shows that I am still learning and growing as a developer.

You may have noticed that I use the term “better practices” as opposed to “best practices.” This is because I always want to be on the lookout for new, better, “best practices” as they become available. This is the only way to stay on the edge and keep ahead of the curve.

When I look back at this semester, I think the most important thing I learned was how to learn. Learning how to apply lessons from books as you read them is an invaluable skill that I will use for the rest of my life. By continually integrating new skills and better practices, my code will continue to become cleaner and more robust. No one’s code will ever be perfect, but with each new idea that I integrate I move closer towards the impossible goal.

No comments:

Post a Comment