Posts about Java programming, Java news and comments!
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!
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?
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.httppackage. I had to try it on my own in the home laboratory.
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?
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).
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.
subscribe via RSS