Correct Code: Pattern for handling user input

Correct Code: Pattern for handling user input

My 15yo daughter has got programming as a subject in her high school. A couple of days ago, she came asking for help. The program she wrote had a bug. While asking her what are the possible options, I came to one simple realization: handling user input has rules. Naimly, her bug was that she was missing the very first step, which I did intuitively. Breaking it down for her is the basis of this post. The pattern When I…

Read More Read More

Correct Java switch statement

Correct Java switch statement

When writing correct code, we should prefer the compiler to do the checks for us, instead of the runtime. While Java’s type system helps us a lot with writing correct code, it has its limitations. Consider an enumeration. public enum AllowedColor { RED, BLUE, YELLOW} A switch statement would look like switch(color) { case RED: System.out.println(“it’s red”); break; case BLUE: System.out.println(“it’s blue”); break; case YELLOW: System.out.println(“it’s yellow”); break;} This looks ok, but what happens when someone adds a new enum…

Read More Read More

Error handling – categorization and best practices

Error handling – categorization and best practices

While working with different teams in the Java world, I found that many people intuitively know how to deal with software errors. I found it fascinating that so many people came with the same solution, in so many different places. This must be some sort of a pattern that we are yet to name, since we have already named Pokemon exception handling. Categorisation Even though the idea for this article came from Pokemon exception handling, in order to convince people…

Read More Read More

Repositories – why and how not to use them?

Repositories – why and how not to use them?

Story time It’s really a sin not to use a repository, but I can be forgiven if I don’t use it in an example.Consider your software dealing with customers, which, internally, have different features based on when the customer signed up and his status. If customer is registered less than 5 days ago, you should display a big red button saying ‘buy this feature’, and in the back office, you should prioritize tickets these customers create. Let’s call these customers…

Read More Read More