SIX WAYS TO TIGHTEN UP YOUR CODE

A colleague, Andrew Chernauskas, recently sent me a link to a blog post on InVision’s site, titled “6 Ways to Tighten Up Your Designs.” As a developer who leans heavily on GSATi’s very talented designers for anything design-related, I found this an extremely useful 5 minutes of my time.  If you haven’t tried InVision, give it a go.  It is an awesome prototyping/collaboration/workflow tool that GSATi’s designers are putting to great use.  But Andrew didn’t stop with sharing the post.  He challenged me to do the same for development. 

So, with apologies to Andy Orsow at InVision for blatantly ripping off his idea and format, here are my “6 Ways to Tighten Up Your Code”:

 

1) Remove braces within braces.  If you have lots of nested braces (from loops, conditionals, etc.) consider separating some of them out into functions.

 

2) Check your alignment.  Use consistent indentation.  If the original developer used three spaces for indentation, don't start using two for just the eight lines you're adding.  Set your editor to replace tabs with spaces, so that your alignment is readable when viewed by someone whose editor is set to a different number of spaces per tab.

 

3) Check your hierarchy.  If you're writing a complex piece of functionality, how you name and organize your functions makes a big difference.  Try to have a function for each important logical operation, so you only have to think about one thing at a time.

 

4) Use objects.  Encapsulation keeps you from writing fragile spaghetti.  Inheritance lets you reuse code safely and logically.  Polymorphism increases flexibility and reduces cut-and-paste code.

 

5) Simplify your language usage.  Sure, it may be cool to use every little corner of the language you're writing in, but unless there's a legitimate reason to do this one operation using totally different techniques than you've used everywhere else, be consistent.  It reduces the mental baggage you (and anyone maintaining the code later) have to carry.

 

6) Try the one-page rule.  If a function won't fit on your screen, consider breaking it up.  Some functions (Drupal’s hook_menu, for example) can't reasonably be broken up, and are just naturally long.  But many functions can.  And don’t cheat by putting your 27 inch monitor into portrait mode. 

 

Hopefully these tips will provide some guidance the next time your code needs some maintenance. Have your own Top 6 ways to tighten up your field? Let us know in the comments below. We'd love to hear from you!