6 different ways professional software engineers in the same team solved the same problem

Roger Creyke
3 min readMar 26, 2021

--

A team of professional software engineers were given a problem to solve by implementing a previously defined interface.

Here’s how they each solved it.

Problem

Implement a function which takes an array of strings as an argument and outputs another array of strings which contains the same strings but ordered based on the following constraints:

  • Strings which contain only a delimiter (‘-’) with a valid integer on either side such as “12–1” should be ordered last, and should be ordered first by the first number, and then by the second number.
  • Remaining strings such as “Any Other Score” should be ordered first and alphanumerically ascending.

Test Data

The engineers were provided with the following example test data with which to (partially) validate their implementation.

Example Input

{ "1-0", "0-0", "3-2", "10-1", "Any Other Score", "1-10", "5-5", "0-1", "1-1", "1-2", "1-3", "2-0", "2-1", "1-4", "4-0", "4-1", "6-1", "7-0", "7-1", "8-0", "8-1", "9-0", "0-10" }

Example Output

"Any Other Score", "0-0", "0-1", "0-10", "1-0", "1-1", "1-2", "1-3", "1-4", "1-10", "2-0", "2-1", "3-2", "4-0", "4-1", "5-5", "6-1", "7-0", "7-1", "8-0", "8-1", "9-0", "10-1"

Interface

The engineers were provided with the following interface for the implementation.

Solutions

Here are each of the solutions, along with a brief explanation from the engineer as to how they approached the problem.

Solution A

I  tried to implement the entire process in a single linq statement without using anonymous functions.

Solution B

The general idea was to create a tuple to have ordering of processed values without losing the original values.

Solution C

I started off by *duplicating* the values so I can preserve the original value of one, but also manipulate the other, without having to reconstitute it back into it's original form.I did this by creating a dictionary, keyed by a Tuple(Home and Away scores) by splitting on "-".Any values that don't return a successful split are given a default *no-order* that appear at the start of the list.Once I've got those home and away keyed tuples, It's simply a case of doing a linq orderby Home team, then by the Away team.

Solution D

The issue with ordering strings containing numbers is that 10 comes before 2However in hexadecimal 2 comes before A, so I converted the numbers to hex, did a straight sort, then converted back to integers.Note!  This only works up to scores 15-15

Solution E

I implemented my solution this way to play around with the IComparer interface, which was unfamiliar to me but which made the call signature quite clean:It was written in an overly verbose style with heavy usage of variables in an attempt to increase readability, and makes use of a switch expression with a tuple to determine the result of comparing the two scores.This tuple switch expression is perhaps not the most obvious way of comparing the results, but overall I believe the code to be quite clean and extensible(if required).(Since originally sharing with the group I've added extra validation to the IsScore function and increased the re-usability of the function by allowing a string to split on to be specified in the constructor).

Solution F

Goal: Minimal code

Observations

  • There are many different ways to do solve the same problem with code.
  • No one solution was obviously superior to all of the others.
  • Code reviews and peer programming help all levels to learn.
  • Business requirements rarely consider performance, maintainability etc.
  • There is a trade off between maintainability and performance, and succinctness and elegance.

--

--