Outsource Amazon Product Sourcing to an Experienced Consultant

In need of Amazon product sourcing for your Amazon business? Looking for product opportunities on Amazon and suppliers of those products? Here’s a guide to outsourcing your Amazon product sourcing to…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




TDD With URM and Kotlin

First of all…

TDD is the acronym for Testing Driven Development. It works as a technique for building software based on writing the tests for the given software.

It’s worth to mention this technique does not solve all the problems it allows you to identify problems before they get too messy. Also, it’s a different way of writing code, which means probably it won’t make total sense at first sight or won’t stick too much with you. I think that once you get used to the process it’s easy to just flow with it.

Some friends complain about delivery times and the issue of TDD taking longer than just throwing a lot of code, one argument about this is that if something breaks in the future and you don’t have the tests for it, you would end up taking the same time fixing that as if you have taken the time to do TDD since the beginning. Still I think is worth the effort to learn why is so popular and try at least once — where once is a period of time bigger than 1 day.

TDD has only three rules to follow:

With these three rules you can follow TDD kind of easy. The whole process involves more than just follow the rules, it involves changing mindsets. For the rules to work, you need to think as 3 different roles:

This means you’ll be in a loop following the rules along with changing the current mindset. The whole process you would get use to, is here:

The whole process for getting into TDD works differently for each person, and on daily basis when you’re facing tight deadlines is also difficult to get used to it. Still, I insist: worths the try.

The project is a simple implementation of a working URM. The goal is to write a project capable of executing files in the URM notation:

The first thing we are going to build is the operations for the URM machine to work, this is the easiest part because we don’t have to worry about the internals of the registers or the parsing process.

The Z(x) function will be our first feature to write. The function will take a number that represent a register position and will put a zero on it. We will build this function to be invoked in our code, it should be easy, right? Well… I have some questions:

With this questions and answers we decide with a simple way of calling our function:

This way the function is not tied to a certain registry, it’s not dependant on the inner implementation of the URM, and we can test it without much things involved.

Our first test (in pseudocode) will be something close to:

This is pretty close to our first kotlin test:

Using gradle we should be able to run the test:

As we can see, we have a compilation error…

Hey! Wait a minute… before saying that, you should remember the rules of TDD:

So that means I couldn’t write the zero function before writing the test. But now we have a failing unit test, which means we are allowed to write production code!

Note: Production code can be a term that people understand as code that will be inmediatly on a server/app to answer for user interactions. This is not 100% true, production code is basically code that will be on the final project and satisfies part of the functionality of the project.

To write the code, let’s hear what the errors have to say:

This means we don’t have a zero function! Let's create one in the package: net.sierisimo.kurm.operations.Operations.kt

If we invoke gradle check again we get:

Which means we forgot to add the arguments for the function. So our function will end like:

I left something in the function… what’s the type of registry? Is an array? I don't think so, our program is the "Unlimited Register Machine", and arrays have a limit size. Is it a list? Could be, as long as the list is initialized or has a way to grow to certain size to hold empty slots. The real question is: what functionality does the registry has? well, it should be able to retrieve the value of a register in a given position and set the value of a register in a given position. Then a list could work but still has the issue with empty slots.

We decide that, in this precise moment, we don’t care about how the registry works, and because we don’t care about how it works — as long as it works — we can create a definition for that:

In that way, we can delegate later the responsability of the implementation. Now our first approach of the zero function goes like this:

Since we don’t care about the details of the registry itself, we can create an implementation on the test:

This can be a final implementation, but writing this final implementation will violate the rules of TDD. We stay with this local implementation just for our test now.

If we run gradle check now it shows:

Which means the project successfully compiled but our test did not pass. And we know that we currently don’t have a body in our function, let’s fix that:

And finally gradle check will show:

Our test is passing now. Now we should write other test to validate everything works as expected. For example, we know that URM works with positions in registers, but what happens to negavite positions? They are not valid positions… let’s write a test for it:

In this test we check that our function don’t work with negative positions. But also we said in the test we expect the function to throw an IllegalArgumentException. If we run the gradle check we will see this test is not passing, but the previous one is.

We fix the zero function adding Kotlin contracts:

Now if we run the check, both of our tests will be passing and we have for sure that even when we modified the way the function works internally it works as expected for different scenarios.

Before moving into the next function, we should check for more cases, instead of having one method per case we go for parameterizing the test:

We also take advantage of Kotlin support for function names with spaces using backticks to remove the @DisplayName and put directly the name in the function. And we can test our function just adding the values we want into @ValueSource(ints = []) without writing more test cases.

For now, we have understood/learned the basics of following the process of design-test-check-fix-repeat loop of TDD. All of this with a simple function.

There’s still too much to do, like creating tests for the other functions (increment, jump) but we are gonna take that for the next article.

Thanks for reading!

Add a comment

Related posts:

ProximaX Sirius Blockchain SDKs are now open!

We are delighted to announce that the ProximaX tech team has opened some of the stable builds of the blockchain SDKs (Software Development Kits). Head over to the wiki page of each SDK to get…

Introduction to React Components

One quick look at an HTML file in Google Devtools will remind you what a mess an HTML DOM can be. Components allow you to create code that is modular, reusable, readable, and predictable. But by…

Corner Lots Baseball

According to the dictionary the word ‘certain’ means known for sure or established beyond doubt. I like things to be certain in my life with no maybe, but I know that that isn’t always possible. Most…