Rubber Duck Method

Just about every day I come across a new phrase or I have a “ahaa!” moment in my learning. Today both happened.

Rubber Duck Method

This was mentioned in our Slack group and I had no idea what it was so I looked it up. I grabbed this from Wikipedia

In software engineering, rubber duck debugging (or rubberducking) is a method of debugging code by articulating a problem in spoken or written natural language. The name is a reference to a story in the book The Pragmatic Programmer in which a programmer would carry around a rubber duck and debug their code by forcing themselves to explain it, line by line, to the duck. Many other terms exist for this technique, often involving different (usually) inanimate objects, or pets such as a dog or a cat. 

Wikipedia

So, the idea is if I can explain what the code is meant to be doing to a rubber duck (or cat in my case) then chances are I will hit upon the issue that needs to be debugged.

That is a brilliant idea. I am one step behind that though. After learning JavaScript for the last 4 weeks I think I will be happy if I could just read through the code and know what it meant!

This is how I have been learning. I look at a piece of code and try to figure out what it means and why it has been put together the way it has in the hope that by doing that I will then know when to use it. That’s my logic anyway.

As an example:

I have been following videos to help me put together my code for my second project. Its a game. This works but what I need to do now is understand why it works, why it has been put together that way and why certain code has been used. Once I can do that I will then know when I can use it next time.

So, I know this is a function()

I understand the if / else statements

I am also clear on the variable comChoice and if the computer chooses 1 the output is human, if it chooses 2 the output is plant, otherwise the output is soil.

I understand that return will show the result.

The part I am still getting to grips with is the

for (let i = 0; i < 1; i++) {
comChoice = Math.floor(Math.random() * 3) + 1;

Still working on learning what all of that means.

My Ahaa Moment!

I am working on my second project and my mentor helped me turn a lot of if/and statements into an operation instead. So, I am working on a rock, paper, scissors game – only I have changed the name. I call it The Cycle of Life and use human, soil and plant as my options.

So, here are the rules:

I had a lot of if / else statements that read if player choses human and computer choses plant then player wins because human beats plant.

I had to go through and write statements for each one of them. If at any point I decided to add more to the game e.g. environment and animals, my code becomes a very long list of if / else statements – it all gets too busy.

I recognised this but wasn’t sure of a better way so my mentor explained one to me using conditionals and instead of a long list of statements it is nice and short:

using conditionals javascript

Now I can just add any new items in the const winOrder list with no issues.

Plus I only needed three if statements – one for the winner, loser and if it was a draw. That will always stay the same.

Adding Options

I am still trying to decide whether I add another couple of options so it mimics rock, paper, scissors, lizard, spock!

The Cycle of Life game, project two, is here:

https://todiane.github.io/cycle-of-life-p2/

It isn’t due for submission until 10th August so I have time to add to it.