New year, new me.

We've all heard it before, right?

Just before the end of every year, family, friends and colleagues start to chant those four words.

For many of them, that phrase fizzles out and fades away in a matter of days. Or weeks, depending on who says it.

But for a very special few, the words resonate and they're able to achieve their goals by the end of the year.

What does "New year, improved code" mean? As developers, we very rarely look at our code and say, "This won't work. Let's trash it and start over."

Because that's just unrealistic.

We have an agile work methodology as well as clients, deadlines, family and other interests that occupy much of our time. More than likely, our strategy is going in the right direction. The execution, on the other hand, may need tweaking.

Instead of writing new code, we can simply improve our existing code. Improving our code will make it reusable, maintainable, and most importantly, more approachable.

With that being said, here's the top three "improved code" resolutions that will go a long way in our journey as craftsmen and women:

  1. Abstract whenever possible
  2. Be more concise
  3. Fix grammatical errors

Abstract

Abstraction, in terms of your application code, should be taken advantage of whenever you see the possibility of reusing a majority of the code you've just written elsewhere in your app. This has the added benefits of saving you time, causing less confusion and keeping functionality in a centralized location.

Not to mention, your fellow developers will love you and perhaps buy you a beer when using an approach such as this.

Be more concise

My wife, Ashley, is a former journalist. She nags me when I write posts and use too many words. The same can be said for snippets of code.

Lately, I've been working with a lot of form elements.

When trying to serialize a form, my code was way too complex. Essentially, all I needed to grab was some type of identifier indicating what data should be submitted.

Here's an example of my improved approach when solving for this problem:

~~~ javascript /**

  • Serialize a form object

  • @param {HTMLFormElement} form

  • @return {object} a simple, serialized representation of an HTML Form

  • @example:

  • // form =>

  • getFormElements(form)

  • // => {firstname: "Tom", lastname: "Ford"} */ function getFormElements(form) { var formObject = {};

    for (var i = 0; i < form.elements.length; i += 1) { var element = form[i] || {}, hasName = element.hasAttribute("name");

    if (hasName) { var name = element.getAttribute("name"); formObject[name] = element.value; } } return formObject; } ~~~

The piece of code above does the following:

  • Creates an empty object
  • Iterates through the form's elements
  • Checks if the element has the name attribute present
  • Adds the element to our form object making it ready for other actions/submission

Being able to get your point across in as few words, or, in our case, as few lines as possible will help make things clearer and easier to comprehend.

Everything should be made as simple as possible, but no simpler.

Albert Einstein

Grammar

Again, my wife gets on me about grammar too. But in the land of software development, syntax is our grammar.

Things like the following should be an absolute no-no:

~~~ javascript if(typeof check != 'undefined') return runAction(); ~~~

To me, this looks like a run-on sentence with an incorrect conjunction. If you're writing a real sentence, you'd use the necessary punctuations and spacing to complete your sentence, right?

The equality operator is, for lack of a better word, slack. It is the equivalent of using an ampersand (&) rather than "and."

I'd prefer to see something along the lines of this:

~~~ javascript if (typeof check !== 'undefined') { return runAction(); } ~~~

This just feels complete and reads the way a sentence should be read. Beautiful!


So, instead of jumping on the "New year, new me" bandwagon this year, let's not forget the things we've learned from years past. This year, I want you to say, "New year, improved me."