The time is ripe for New Years’ resolutions. Losing weight, running a marathon, learning cha-cha, reading 1 book a week and so on. And for programming? It’s time to boast about writing spotless code, with perfect readability. It’s time to take a pledge never to patch things up in production, never to do awkward quick fixes and never to stray from the teachings of Martin Fowler and Uncle Bob. Well… we have a better proposition. Instead of long-term commitments, let’s break it down to one tip a day.
The programming process
We all have our own programming process, using the tools and language(s) of our choice. And most of us are so very attached to this process, we rarely think about improving it. Furthermore, radical improvements come with steep learning curves. Such is the case of learning Vim or switching operating systems. Unfortunately, we are master procrastinators (Remember those 2017 New Years’ resolutions? 2016? 2015? Yeah, me neither.), so I’m not going to talk about any radical process improvements. I’m going to give you a list of 10 micro optimisations, the kind of things you can do immediately. No excuses. One micro-improvement a day. Starting now. Are you ready?
Tip #1. Aliases – write less
Here, at Algotech Solutions, we do not care that much for typing. We are big fans of the “less is more” approach, especially when struggling with common commands. Our dotaliases package contains various aliases you can configure in your terminal and save time by writing shorter commands.
Tip #2. Cleanup Git branches
Define this function in your terminal:
for i in $*; do
cmd=$cmd' | grep -v "'$i'"'
cmd=$cmd' | xargs git branch -D'
You can then use it like this: delete_branches_except master develop . This will delete all Git branches in your repository, except for those containing “master” or “develop”. This one is great for when you have an awful lot of branches to delete, but also a few ones you want to keep. It saves much time from picking and choosing what stays and what goes.
To better visualise your local and remote branches, I suggest using a GUI Git tool like SourceTree.
Small note: I’ve done a PR to add this to our dotaliases repository, so in a while you might find it there.
Tip #3. DRY your terminal
Forgot about sudo? Had a Byzantine failure? To replicate the last command, use the !! (two exclamation marks) shorthand. So instead of copy-ing and pasting the same command, or going up the command history once and moving your cursor to the beginning just to add a “sudo”, next time try sudo !! .!$ will reuse the previous command’s options. This means if you first cat file_path/with/really_weird-name.txt you can edit that same file using vim !$ to save yourself from Carpal Tunnel.
Similarly, remember that - ! While cd - will switch you back to the previous directory, git checkout - will take you to the previous branch.
Tip #4. View your timeline
These small date and time-related tweaks are great for those of us obsessed with time tracking, but also for debugging difficult failures using the time introduced as a clue.
To view the history of all commands ran in the Terminal, use the command history. Remember to add something like:
export HISTTIMEFORMAT='%F %T '
to your ~/.bashrc file to get the timestamps as well.
For Git, you can use git log with the date option, as presented in the docs:
My favourite is definitely the “relative” option, which helps a lot when clients or colleagues report errors arising “about an hour ago when I merged” or “a couple of days ago when deployed”. Believe me, tracking dates makes you a real Sherlock Holmes. Check out Sergiu’s “Git tips and tricks” article for even more of these goodies.
Tip #5. Write a Bash script
I’ve seen many programmers shy away from Bash files. If you have a set of commands that you repeat on a regular basis, maybe just slightly parameterised, please consider writing a Bash file to do your job for you. It’s easier than you think, and people have come up with the funniest Bash solutions.
Tip #6. Get inspired
Need a good .vimrc file? There are so many on GitHub! How about a .gitignore? There’s a website for that. Use Boilerplates as much as possible. Use the --help option and interactive manuals like vim tutor as often as possible. The gist is: it’s bad practice to reinvent the wheel and there’s always someone who know more than you do. Search before coding yourself and your life will be much easier.
Tip #7. Autocorrect for your code
PRO level: enforce adding these tools to your workflow. Use a pre-commit git hook to make sure non-conformant code doesn’t slip into the codebase by “accident”.
Tip #8. Prevent database accidents
Chuck Norris debugs in production, but you shouldn’t. However, when working on sensitive databases such as those on production servers, you can either:
a. Do everything as a transaction
b. Enable safe modes that prohibit users from updating without WHERE clauses (e.g. SET SQL_SAFE_UPDATES in MySQL or implicit_transactions in TransactSQL)
Double check JOIN types, data types, SQL flavours when searching for solutions to any database problem. Remember a bad move in production can take up a lot of your time, including fixing and explaining it to stakeholders. Better be safe than sorry, so enable your safety nets to save important time and effort.
Tip #9. Autoreload pages, auto run tests
Most front-end frameworks already do this, but consider autoreloading your web pages when working on the back-end of web applications as well. Here’s some examples of how you do it with Vim, or PHPStorm. Remember similar guides should exist for your favourite code editor.
The same goes for tests. Who wants to spend that extra time switching tabs and running the command for unit tests? I don’t. So configuring your code editor of choice to automatically run your tests will be a time-saving blessing. Let’s talk PHPUnit and PHPStorm, or this StackOverflow answers discussing the same tricks for Python and NodeJS! This Vim Package allows for test running directly from Vim, in many languages. Just open that Google tab and search for your combination of choice. It will certainly be worth your while.
Tip #10. Keep your focus
Probably one of the most important tips on this list is to stay “in The Zone”, avoiding all distractions from the outside world. Or, at least, limit them. I would start with turning off notifications on all social media. You see, when you keep interrupting your work to check new likes to your latest photo, it’s damaging to your train of thought. Remember that XKCD comic?
Now imagine that adjusted to our 2018. We have phones and tablet and browser notifications, and any message can reach us anytime from Facebook Messenger, WhatsApp, Viber, Telegram, Instagram, Snapchat, SMS messaging and who knows how many others? Turn notifications off and check them during breaks, or at home.
But what if there is an emergency? Really? On Snapchat?
For those lacking the self discipline of not constantly checking notification outlets, I recommend using a technique such as Pomodoro. Still not enough? Enforce those techniques using a browser plugin that restricts access to distracting sites, when active. Try Strict Workflow (previously called Pomodoro).
Do you have special techniques to improve your coding flow and keep your focus? We’d be glad to learn about them, so please leave us a comment below. For more programming tips, check out our article on best coding practices or read one of our recommended books for programmers.