10.5 Programming

In this chapter we have moved quickly, from scripts to functions via the tricky topic of algorithms.Not only have we discussed them in the abstract, but we have also created working examples of each to solve a specific problem:

  • The script 10-centroid-alg.R was introduced and demonstrated on a ‘polygon matrix’.
  • The individual steps that allowed this script to work were described as an algorithm, a computational recipe.
  • To generalize the algorithm we converted it into modular functions which were eventually combined to create the function poly_centroid() in the previous section.
    Taken on its own, each of these steps is straightforward.But the skill of programming is combining scripts, algorithms and functions in a way that produces performant, robust and user-friendly tools that other people can use.If you are new to programming, as we expect most people reading this book will be, being able to follow and reproduce the results in the preceding sections should be seen as a major achievement.Programming takes many hours of dedicated study and practice before you become proficient.

The challenge facing developers aiming to implement new algorithms in an efficient way is put in perspective by considering that we have only created a toy function.In its current state, poly_centroid() fails on most (non-convex) polygons!A question arising from this is: how would one generalize the function?Two options are (1) to find ways to triangulate non-convex polygons (a topic covered in the online algorithm article that supports this chapter) and (2) to explore other centroid algorithms that do not rely on triangular meshes.

A wider question is: is it worth programming a solution at all when high performance algorithms have already been implemented and packaged in functions such as st_centroid()?The reductionist answer in this specific case is ‘no’.In the wider context, and considering the benefits of learning to program, the answer is ‘it depends’.With programming, it’s easy to waste hours trying to implement a method, only to find that someone has already done the hard work.So instead of seeing this chapter as your first stepping stone towards geometric algorithm programming wizardry, it may be more productive to use it as a lesson in when to try to program a generalized solution, and when to use existing higher-level solutions.There will surely be occasions when writing new functions is the best way forward, but there will also be times when using functions that already exist is the best way forward.

We cannot guarantee that, having read this chapter, you will be able to rapidly create new functions for your work.But we are confident that its contents will help you decide when is an appropriate time to try (when no other existing functions solve the problem, when the programming task is within your capabilities and when the benefits of the solution are likely to outweigh the time costs of developing it).First steps towards programming can be slow (the exercises below should not be rushed) but the long-term rewards can be large.