10 Top Tips For Becoming A Better Coder!

October 15, 2015
By

(This article was first published on Mango Solutions, and kindly contributed to R-bloggers)

catgrammer

By Steph Locke

We polled our consultants for their tips on how to be more effective at writing R code; and here are the top 10!

10. Don’t pre-emptively tidy
Spending time on formatting as you go along gives you a cause to procrastinate plus it’s hard to make things consistent.

Instead of worrying about it, let a linter like formatR do it for you and then do any minor tweaks required.

9. Use an IDE
You might be hardcore if you code directly in the R GUI or in Notepad but Interactive Development Environments (IDEs) like Rstudio make it much easier to write code quickly. The code-completion alone will save lots of time.

8. Learn your hotkeys
There’s a never-ending list of hotkeys for your OS, and for IDEs like Rstudio. The amount of time you can save by never taking your fingers off the keyboard really mounts up so `Ctrl+S` this tip to your memory.

7. Plan twice, code second

Making sure you understand thoroughly what you’re trying to achieve will make coding it much easier and quicker. Doing things like writing your comments first can really help (and give you a taste of your progress).

# Step 1: Get data
# Step 2: Clean up data issues
# 2a: Remove excess rows
# 2b: Correct case issues in columns x,y,z
# Step 3: Apply glm function
# Step 4: Output validation components

6. Don’t be afraid to google
Building everything from first principles is reinventing the wheel. You may learn a lot but you take longer and can’t guarantee you’ve done it the most sensible way.

See if there’s a canonical solution, some common pitfalls, or simply some useful things to consider.

5. Don’t Repeat Yourself (DRY)

You’ve probably heard this before, but with the ease that R can make functions, split repetitious code into it’s own functions.

Check out the *apply functions as well to help avoid C&P.

4. Use the right tool for the job
Don’t let R become your proverbial hammer. Evaluate the needs of a project and use the appropriate language – a little bit of learning now can save a lot of pain later!

3. Write to facilitate tests
You should be testing your code, and to make that as quick as possible it has to be easy so when you write code, incorporate components like verbose outputs and plenty of validation. Avoid building functions that have “side effects” and putting things into the global environment.

2. Document as you go
Documentation rarely happens if left to the end – so as you’re writing your code, take the time to pen the vignettes and the roxygen headers as you go along.

1. Source control
Be the bigger Git!

Using source control (Git or SVN) helps you maintain your versions of code, and allows you to develop in a team more effectively.

Conclusion
We think that code that’s tracked, well documented, and easily tested is super valuable. Taking the time to do these things will set you apart from the pack and “Future You” will love you for it.

To leave a comment for the author, please follow the link and comment on their blog: Mango Solutions.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)