Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TU Delft R Cafe | Refactoring Code

Simple refactoring techniques to make R code easier to read, maintain, and share. An exercise for experiencing 'code refactoring' at the TU Delft R-Cafe.

Purpose

The code in this repository was developed with the intention to provide a quick way to generate maps for the Map Chanllend in 2024. In which, volunteers produced an atlas with maps portraiting a variety of themes. A template was created to quickly to help vonlunteers to prepare their maps. Volunteers used the template to write code that generares maps, most of the code and data was compiled into the reposotory.

The approached worked well to produce the maps, however, the maintainers of the repository realised that code was repeated in many places, commented code was left, and overall the code as a whole was hard to read and maintain. How would the code in the repository look like if the goal was to create a program that generates all submitted maps with a few lines of code? How could the code be provided so that code repetions was kept to a minimun, and still map makers could easily add their maps to the repository?

For simplicity, only a few maps are provided as examples. Now, it's time to clean up the code and make it more maintainable.

Hands-on

  1. Start a new RStudio project using Version Control -> Git. Copy the URL of repository into the Clone Git Repository dialog, and click Create Project.

  2. Follow the steps below to refactor the code in this repository. Do as much as time allows. A more detailed explanation of the steps is provided in REFACTORING.md.

    • Write approval tests. A tests that runs the code for three maps is provided in test.R. You can run it with testthat::test_file("test.R"). Make sure it passes before starting refactoring.
    • Reduce clutter. Remove commented-out code, unnecessary print statements, and any redundant code.
    • Reduce Cyclomatic Complexity. Break down large functions into smaller, more manageable ones. Each function should ideally do one thing.
    • Compose methods. Combine related functions into classes or modules to improve organization and readability. Rename functions, variables, files, directories to better reflect the code's purpose and behavior.

Tip

If you don't know where to start, you could:

  1. Take a few scripts and work on declutter.
  2. Create general-purpose functions to be reused across maps.

Learn More

About

R cafe: refactoring code

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages