Thursday, April 20, 2017
Browser automation in Java using Cucumber and Selenium
Thursday, April 13, 2017
Implementing Strategy Design Pattern(Practical example)
In this video I show how to refactor some ugly if-else nested statements using polymorphism through the strategy design pattern.
Please note that I didn't use code coverage in this occasion, this was more of an Spike than a refactor I would say. My goal was to just show the implementation of the pattern and make a brief video. Of course if you are trying this in real software, please make sure that your code is covered by unit tests before proceeding.
Please note that I didn't use code coverage in this occasion, this was more of an Spike than a refactor I would say. My goal was to just show the implementation of the pattern and make a brief video. Of course if you are trying this in real software, please make sure that your code is covered by unit tests before proceeding.
Tuesday, April 11, 2017
Checking that 2 Lists have elements in common
How many times have we created loops to check if any of the elements of one list are contained among any of the elements of other list?
Lists have a contains() and also a containsAll() methods but they doesn't seem to do what we want and we end up writing spaghetti that looks like this often:
Sometimes we forget that the Java API has so many useful things. Like this disjoint() method, from Collections.
Its not Java 8 ;) its being there for a bit already, have a look at this docs:
https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#disjoint(java.util.Collection,%20java.util.Collection)
Lists have a contains() and also a containsAll() methods but they doesn't seem to do what we want and we end up writing spaghetti that looks like this often:
1: boolean isSafeToEat(List<String> allergicFoods) {
2: for (String ingredient : getIngredients()) {
3: if (allergicFoods.contains(ingredient)) {
4: return false;
5: }
6: }
7: return true;
8: }
Sometimes we forget that the Java API has so many useful things. Like this disjoint() method, from Collections.
1: boolean isSafeToEat(List<String> allergicFoods) {
2: return Collections.disjoint(ingredients, allergicFoods);
3: }
Its not Java 8 ;) its being there for a bit already, have a look at this docs:
https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#disjoint(java.util.Collection,%20java.util.Collection)
Refactor switch to Enum
Do switch statements sound good to you?
Trust me better switch to Enum before it destroys your ears ;)
Labels:
clean code,
code smell,
enum,
java,
refactor,
switch statements,
tips and tricks
Monday, April 10, 2017
Mocking static method calls
Very simple example on how we can use method delegation to enable unit testing where there is a dependency to an static method call.
Labels:
dependencies,
hard wired dependency,
java,
Mock,
Mockito,
static method call,
TDD,
Unit Testing
Friday, April 7, 2017
I love my new keyboard :)
Hello World! Look at my new keyboard:
comfortable, noisy, smells of bambu, spacious, feels dry and fresh all the time, nice polished surface finish, not hypersensitive to touch, resistant, doesn't need batteries, only relevant keys, free included mouse... Using it for a week already, it works great. I just love it! :)))))))
Refactoring "Feature Envy" Code Smell
In this video I explain the "Feature Envy" Code Smell and how it can be refactored. Sorry I made a mistake in the screen casts and I just selected the window rather than the area, you can't see the pop up menus and auto suggest when I press Ctrl + Space. Sorry about that :(( It takes me some time to make videos but if I get the chance soon I will repeat the demo and re-upload it.
Labels:
clean code,
code smell,
feature envy,
java,
Refactoring,
tips and tricks
Thursday, April 6, 2017
Dealing with Hardwired dependencies part 2 - Making a design decission
In this second video, I make a design decision and I remove the temporary refactor that I did earlier.
The Seam was not a bad idea, but it was a temporary solution only until I make a decision about the design of the class.
The Seam was not a bad idea, but it was a temporary solution only until I make a decision about the design of the class.
Dealing with Hardwired dependencies part 1 - An alternative to Dependency injection.
In this video, I show a little refactoring trick that can be used to unit test a method that has a hardwired dependency to an external system via an static method call. This is an alternative approach to dependency injection, which can be used when we are not sure about modifying the design of the class. In very large systems making design decisions such as adding another method to a constructor etc ... may have a big impact in design of both production and test harnesses. This trick can help you temporarily postpone your design decision and get you going with your unit testing.
Labels:
code smell,
hard wired dependency,
java,
Refactoring,
Unit Testing
Wednesday, April 5, 2017
Observer Design Pattern in Java
A very interesting, useful and popular design pattern that I like.
Here a video with explanation and code example:
Subscribe to:
Posts (Atom)