Pages

Wednesday, December 12, 2018

Java 8 Refactoring Part 3: Removing null checks and introducing Optional

The Optional feature of Java 8 it's very powerful it can prevent lot's of bugs and also make our code look more tidy. In this video I show an example which is very similar to the one presented at Devoxx London in 2018 by V. Rentea


Sunday, December 9, 2018

Java 8 Refactoring Part 2: Stream Wrecks

A stream wreck is a complex concatenation of streams that tries to provide some results in a one-liner. One-liners are good but concatenation of Streams is not good since makes the code quite difficult to follow. To fix stream wrecks we need to extract methods, local variables or classes in order to be able to improve the readability of the code.


Saturday, December 8, 2018

Java 8 Refactoring Part 1: Extracting Anonymous function to it's own class

In this video I mimic the first of the design patterns presented by Victor Rentea in the Devoxx conference in London in 2018.


Perhaps worth mentioning that the advantages of extracting anonymous functions into separate classes are:

- better readability of the code
- code easier to test
- more maintainable code

Tuesday, October 9, 2018

Stop and think!

I don't know if it is because I am approaching my 33rd bday and I am starting to show certain sings of aging or why, but sometimes I feel that sometimes we tend to work at super-sonic speed.

The priority is always to fail fast, or the market window, or the mvc, the agile cycle, etc ... When it comes to work we are all somehow indoctrinated to have a results-driven mindset. Everything seem to be results and competition.

Today I had a thought and I wanted to share it here on my blog:

"How would our industry look like if companies replaced their competitive results-driven mindset with a thinking-learning oriented mindset?"

Tuesday, September 11, 2018

AspectJ + Custom Annotation + Gradle (Without Spring)

Recently I had to create an aspect to run before and after an annotated method was executed.
The first thing I thought was to just use Spring, but I was told that the devices where this software was going to run have limited resources so Spring would not an option because of it's large memory footprint.

After looking around the internet for a while I decided to do it just by using the aspectJ framework on it's own.

This is how I configured my gradle file
group 'com.javing.customAnnotations'
version '1.0-SNAPSHOT'

project.ext {
    aspectjVersion = '1.8.4'
}

apply plugin: 'java'
apply plugin: 'aspectj.gradle'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "gradle.plugin.aspectj:gradle-aspectj:0.1.6"
    }
}

dependencies {
    compile 'org.aspectj:aspectjrt:1.8.4'
    compile 'org.aspectj:aspectjweaver:1.8.4'
    compile 'org.aspectj:aspectjtools:1.8.4'
    compile 'junit:junit:4.12'
}


I created a custom annotation which will later allow me to trigger the aspect.
package spike;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface HelloAnnotation {

    public boolean isRun() default true;

}


I placed the annotation on the methods I want the aspect to run around
package spike;

public class HelloApp {

    public static void main(String[] args) {
        HelloApp helloApp = new HelloApp();
        helloApp.work();
    }

    @HelloAnnotation
    public void work() {
        System.out.println("Hello world!");
    }
}


Finally I created the logic of the aspect
package spike;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class HelloAspect {

    @Around("execution(* *(..)) && @annotation(spike.HelloAnnotation)")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("Before");
        Object proceed = pjp.proceed();
        System.out.println("After");
        return proceed;
    }

}

In order to see this working in the IntelliJ editor you have to make sure you enable the Gradle Test Runner.



Friday, August 17, 2018

Refactoring - How do I start?



According with the Software-craftsmanship legend Sandro Mancuso we should start:

  • Refactoring from the deepest nested branch of the code and work our way inside out.
  • Test from the shortest/less nested branch of the code and work our way from outside in.

Wednesday, August 8, 2018

Using Wiremock to simulate a slow responding server.

Here a little example of a stub created with Wiremock that simulates a slow reply from a server:

 import com.github.tomakehurst.wiremock.WireMockServer;  
 import java.util.Scanner;  
 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;  
 import static com.github.tomakehurst.wiremock.client.WireMock.get;  
 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;  
 public class FakeThirdPartySystem {  

   public static void main(String[] args) {  
     WireMockServer server = new WireMockServer(8081);  
     server.start();  
     server.stubFor(get(urlEqualTo("/someUrl"))  
         .willReturn(aResponse()  
             .withStatus(200)  
             .withFixedDelay(10000)  
             .withBody("Slow reply!")));  

     //This is just so that the app doesn't exit straight away
     Scanner scanner = new Scanner(System.in);  
     System.out.println("Press enter to exit");  
     scanner.nextLine();  
     server.stop();  
   }  

 }  

Note: This is just for illustration purposes but this same way of stubbing can be added to an acceptance test. 

Share with your friends