Scripting languages are incredibly useful for quick-fix scripts. The
problem with this is when the quick-fix script is still in place 6 months down
the line, at which point the corners you cut because, hey, it’s just a
short-term fix, come back to bite you. Read on for some best practice tips to
make the experience less painful.
(A note: my own scripting languages of choice are perl and bash, but the principles hold across other
languages.)
When you pass variables into a script or a subroutine, don’t just
use the default names. Give them real names
immediately – it’s much easier to keep track of what
you’re passing in and what you’re doing with it. A perl example:
sub concatenate_files { my ($firstfile, $secondfile) = @_; // rest of the subroutine }
And while we’re naming variables: name them something sensible.
Something that’ll be meaningful when you read this, say, tomorrow morning
before coffee. So, $filelist, not $fl.
Don’t hard-code anything. Put all your static values into variables, and
collect them all at the top of your script.
#!/bin/bash EMAIL=admin@example.com LOGFILE=/var/log/locallogs/mailscript.log
That way when something changes
(your email, the mailserver address) and the script breaks, the fix will be
a straightforward two-second job.
A perl-specific tip: the single most helpful thing you can do for yourself
is to use these lines in every script:
#!/usr/bin/perl -w use strict;
The -w flag turns on warnings. This means that if perl sees
something that it thinks looks dubious (e.g. variables mentioned only once),
it’ll tell you so.
use strict requires you to declare all your variables
before (or at least at) their first use. Thus,
my $newvar; // some stuff $newvar = 3;
will be AOK, as would my $newvar = 3. But $newvar = 3
without the declaration (or a local declaration) will cause an error.
This is fantastic for avoiding typos or variable name brain-blips: if you
declared $newvar and then use $newcar a few lines down,
you’ll be warned.
Ideally, most of your code should be crystal clear without comments (this
is where your variable names come in, for example). But having at the least a
comment up top saying what the script does, what input it expects, and what
output it provides, will be incredibly helpful in the future.
Within the code itself, if you’re doing anything remotely complex, a line
or two of comment never goes amiss. While you’re at it, consider whether that
section would be better as a subroutine.
There’s a lot that can potentially be said about testing, but here are a
couple of basic recommendations:
Scripts are great. They do many good things, and they’re surprisingly
extensible. perl is a bit better than shell for anything complicated
— really, if you’re writing more than about 10 lines, tops, or doing anything
much more complicated than stringing a series of shell commands together, you
should probably be using perl (or the alternative of your choice)
rather than shell.
But sometimes, what you actually need is a Real Program. If you find
yourself spending vast amounts of time on a particular script, or that it’s
getting steadily larger and more out-of-hand: stop, take a step back, and
consider whether you are applying a tiny Band-Aid to a gaping wound. Your
scripting language may still be entirely appropriate, but you’ll need a
different type of structure. Sometimes, the quick-and-dirty fix isn’t going
to cut it — it’s best to work that out ASAP.
So, that’s a quick run over a set of practices that should keep you from
cursing your own name unto the seventh generation a few months down the road.
Now go forth and implement them the next time you have a problem for which a
script is the obvious fix.
This article was first published on LinuxPlanet.com.
Ethics and Artificial Intelligence: Driving Greater Equality
FEATURE | By James Maguire,
December 16, 2020
AI vs. Machine Learning vs. Deep Learning
FEATURE | By Cynthia Harvey,
December 11, 2020
Huawei’s AI Update: Things Are Moving Faster Than We Think
FEATURE | By Rob Enderle,
December 04, 2020
Keeping Machine Learning Algorithms Honest in the ‘Ethics-First’ Era
ARTIFICIAL INTELLIGENCE | By Guest Author,
November 18, 2020
Key Trends in Chatbots and RPA
FEATURE | By Guest Author,
November 10, 2020
FEATURE | By Samuel Greengard,
November 05, 2020
ARTIFICIAL INTELLIGENCE | By Guest Author,
November 02, 2020
How Intel’s Work With Autonomous Cars Could Redefine General Purpose AI
ARTIFICIAL INTELLIGENCE | By Rob Enderle,
October 29, 2020
Dell Technologies World: Weaving Together Human And Machine Interaction For AI And Robotics
ARTIFICIAL INTELLIGENCE | By Rob Enderle,
October 23, 2020
The Super Moderator, or How IBM Project Debater Could Save Social Media
FEATURE | By Rob Enderle,
October 16, 2020
FEATURE | By Cynthia Harvey,
October 07, 2020
ARTIFICIAL INTELLIGENCE | By Guest Author,
October 05, 2020
CIOs Discuss the Promise of AI and Data Science
FEATURE | By Guest Author,
September 25, 2020
Microsoft Is Building An AI Product That Could Predict The Future
FEATURE | By Rob Enderle,
September 25, 2020
Top 10 Machine Learning Companies 2021
FEATURE | By Cynthia Harvey,
September 22, 2020
NVIDIA and ARM: Massively Changing The AI Landscape
ARTIFICIAL INTELLIGENCE | By Rob Enderle,
September 18, 2020
Continuous Intelligence: Expert Discussion [Video and Podcast]
ARTIFICIAL INTELLIGENCE | By James Maguire,
September 14, 2020
Artificial Intelligence: Governance and Ethics [Video]
ARTIFICIAL INTELLIGENCE | By James Maguire,
September 13, 2020
IBM Watson At The US Open: Showcasing The Power Of A Mature Enterprise-Class AI
FEATURE | By Rob Enderle,
September 11, 2020
Artificial Intelligence: Perception vs. Reality
FEATURE | By James Maguire,
September 09, 2020
Datamation is the leading industry resource for B2B data professionals and technology buyers. Datamation's focus is on providing insight into the latest trends and innovation in AI, data security, big data, and more, along with in-depth product recommendations and comparisons. More than 1.7M users gain insight and guidance from Datamation every year.
Advertise with TechnologyAdvice on Datamation and our other data and technology-focused platforms.
Advertise with Us
Property of TechnologyAdvice.
© 2025 TechnologyAdvice. All Rights Reserved
Advertiser Disclosure: Some of the products that appear on this
site are from companies from which TechnologyAdvice receives
compensation. This compensation may impact how and where products
appear on this site including, for example, the order in which
they appear. TechnologyAdvice does not include all companies
or all types of products available in the marketplace.