Better Code with Less Typing
Choosing a good code editor for science
If an artist is only as good as their tools, then I spent years finger-painting, followed by years getting hand cramps from paint-brushes I couldn’t figure out how to hold correctly.
When I was in High School, I used to write HTML in Notepad. Then my friend showed me Notepad++ and I was astonished by how much easier it made so many tasks.
When I started writing in Python, I kept using Notepad++, which really isn’t suited for extensive code editing. Then I spent several years trying and failing to configure and master Vim and Emacs.
Years later, I discovered there was a middle ground between using rudimentary tools and spending days configuring esoteric editors.
Visual Studio Code or Pycharm
Both Visual Studio Code and Pycharm were created with Python in mind. They have features like:
- Syntax Highlighting Making code easier to read with colours differentiating constants, variables… etc.
- Code Completion Like your phone’s auto-complete, but for code.
- Tool Tips Documentation and hint pop-ups when hovering your cursor over a piece of code.
All enabled without any configuration!
They’re both good, but I have a slight preference towards Visual Studio Code, because it’s faster to start up. So the links and keyboard shortcuts in the following discussion will be Visual Studio Code specific.
Everyday commands you should know
In each editor, there are a few commands which are absolutely essential to leverage:
- Go to definition Find where a function was defined or where a variable was declared with
F12
. Faster and more accurate than scrolling or searching the full text withCtrl+F
! - Find all references Find where a function is used in your project using
Shift+Alt+F12
. Especially useful when you want to change a function's parameters without breaking your code in several places. - Rename symbol Rename a variable using
F2
, instead of using find-and-replace to and accidentally changing something unrelated! - Multi-cursor editing Although the find-and-replace functionality of Visual Studio Code is powerful, I sometimes find it hard to track which parts I’m changing. Instead, I like to use mutli-cursor editing, wherein I highlight the text I want to change and use
Ctrl+D
to spawn a cursor at the next occurrence. This is difficult to explain textually, so try it out for yourself!
If you have trouble remembering keyboard shortcuts, both editors have the Command Palette, which lets you type the full command name, instead of hunting through drop-down menus.
I use the Command Palette everyday. However, I also recommend the tried and true method of printing out a cheat sheet and taping it to the side of your monitor.
In the next post, we’ll keep building on the theme of small changes to coding habits and environment with significant benefits. Specifically, we’ll cover how to avoid bugs without running code and how to stop arguing with your collaborators over code formatting.