Andy Crouch - Code, Technology & Obfuscation ...

Automate The Small Stuff

Photo: Unsplash - Franck V

Sometimes as part of your regular development process, you have tasks that you repeat a lot. Or you have long commands or groups of commands to run that need a lot of repeated typing. When working with teams I’m often surprised when they do not live by the mantra of “do it once manually, do it twice, automate it”.

Modern development environments need the use and knowledge of the command line (cli). This means it is easy to automate every repeatable task imaginable. When should you automate a task? When:

  • You do it more than twice.
  • You do it infrequently.
  • It requires multiple steps.
  • When it is easy to get wrong and where the result is not easily reverted.

As an example, I am currently working on a project that is using a lot of uuid’s. For testing and seeding purposes I am generating and pasting a lot of these. So I created the following call:

echo $(cat /proc/sys/kernel/random/uuid) | xclip -selection clipboard

This command generates a uuid and adds it to the clipboard. This means I can flick between my terminal and code and create and paste in a new uuid with only a couple of keystrokes. It means I do not have to use a browser-based generator or my mouse or anything else that takes me out of my flow.

(As an aside there are a variety of packages on Linux that will generate a uuid such as uuidgen. The above approach requires no extra packages. Instead, it uses the kernel’s random number generator.)

For short snippets such as the above, I create functions that I can call in my shell. Often in projects, there is a possibility to automate a project-specific task. In these instances, I always create a shell script. The script is checked in with the project so other team members can utilise them. I find it can be helpful to use action names with these scripts such as “hydrate-database”. I wouldn’t use a technology-specific term so that the implementation doesn’t matter. As an example, on my project, I have created create-migration.sh and create-seed.sh. Although I have Knex installed globally, there is currently a bug which means that I have to use the project-specific version to generate files. So now I call:

$ ./create-migration.sh file_name

Instead of

$ ./node-modules/knex/.bin/knex migrate:make file_name

It may seem trivial but as your project grows it makes things easier. It also means that you don’t have to think too hard about tasks or perform repeated tasks that slow you down or break your concentration.

I’d love to hear about the ways in which you automate the small stuff so please contact me via twitter or email.