41 Pseudocode
Pseudo-pseudocode
The primary tenet of evolution is that natural selection has acted over a sufficiently long period that organisms have become better adapted to their environment. Traits of present-day organisms represent a collection of solutions to challenges that their ancestors faced. Evolution has produced organisms that excel at solving problems, to the extent that humans look to nature (e.g., for medicine) to solve human challenges.
Programming has existed since the birth of computers. With nearly 180 years of evolution1, programmers have faced numerous challenges that led to the development of vast coding practices designed to surmount these issues. For ecologists and data scientists, adopting some of these practices can also make coding easier. One practice that is almost never mentioned in applied coding is the use of pseudocode.
While the majority of readers of this book might not need to develop pseudocode in the traditional programmer sense, a accessible version—referred to here as pseudo-pseudocode—can be added to programming toolkits. In a nutshell, pseudo-pseudocode is a written description or sketch that could be shown to a colleague or relative to explain how the code is structured and what the code is doing.
Formal
Pseudocode is an informal, high-level way of writing computer program algorithms using plain, human-readable language mixed with basic programming concepts. It allows developers to outline logic and program structure without worrying about strict syntax rules, semicolons, or memory management.
The key characteristics of a pseudocode are:
- Human-Readable: Written in plain English (or any natural language) so both technical and non-technical stakeholders can understand it.
- Syntax-Free: Has no rigid compilation rules or syntax errors.
- Language-Agnostic: Translates easily into any actual programming language like Python, JavaScript, Java, or C++.
- Structure-Oriented: Uses familiar programming structures like loops, conditionals, and functions to maintain logical flow.
Examples
The example below is a developper style of the pseudocode is to find and print the largest of two numbers.
BEGIN READ number1 READ number2 IF number1 > number2 THEN PRINT "number1 is greater" ELSE IF number2 > number1 THEN PRINT "number2 is greater" ELSE PRINT "Both numbers are equal" ENDIF ENDAn example of pseudo-pseudocode from the pseudocode above
- Import or input two variables called number1 and number2
- Compare the values (three possible scenarios)
- Print to the console which scenario occurred
A more concrete example of pseudo-pseudocode to plot a linear regression between the height and width of trees
- Import data containing the two columns
- Clean the data if needed
- Rename the columns to
heightandwidth - Perform a linear regression and extract the regression coefficient, the intercept, and the adjusted R2
- Create the scatterplot, add the regression line, and add an inset in the top-left showing the equation y = ax + b (with a and b replaced by their estimated values) alongside the adjusted R2
- Export the plot as a PDF file
Tips and warnings
- Pseudo-pseudocode is not an official term; it is used in this chapter to illustrate an idea.
- Pseudocode consists of brainstorming and constructing the skeleton of the code before actually writing any code.
- Pseudo-pseudocode can be implemented in many ways. The most practical approach is to treat pseudo-pseudocode as the comments that will appear in the future code—writing comments is strongly advised.
- There are many ways to reach the same result. For instance, while most figures are created using
ggplot2, all figures in this volume use base R. Thinking at a higher level provides a clear conceptual understanding of the process rather than focusing strictly on specific syntax or functions. - Pseudocode is an effective format for sharing workflow logic with colleagues or advisors.
Calculated from Ada Lovelace’s 1843 publication of the first computer algorithm for Charles Babbage’s Analytical Engine↩︎