TDD Basics : Obvious Implementation
Given two variables a and b, exchange the values assigned to them.
Learn when to use Obvious Implementation instead of Fake it Till You Make it or Triangulation.
Problem Domain Analysis
In this phase, we define the problem clearly.
Before exchange, the initial values are : a = 1 and b = 2
After exchange, the final values are : a = 2 and b = 1
Solution Domain Analysis
It's very easy to exchange values in Ruby. Here is the irb session that shows how to swap the values of a and b using just one line.
$irb > a = 1 => 1 > b = 2 => 2 > a,b=b,a => [2, 1] > a => 2 > b => 1
Let's solve the problem in a way that allows implementing it in any language. This means we can implement the solution in languages that requires more work to swap values. Let a = 1, b = 2, here is an experiment for exchanging the values in the irb session:
$irb > a = 1 => 1 > b = 2 => 2 > a = b => 2 > b = a => 2 > a => 2 > b => 2
This means we cannot do the following:
a = b b = a
Because both a and b values end up with the value of 2. Our objective in this phase is to create a model and test the model to make sure that it works. Here is a model that fixes the above problem:
new value of a = original value of b new value of b = original value of a
This means we need to preserve the old value of a. Let's consider the following model:
t = a a = b b = t
Let's test this model in the irb session:
> a = 1 => 1 > b = 2 => 2 > t = a => 1 > a = b => 2 > b = t => 1 > a => 2 > b => 1
The revised model works.
- Save the original value of 'a' in a temporary variable 't'.
- Assign to 'a' the original value of 'b'.
- Assign to 'b' the original value of 'a' that is stored in 't'.
class Exchange attr_reader :a, :b def initialize(a, b) @a = a @b = b end def perform end end describe Exchange do it 'should swap the values of the given two variables' do e = Exchange.new(1, 2) e.perform expect(e.a).to eq(2) end end
This test fails.
Implementing our algorithm makes our test pass.
def perform t = @a @a = @b @b = t end
We did not use Fake It Till You Make It in this case because the implementation was obvious, so we just typed in the correct implementation. Fake It Till You Make It and Triangulation are helpful when you the solution is not obvious and you need to take small steps to learn more about the problem.
This problem is small. For big problems, you want to decompose it into smaller problems and solve the smaller problems first and combine the solution to smaller problems to solve the big problem. This divide and conquer strategy becomes necessary when the problem is unfamiliar or complex. Obvious Implementation is also called as Right Solution.
In this article you learned when it's ok to take big steps and not use Triangulation or Fake It Till You Make It. You can also see that the test code to the production code seems to be reasonable.
Ace the Technical Interview
- Easily find the gaps in your knowledge
- Get customized lessons based on where you are
- Take consistent action everyday
- Builtin accountability to keep you on track
- You will solve bigger problems over time
- Get the job of your dreams
Take the 30 Day Coding Skills Challenge
Gain confidence to attend the interview