skip to content rich footer

stevenclark.com.au

subscibe to the StevenClark.com.au rss feed

Use Comment Blocks Before Functions

When you write a function in any programming language there are a number of things you might want to get right within the code. Examples might be:

  • trying to have only one entry and exit in a function (although I’ve seen some contrary and sensible examples in Jeremy Keith’s book DOM Scripting)
  • properly indenting code to make it more readable
  • following naming conventions
  • testing the edge cases to see the function works as expected, and
  • not misusing recursion like a mad little russian doll gone AWOL (Absent Without Leave)

Also, for the love of my own sanity its really shit practice to have non-meaningful variable names like e, j and fp unless its a standard increment or something very obvious. Remember what is obvious today is tomorrows meltdown! It seems JavaScript and C are two languages where esoteric naming seems to have a cult following that spells guru.

But these aren’t the tips I’m thinking of this morning. Robert Nyman’s post on code assertions got me thinking about writing maintainable functions. I’d like to bring your attention to the humble comment block directly before the function you just wrote… its boring, its even a little tedious, but it will save you a world of pain when you go back to that function in 4 months time or for someone opening your code at a later date.

The importance of robust code that is maintainable can’t be over emphasised. Both internal and external documentation need to be a part of a professional programming regime.

So what might you put inside the function comment block… You might write:

  • the date the function was created and the date it was last modified
  • who was the author of the function
  • at least a very basic description of what the function does (rather just writing “this function gets stuff from the server”), and
  • the expected inputs and outputs the function handles (not to be confused with the description)

External documentation might provide further information about who has changed the function and why they did so. Did it have errors or did it get repurposed somehow so that it needed modification? Was there an upgrade to a newer technology or was there a security reason? These can be important artefacts to maintain and save your bacon. You might want to keep timesheets (and in again in German) or somehow record how long it took you to write the function so that you have a growing reference that enhances your ability to quote on future work.

I can see that internal documentation in a web based environment is a grey issue. After all, we’re trying to optimise our code and not bloat it with meaningless irrelevant dates and explanations. I think the career migration from smaller scripts into larger projects will see more emphasis on software engineering principles like quality and maintainability. That comment block can go a long way to improving these aspects of your function. In short, don’t write functions just so you understand them.

One Response to “Use Comment Blocks Before Functions”

  1. » Blog Archive » You Need More Than Unit Tests - StevenClark.com.au

    [...] going to break anyone’s day typing it in there. The next level of internal documentation are comments before each method or function. This should at least describe the function and it would be good to have the expected inputs and [...]

Social Networking

Keep an eye out for me on Twitter

About the Author

Steven Clark Steven Clark - the stand up guy on this site

My name is Steven Clark (aka nortypig) and my passions are business, web development, photography and writing. I have an MBA (Specialisation) and a Bachelor of Computing from the University of Tasmania. Currently completing a Grad Dip in Journalism, Media & Communications.

Photography

My photography is at Steven Clark Studio and my regular photo blog presents an ongoing stream of latest images at Walk a Mile in my Shoes and I'm working on a long-term photography project called the King Island Project.

Recently Reviewed Books

Site Supporters

Hosted by Brett Drinkwater at Tashosting who is always there at the other end of my every inconvenient question and technical crisis. Brett's local community support for us over the last five years is greatly appreciated.

skip to top of page

Currently Reading

Ansel Adams: The Camera

As the first of three parts of Ansel Adams Photography Series, Ansel Adams: The Camera begins by discussing the idea of visualisation in relation to photography. Ansel Adams is a master of his craft; this series has sat on my backburner for some time. Book 2 in this series is The Negative and it's followed up by The Print. In them Ansel outlines his philosophy of photography rather than trying to lay down a set of rules. This first instalment is a technical book that explains the good old fashion film camera.