Scripting (part 2) – learn it properly

In other articles, I discuss why scripting is important, and which languages to use. This one is largely an exhortation to learn it properly – just as you are wasting your time doing things manually that could be scripted, you are wasting your time if you do not make the most of this vital tool.

A good administrator will spend a lot of time reading and writing in their chosen script language. It is simply unprofessional not to be fluent in that language: not necessarily a guru, but able to write idiomatic code, and with a decent knowledge of what functions are available.

Personally, my favourite language is Perl - this comes with a reputation as “tricky”, but it is amazing how some people will try to get by without even reading a primer - and then they complain that they find the language baffling or treacherous. The same is true in most languages, though perhaps Perl is less tolerant than some.

Don’t waste your time being ignorant. If you had a new calculator and had learnt the ‘plus’ key, then needed to find 2 multiplied by 5, you would not type 2+2+2+2+2, you would look for the ‘multiply’ key. Too many people do the equivalent in programming.

Of course, most scripting languages are designed so that you can start simple, and add new techniques to your repertoire as they are needed. Do start with the basics, but do not rest there – you should probably aim to read at least one book on the language early in your learning. Even if you don’t fully remember everything in there, it will give you an idea of what is possible so that you don’t carry on in blissful ignorance unaware of how you could improve.

You should aim to get a sufficient grasp of your chosen language at least to be able to:

  • Read and understand moderately complex code written by others
  • Structure your code into functions & subroutines where necessary
  • Import and use modules/classes written by others
  • Separate parts of your code into modules/classes for re-use
  • Avoid unnecessary calls to external programs – opening a system command to run cat is a poor alternative to reading a file directly.
  • Do basic error handling
  • Handle command-line parameters and inspect environment variables

Some of these may seem a bit “over the top” at first, but it’s amazing how often a one-off script gets reused and needs to be modified to extend its functionality or behave slightly differently depending on the parameters.  If you routinely structure your code well and check for errors, you need not be ashamed when someone else comes to read your code… and you are less likely to store up trouble for yourself when you reuse the script a few months later.

You should also learn your editor(s) and other tools - powerful editors like vim or emacs have many features that will repay your learning time manifold. On Windows, you should find better options than Notepad. Remember that you may not always be working on your own machine, so at least get basic competence in standard editors – you should be able to work without code completion, auto-indenting & colouring, etc. But on your own machine use the best editor (or even a full IDE) for you, whatever helps you to write code quickly and without errors.

All this is basic stuff. Many of the advantages of scripting as discussed earlier rely on it being a tool you can use quickly and confidently. Don’t rely on always following sample code from someone else.

Last statement of the obvious: learn to type properly.

Leave a Reply