Java

Me

Posts about Java programming, Java news and comments!

  • SOLID in Test Automation #2: Liskov Substitution Principle

    Liskov Substitution Principle

    My mental abilities (actually lack of them) pushed me to software engineering. Seriously. During my childhood and adolescence I had big troubles with remembering things. A poem to learn? No way. Geography or history? In these fields you were a good student if you remember dates, names, locations. I just couldn’t remember these things that were coming to me out of blue. Mathematics in primary school was also painful: multiplication tables. That was awful! I remember other kids were just immediately writing a number based on input and I had to perform some calculations in runtime (6 times 9? That’s 6 times 10 and minus 6). The funny thing is it turns out that processors are not keeping multiplication tables, oh what a shame, we learnt absolutely useless portion of knowledge. On the other hand, understanding of complex behaviour or systems was relatively easy (as long as I understood the motivation behind: it was damn easy to learn trigonometric functions by unit circle rather than some ridiculous rhyme where cosines is negative). Without a struggle, I learnt physics, somewhat of electronics and programming.

    When I was a bit younger, an interviewer (some Java developer) asked me:

    Do you know SOLID, boy? What does L stand for?

    ↓ more
  • SOLID in Test Automation #1: The Open Closed Principle

    Open-Closed Principle in Test Automation

    I know, your code works, but…

    I am going to do the review of SOLID principles for tests, mixing my own experience with top-shelf publications about testing and clean code in general: let’s take a look at approaches we can apply in our greenfield or brownfield projects. Do you know how to prevent rewriting all tests when implementation (but not necessarily behavior) is changing?

    ↓ more
  • OSGi clean code and bundle patterns

    SlingGetServlet code screenshot

    Eclipse, Adobe Experience Manager, Jira - what do they have in common? They all are using OSGi container. OSGi is a container of bundles. There is a very important bit of bundles architecture then. Keep in mind, that OSGi is nothing similar to classic DI mechanisms, as it defines another layer of isolation: bundles. Guice or Spring still can be used within OSGi container separately for just a one bundle. Within the bundle, the framework provides special Class Loader which is loading all classes based on the configuration of the bundle and a context of its execution. You can think about single bundle as an isolated runtime environment with components, explicitly defined packages and services as an external way of communication.

    ↓ more
  • How to write bad Java constructors

    An engineer looking for well designed constructor but deadline is coming

    Constructors in object oriented programming are very important static methods - they start an existence of every object. Regular Java code is full of constructors, and we actually used to them as well. But can constructors be better and worse? Sure. I made a list of 4 bad constructor rules that came up first to my head.

    Good practices are so boring so let’s see how we can make other engineers’ life worse!

    ↓ more
  • Are design patterns gone already? (poll!)

    Design Patterns book

    How many design patterns do you know?

    How do you implement Observer?

    What Java class has been implemented with Decorator pattern?

    Every interview was starting with these questions. How many design patterns I don’t know? I read the book, I knew every single coming from Group of Four. Heck, I had these patterns taught in the University!

    Are they used now, anyway?

    ↓ more
  • HTTP Client (java.net.http) Java 11 API - the practical example

    Java logo in binary code

    Any HTTP Client library in Java is a hell to me. Comparing to other technologies like Groovy which have built-in APIs, Java sucks. Projects use Apache HttpClient API for trivial tasks like testing, which I consider as a brain rape because of the usage difficulties. When the HTTP client was introduced and incubated in Java 9 I was like:

    Finally, we have our HTTP client, at least for testing!.

    Now it’s officially moved to java.net.http package. I had to try it on my own in the home laboratory.

    ↓ more
  • Testing Java code performance with JMH

    JMH on code background

    Recently, I wrote a blog post about performance of Java Data classes. Many people pointed me out I did not write a JMH test. Instead, I wrote simple microbenchmark on my own, based on the Java performance book. Because it is, apparently, unreliable I had to write JMH test as well and compare results - trying to explain why it’s different. Let’s answer outstanding questions.

    What do you think? Am I, in fact, totally wrong here?

    ↓ more
  • Bug się rodzi - the most common Java bugs

    Santa with laptop

    Hello, fellow programmers! First of all, I wish you all Merry Christmas! Spend this time well, there is not many moments for rebuilding family bounds these days. This is the special, Christmas blog post. I named it that because it’s fun comparison with one of the most known Polish carols (listen it here, play it now) - “Bóg się rodzi”. The name is “God Is Born”. Polish word for God - “Bóg” - reads as same as “Bug”. How programming-ready this language is!

    Today, we are going to discover the most common Java bugs I have seen in my Java journey. Hope you won’t find them under the Christmas Tree :D (that’s another polish tradition - we are putting presents under the tree).

    ↓ more
  • Data classes in Java - runtime underperformance vs code readability

    Screen with some code

    BTW: Funny thing. Let’s go back to the day I started writing this blog. I thought I would never find enough topics to blog weekly. Now, the number is amazing. Optimistically, I should write about all of them in 2090. I am just saying - regardless to many people’s opinion, the blog exists.

    I recently wrote a constructor:

    Cuboid(double width, double height, double depth, byte r, byte g, byte b)
    

    It doesn’t look right. There are many values of the same (and similar, like bytes-doubles) type. On the other hand, what can be wrong with many parameters? I started to instantiate these cuboids. The order of parameters was obvious but I had to check it few times - it’s very painful. And, yet, I wrote it myself! What if someone else will try to use it?

    But hey, I can create additional data classes, right? So, I created two POJOs (Dimension and Colour) and changed the constructor to:

    Cuboid(Dimension dimension, Colour colour)
    

    Parameters now are related to the purposes they serve: dimension and colour. During the code review I received an honest but scary feedback though:

    Classic Daw. Are you mad? This is really inefficient way, you are creating new instances! R U MAD?!?!?!?!?!?!

    It made me nervous. Is creating these classes a bad thing? We are not writing here some serious, highly-performing stuff, just regular code that helps other people to solve their problems. I was really confused - is the guy right? Many people would say that object instantiation takes too much time in JVM. They are correct, however, the next question is: “How long?”.

    I needed to know. It is obviously an additional work to a computer but the advantage is significant for other programmers’ work. I had to test in on my own. Let’s do the microbenchmarking.

    ↓ more

subscribe via RSS