I made an attempt to the Toy Robot Challenge using Ruby as the programming language and RSpec for test driven development. Here’s how I approached it. The challenge instructions can be found here. The reference to my GitHub repo for this challenge can be found here.
- The design - Structure of the code
As a starting point I decided on the components that would be required:
- Table
- Compass/Directions
- Robot
- Following this I worked on each of the components breaking them down further into smaller components/properties and following the TDD methodology. I worked on the problem in the following order:
- Table
- it has a table (Class)
- it has a width and height of 5
- it’s width and height could be changed
- Compass
- it has a compass (Class)
- it contains directions: N/E/S/W
- Robot
- it has a robot (Class)
- it needs to know it’s ‘place’ or coordinates
- it needs to know the commands
- it needs to report it’s position
- Challenges/Learnings:
-
RSpec: I am still quite new to RSpec. It was difficult initially to break the test down into smaller components and later with the readability aspect. It was best to write lots of tests to break the problem down and keep the code in lib/ simple rather than the other way around.
-
Coupling of Classes: Knowing which classes need to know about one another and at what time. It is tempting to get the classes to know each other at the start but it’s best to keep them separate until necessary.
-
Creating components: Keeping things simple is best. I initially thought of having a separate Commands class however things got complicated quickly and Robot class would be tightly coupled with the Commands class so it was best in this instance to incorporate Commands into Robot itself.
-
Private methods: Something new I learn’t was that RSpec cannot test the content of Private methods. Also, only the class in which the private method is contained can know about it.
-
Ruby’s built in methods: Using Ruby’s built in methods can be very helpful such as the .freeze method which was used in the Table class.
-
CLI: I am yet to attempt the CLI. I need to do a bit more research into this before I can attempt it.
comments powered by