The Power of Code Snippets


Modern day programmers write a lot of code. That’s part of the job. But one of the main principles of writing good code is the DRY principle: Don’t Repeat Yourself (see The DRY Principle: R Functions or The DRY Principle: Python Functions or Wikipedia or any number of other online sources). In essence, this principle effectively states that if you are going to write the same code twice, then don’t; instead, write the code in a function, then call the function twice.

There’s also another principle, which is equally if not more important: Document Your Work. For this, function docstrings are extremely helpful. Basically, you want to write your code for someone else (even if that someone else is the future version of you…), so that they can understand your code better. Here, you want to explain why your code is doing what it is doing (the reader can see what the code is doing by simply reading the code itself). Such as if there is some complicated logic, or to handle a quirk in the data, then it’s best to write a docstring. But don’t go overboard though! There are many reasons why too much documentation can be a bad thing (such as How to Comment Your Code Like a Pro: Best Practices and Good Habits and Putting Comments in Code: The Good, the Bad, and the Ugly). It is the intersection of these two principles that gives rise to the use of a fantastic thing in the world of programming: Code Snippets. These snippets are effectively ‘saved chunks of code’, which can allow you to speed up the implementation of your code, and the overall readability of your code. And ultimately, will turn your code from Good to Great.

When approaching this from a Data Science perspective, I will focus on the two most popular coding languages in this field, and the popular IDE for each of them: R + RStudio and Python + VSCode.



Also Promoted Via

LinkedIn:

Facebook: