On my GitHub repository, I submitted the initial code that covers all the cases except the edge ones. But, the code I’ve written is in bad shape.
To solve this kata, you have two tasks to do. Firstly, you need to refactor the code and ensure it follows the clean code principles. Secondly, you need to write code that covers the edge cases for the Mars Rover.
Note: Before proceeding, make sure to fork the repository to your own Github account.
There is a missing feature with my code. Currently, the rover can move out of the plateau if given the wrong commands. It’s your task to write the code that handles this case. The rover’s position should not be modified if the next spot is outside the plateau.
This kata is relatively easy, but, I think it is a good first step into the world of code refactoring and writing tests for existing code.
Looking forward to your feedback…
References & Links
I was one of them! Now, I consider that to be a bad habit.
With time I realized that it is hard to remember the intention of an old commit by just looking at the code. It becomes even impossible when reviewing a colleagues’ code.
In this post, I will be sharing a template message I adopted with one of my previous teams.
For simplicity, I will be referring to one of my commits to a fork of the GildedRose-Refactoring-Kata on my GitHub account.
Below is the template. As you can see, it is split into four colors. In the next part of the blog, I will break down the message to explain each of its sections separately.
[Issue Tracker #] One-line summary of the commit description Why is this change necessary? - Detailed description 1 - Detailed description 2 ...
Between the two brackets, I write the issue number corresponding to this commit. The format here varies depending on which issue tracking system I am using (ex: [ProjectName-123] for Jira and [#123] for Github.)
[#5] One-line summary of the commit description Why is this change necessary? - Detailed description 1 - Detailed description 2 ...
In the second section, I try to summarize the full commit in just one sentence. Failing to do so means that my commit is doing more than one task and needs to be broken down.
[#5] Move the constants out of the GildedRose class
Why is this change necessary?
- Detailed description 1
- Detailed description 2
...
In this line, I try to answer the question ‘Why is this change necessary?’ In most cases, this line is a copy of the ‘Issue Title’.
[#5] Move the constants out of the GildedRose class
In order to, refactor and clean the code of GildedRose
- Detailed description 1
- Detailed description 2
...
Finally, I replace the items of the list in this section with a detailed description of the important technical changes I did in the code. Here I try to think of what I should remember if I was to read this message a couple of months ahead.
[#5] Move the constants out of the GildedRose class In order to, refactor and clean the code of GildedRose - Make MAX_QUALITY, MIN_QUALITY & MIN_SELL_IN_DATE as local variables ItemVisitor - Move AGED_BRIE string to the AgedBrie class - Move SULFURAS string to the Sulfuras class - Move CONCERT string to the Concert class - Adapt the tests accordingly
By just reading this last message, anyone should deduce that firstly, in this commit I only moved some constants from the GildedRose class. And secondly, the commit was part of a larger code refactoring of the GildedRose class.
So, why do I consider this to be helpful?
Here are my reasons:
At first, it was a bit annoying to write this message for each commit. But as we realized its benefits, it became a habit for us!
You don’t have to adopt this exact message, you can come up with any format you think is good for you as long as it is clear and concise.
Enjoy coding 
From the first look, I thought my only job was refactoring the tests and probably some helper classes. But, I was wrong! I ran into a configuration issue with the maven-surefire-plugin configuration.
In this post, I will be sharing the encountered issue and its fix. Note that I will not cover the detailed steps of how to migrate from ‘JUnit 4’ to ‘JUnit 5’.
We, as many other developers, use the maven-surefire-plugin to run our tests during the test phase of maven’s build lifecycle. We rely heavily on this plugin because it fails the build when one of the tests is broken!
With JUnit4, everything was working perfectly! But after the migration, the plugin was always reporting that not tests are run:
'Tests run: 0, Failures: 0, Errors: 0, Skipped: 0 ...'!
For the purpose of this blog, let us assume we want to build a Calculator. For now, we only have the ‘add‘ method implemented as shown below.
Calculator.java
public final class Calculator {
public static int add(int firstNumber, int secondNumber) {
return firstNumber + secondNumber;
}
}

I will start with the ‘JUnit 4’ version of the code.
pom.xml
We need to depend on the junit artifacts and add the plugin for the surefire. Here is the required pom file:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
CalculatorTest.java
In the test class, we have one test that asserts our method is doing the correct addition.
import org.junit.Assert;
import org.junit.Test;
public class CalculatorTest {
@Test
public void
our_calculator_should_add_2_numbers() {
Assert.assertEquals(5, Calculator.add(2, 3));
}
}
Build Output
Now, if we try to run ‘mvn clean install‘ on the above pom file we get the below output:
------------------------------------------------------- T E S T S ------------------------------------------------------- Running CalculatorTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.037 sec - in CalculatorTest Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 **** [INFO] -------------------------------------------------- [INFO] BUILD SUCCESS [INFO] --------------------------------------------------
Everything works perfectly!

It’s time to migrate our code to ‘JUnit 5’!
pom.xml
The first step is changing our dependencies in the pom file.
Some significant changes were applied to the ‘Junit 5’ dependency metadata. The framework functionalities have been split into several artifacts:
For this simple example, it is enough to depend on ‘junit-jupiter-engine‘ & ‘junit-platform-surefire-provider‘.
So, our pom becomes:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
P.S. Check ‘JUnit 5 User Guide‘ if you are interested in more details on the ‘JUnit 5’ artifacts
CalculatorTest.java
A good portion of the code in tests has to be changed to migrate to ‘JUnit 5’. Again, I am not going to cover those changes in this blog.
For the simple test we have written before, we need to change two things:
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class CalculatorTest {
@Test
public void
our_calculator_should_add_2_numbers() {
Assertions.assertEquals(5, Calculator.add(2, 3));
}
}
Build Output
Although the ‘mvn clean install‘ command is still returning a ‘BUILD SUCCESS‘ message, it is actually not running any tests which make the whole build process suspicious.
This is our issue!
------------------------------------------------------- T E S T S ------------------------------------------------------- Running CalculatorTest Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec - in CalculatorTest Results : Tests run: 0, Failures: 0, Errors: 0, Skipped: 0 **** [INFO] ------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------
The fix of this problem is simple, need to modify the build section in our pom to add the 2 dependencies to the ‘maven-surefire-plugin‘ plugin section as shown below.
By doing so, we forced the maven-surefire-plugin to use the latest JUnit artifacts and thus run the JUnit 5 tests.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.0.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Running ‘mvn clean install‘ will return the correct output now:
------------------------------------------------------- T E S T S ------------------------------------------------------- Running CalculatorTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.044 sec - in CalculatorTest Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 **** [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------
I hope this blog post would save you some time when migrating to ‘JUnit 5’!
Good luck 
Click to Enlarge Image
]]>I had a lot of those debates!
A couple of months ago, I came across such a debate between Jim Coplien and Robert Martin (Uncle Bob). I found this discussion kind of interesting especially that it involves two leaders in software engineering.
You can watch the debate here:
Here are my takeaways from the discussion:
Uncle Bob defines the following three rules for applying TDD:
Jim points out that he has no problem with those rules, his concerns are more architecture related. Jim and Uncle Bob would argue for more than ten minutes to finally reach an agreement on the importance of architecture. The below five points summarizes what they agreed on:
Probably the only disagreement you can sense from this debate is what defines a professional software engineer. For Uncle Bob, it is irresponsible for a software engineer to deliver a single line of code without writing a unit test for it. Jim, on the other hand, considers ‘Design by Contract’ to be more powerful than TDD.
Personally, I have been applying TDD since I joined my team three years ago. After experiencing the benefits of this practice, we got to a point where we don’t write or refactor any line of code without having a corresponding unit test!
In addition to that, I had the chance to coach other developers by running coding dojo sessions at work.
All that makes me say that I agree more with Uncle Bob on the topic of professionalism!
Of course, you’ve heard this question many times before and probably your answer will slightly differ from your teammates’. But, with the help of two agile team-building activities, you can eliminate such a difference and reach an alignment with your teammates on a description for your team and product. Those activities are ‘Collaborative Product Vision‘ and ‘Defining the Team Vision Statement.’
In this post, I will only be covering the product vision activity.
Here is the template of the product vision:
FOR (1. target customer) WHO (2. statement of the need or opportunity) THE (3. product name) IS A (4. product category) THAT (5. key benefit, compelling reason to buy) UNLIKE (6. primary competitive alternative) OUR PRODUCT (7. statement of primary differentiation)
Here are the steps to simulate the activity:
I am writing this blog because I think this is a nice activity for any team! So far, I have played it with three different teams, existing and newly established ones.
Here are some benefits of playing this activity:
FOR Outlook Users WHO want to keep track of time spent in meetings THE MeetingReporter IS An outlook-addin THAT generates time reports from the calendar UNLIKE existing tools where you have to manually insert time spent OUR PRODUCT will parse the calendar and automatically generate different reports based on meeting category
Above is an example!
It is the vision of the tool I am writing on my free time! From this short statement and without any further explanation, anyone should understand purpose and major feature of this tool!
Some people consider such activities as a waste of time. Obviously, I disagree! This is an occasional activity that takes between 15 to 20 minutes depending on the team’s size. Thus, it is an excellent candidate for the first activity of the team’s retrospective!
I encourage teams to try it. For those who do, I am pretty sure you will be posting the outcome on your team’s home page, just as we did!
Java documentation and Uncle Bob’s book (Clean Code) were my references when preparing this tutorial. I noticed a slight difference between what is recommended in each!
While reading the Java documentation, you sense a preference for Checked Exceptions. The documentation states that the usage of UnChecked Exceptions should be restricted to the case where crashing the system is intentional if an exception occurs. In other cases where recovery is still possible, Checked Exceptions should be used.
Uncle Bob has a different opinion! He argues that although Checked Exceptions might have some benefits and can be useful in some special cases like writing critical libraries, they are not a necessity to have a robust software.
Breaking the ‘Open/Closed Principle‘ and ‘Encapsulation‘ are the main two reasons that make using Checked Exceptions a bad idea!
Let’s see how!
In the below example, the MainBookReader.main method is calling the method MainBookReader.readJsonObject to get the book’s description from a JSON file. And since readJsonObject throws an exception we had to add throws FileNotFoundException clause to the signature of the main method and the interface JsonLoader!
import java.io.FileNotFoundException;
public class MainBookReader {
public static void main(String[] args) throws FileNotFoundException {
BookJsonLoader jsonLoader = new BookJsonLoader();
Book bookFromJson = jsonLoader.readJsonObject("books.json");
System.out.println(bookFromJson.name());
}
}
import com.google.gson.Gson;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class BookJsonLoader implements JsonLoader {
public Book readJsonObject(String fileName) throws FileNotFoundException {
FileReader jsonFile = new FileReader(fileName);
Gson gson = new Gson();
return transformToBook(gson.fromJson(jsonFile, JsonBook.class));
}
private Book transformToBook(JsonBook jsonBook) {
return new Book(jsonBook.getName());
}
}
import java.io.FileNotFoundException;
public interface JsonLoader {
Book readJsonObject(String fileName) throws FileNotFoundException;
}
Adding the throws clause to most of our methods means two things!
This is a violation of the ‘Open/Closed Principle‘ and ‘Encapsulation‘!
No matter how careful we are, things can still go wrong. Thus we still need to deal the exceptions when they occur! The alternative to the code above is writing clean code!
In this section, I will refactor the above code to make the code look better, although it might involve more code!
The first step is to wrap the exception in one place! In our case, we can replace the throws clause with a try-catch!
In the catch clause, I’m throwing a JsonFileNotFoundException (UncheckedException) that I have created based on the needs of this system (i.e. providing an informative message to the user).
public class BookJsonLoader implements JsonLoader {
public Book readJsonObject(String fileName){
try {
FileReader jsonFile = new FileReader(fileName);
Gson gson = new Gson();
return transformToBook(gson.fromJson(jsonFile, JsonBook.class));
} catch (FileNotFoundException e) {
throw new JsonFileNotFoundException("Can't find the file " + fileName + ". Please make sure it exists!", e);
}
}
private Book transformToBook(JsonBook jsonBook) {
return new Book(jsonBook.getName());
}
}
The benefits of this refactoring are:
In some cases, developers don’t want to throw an UnCheckedException, so they tend to return a ‘null’ instead! That is a bad practice because it will eventually result in a NullPointerException later.
So, what to do? The answer is simple, return a SPECIAL CASE!
The below class ‘MissingBook‘ is our special case!
public class MissingBook extends Book {
private MissingBook(String name) {
super(name);
}
public static MissingBook aMissingBook() {
return new MissingBook("MISSING BOOK");
}
}
This allows us to return an instance of the MissingBook in the catch instead of throwing an exception!
public class BookJsonLoader implements JsonLoader {
public Book readJsonObject(String fileName){
try {
FileReader jsonFile = new FileReader(fileName);
Gson gson = new Gson();
return transformToBook(gson.fromJson(jsonFile, JsonBook.class));
} catch (FileNotFoundException e) {
return MissingBook.aMissingBook();
}
}
private Book transformToBook(JsonBook jsonBook) {
return new Book(jsonBook.getName());
}
}
In your code, you shouldn’t pass null as parameters and instead use the Special Cases as before. But, it gets harder to prevent your clients from passing nulls thus you might need to assert some function parameters before proceeding with your code execution!
Doing the refactoring for the simple example above might look like over-engineering! But, it becomes beneficial when writing more complex code!
I think that the excessive usage of CheckedExceptions will pollute the code and thus should be avoided! For me, the benefits of CheckedExceptions can still be achieved through:
To understand better the importance and how to write clean code, it is highly recommended that you read the book Clean Code by Robert Martin!
The purpose of this short tutorial is to clarify those two points. And to make it clearer, I will be dividing it into two posts. In the first one, I will explain the different types of Java exceptions. Whereas in the second one, I will show how to write clean exception code!
In Java, the class Throwable is at the top of the class hierarchy of all exceptions. The classes Error and Exception directly extend Throwable. All the subclasses of Error and Exception are grouped into two types ‘Checked Exceptions‘ and ‘UnChecked Exceptions‘as displayed in the image below.

UnChecked Exceptions extend the classes ‘Error‘ or ‘RuntimeException‘. In this case, developers don’t have to worry about catching or handling the exceptions at compile time as the compiler won’t report any error. But, as they are not caught, those exceptions may result in complete failure of the application when thrown during execution.
Below are two examples of UnChecked Exceptions!
The below method simply divides two numbers:
private static int divide(int numerator, int denominator) {
return numerator / denominator;
}
The code is fine and will compile with no errors! So what is the problem?
The problem might occur at runtime if we try to call the method while passing zero as the denominator. Since this exception is not caught, our system will crash throwing the below arithmetic exception:
Exception in thread "main" java.lang.ArithmeticException: / by zero at MainUnCheckedException.divide(MainUnCheckedException.java:7) ...
The below method compiles, but since I missed writing the base cases calling the method will crash the system and throw a StackOverflowError (as shown below) will throw an Error!
private static int fibonacci(int number) {
return fibonacci(number - 1) + fibonacci(number - 2);
}
Exception in thread "main" java.lang.StackOverflowError at MainUnCheckedException.fibonacci(MainUnCheckedException.java:10)...
On the other hand, all other classes extending the ‘Exception‘ class are considered to be Checked Exceptions. In this case, the code will not compile if the developer doesn’t explicitly handle the exception.
This adds a level of security to your code, as you are forced to specify how your code should behave when an exception occurs and thus decreases the chance of having an unrecoverable failure in the system.
Let’s see an example!
public class MainCheckedException {
public static void main(String[] args) {
FileInputStream fileInputStream = new FileInputStream("foo.txt");
}
}
Error:(6, 43) java: unreported exception java.io.FileNotFoundException;
must be caught or declared to be thrown
In the above code, we are trying to read a file ‘foo.txt’, but our compiler complains that we need to handle the FileNotFoundException.
Solving this compilation error can be done in two ways:
private static void readFileTryCatch() {
try {
FileInputStream fooFile = new FileInputStream("foo.txt");
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
Even if the file was not there, this code would not break thanks to the try-catch. As shown in the below code and output, the system will print out the exception and continue execution!
public static void main(String[] args) {
readFileTryCatch();
System.out.println("Done");
}
------------Output------------
foo.txt (No such file or directory)
Done
private static void readFileThrow() throws FileNotFoundException {
new FileInputStream("foo.txt");
}
Using this solution will propagate handling the exception to the calling methods as illustrated in the two options below:
Option 1: Adding throws to all methods signatures
public static void main(String[] args) throws FileNotFoundException {
readFileThrow();
System.out.println("Done");
}
------------Output------------ Exception in thread "main" java.io.FileNotFoundException: foo.txt (No such file or directory) at java.io.FileInputStream.open0(Native Method) ...
Option 2: Catching the exception in one of the methods in the call stack
public static void main(String[] args) {
try {
readFileThrow();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
System.out.println("Done");
}
------------Output------------ foo.txt (No such file or directory) Done
I hope this post gave you a better understanding of Java exceptions! In the 2nd part, I will be sharing more details on how to properly write exceptions in clean code and which of the two exception types to use!
]]>The sessions at the conference were categorized as “Technology and Technique,” “Customer and Planning,” “Team and Individual” and “Process and Improvement.” And some of which were game based sessions!
In this blog, I will be sharing my feedback on two games I participated in!
The purpose of this game is to introduce participants to the practices and principles of Lean-Startup by simulating a business where each group has to produce a product and sell it to customers.

In the beginning, the game seemed a bit complex or vague, but things started to get clearer after the second iteration. Here are some of the game rules:
During the session, we came across many lean-startup vocabularies, but here I will only be mentioning five of them!
This game can be played by startups or even teams at large enterprises! And at the end, players should have got an idea of what Lean-Startup is. And more importantly, they will learn some of the factors that can play a role in any team’s failure or success such as competition, luck, technical excellence, failing and successful experiments, etc.
As the name indicates, this game helps teams to assess how agile they are.
The game can be played by either an existing or a new team. In the case of an existing team, the results can be a base for a plan to change or improve their agile process. Whereas, for a new team this activity can be considered a futurespective activity as it defines the team’s first iteration.
The game consists of 52 cards, where each card holds a sentence on applying an agile practice. To play the game you should follow the below instructions:
Not or inconsistently done

Below are statements taken from five cards:
To know more about this game, you can check Ben’s blog on this session!
I recommend those games to agile teams. Personally, I will be organizing two sessions with my team soon at Murex!
Finally, thanks to the organizers for setting up such an amazing and successful conference! See you next year! Probably as a speaker 
You can watch it here:
You can also have a look at my previous two blogs on the subject:
]]>