Skip main navigation

New offer! Get 30% off one whole year of Unlimited learning. Subscribe for just £249.99 £174.99. New subscribers only. T&Cs apply

Find out more

Optional: Hands-on code review activity

Pseudocode is a plain language, syntax-free code that describes algorithms in a way that is closer to human language than machine language.

In this optional step you are introduced to using pseudocode, a plain language, syntax-free code that describes algorithms in a way that is closer to human language than machine language.

If you would like to do this exercise, please do some research into what pseudocode is first.

The task

Below, you are provided with a simple example of a small code function in both Python and pseudocode, with a checklist of common coding issues.
Working in pseudocode, review the code and identify any issues using the checklist.

Purpose

Doing this will help you gain hands-on experience with code reviews, understanding some of their benefits and challenges.

Don’t worry if you don’t find all the errors in this exercise – you will find a full explanation in the next step.

Pseudocode Review

Here’s a simple function, in the cell below, written in Python.

def calculate_area(length, width):
if length <= 0 or width <= 0:
return "Invalid input"
area = length * width
return area

Here’s the same function, written in what we call pseudocode. 

(for more about pseudocode, see here)

// This function calculates the area of a rectangle.
FUNCTION calculateArea(length, width)
// Check for valid input: positive length and width values.
IF length <= 0 OR width <= 0 THEN
RETURN "Invalid input"
END IF
// Calculate the area by multiplying length and width.
area = length * width
// Return the calculated area.
RETURN area
END FUNCTION

Here’s a checklist of common coding issues:

  1. Logical errors
  2. Naming conventions
  3. Missing or incorrect comments
  4. Inefficient algorithms
  5. Unhandled exceptions or edge cases

Now, let’s look at a slightly different version of the code. Spend a couple of minutes looking at the code…

FUNCTION Calculate_Area(length, width)
IF length < 0 OR width < 0 THEN
RETURN "Invalid input"
END IF
area = length * width * 0.5
END FUNCTION

Ok, so first off we’ll look at the code again, examining for:

 
1. Logical errors

This line is incorrect
area = length * width * 0.5
The correct formula is length * width without the multiplication by 0.5.

// Revised Pseudocode:

// This function calculates area of rectangle
FUNCTION Calculate_Area(length, width)
IF length < 0 OR width < 0 THEN
RETURN "Invalid input"
END IF
area = length * width
RETURN area
END FUNCTION

Let’s do the same again for naming conventions.

2. Naming conventions

The function name Calculate_Area should be in camelCase instead of using underscores, i.e., calculateArea.

Revised Pseudocode:

// This function calculates area of rectangle
FUNCTION calculateArea(length, width)
IF length < 0 OR width < 0 THEN
RETURN "Invalid input"
END IF
area = length * width
RETURN area
END FUNCTION

Now for missing or Incorrect Comment.

3. Missing or Incorrect Comments

The pseudocode lacks a proper description in the comments, and it is not clear what the function does. Adding more descriptive comments would improve readability and understanding.

Revised Pseudocode with Comments:

// This function calculates the area of a rectangle.
FUNCTION calculateArea(length, width)
// Check for valid input: non-negative length and width values.
IF length < 0 OR width < 0 THEN
RETURN "Invalid input"
END IF
// Calculate the area by multiplying length and width.
area = length * width
// Return the calculated area.
RETURN area
END FUNCTION

4. Inefficient Algorithms 

The given pseudocode is simple and efficient in calculating the area of a rectangle. No changes needed.

Now let’s compare a line of code from the above cell
IF length < 0 OR width < 0 THEN
with the code from our original pseudocode cell
IF length <= 0 OR width <= 0 THEN
Can you spot the difference?

5. Unhandled Exceptions or Edge Cases

Unhandled Exceptions or Edge Cases: The pseudocode doesn’t handle the edge case where the length or width is equal to 0. The condition should be length <= 0 OR width <= 0.

Revised Pseudocode:

// This function calculates the area of a rectangle.
FUNCTION calculateArea(length, width)
// Check for valid input: positive length and width values.
IF length <= 0 OR width <= 0 THEN
RETURN "Invalid input"
END IF
// Calculate the area by multiplying length and width.
area = length * width
// Return the calculated area.
RETURN area
END FUNCTION

This brings us full circle, and also to the end of our first Code Review.

Please reflect on the process and then move on to the next step.

This article is from the free online

Foundations of Software Testing and Validation

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now