Posts about Java programming, Java news and comments!
The last Christmas article - about most common Java bugs - was a success for me and this blog. This time we will take a look into OSGi bundles, which I consider as the vital top architecture for many products (like Eclipse, Jira, Adobe Experience Manager). On the other hand, OSGi is often treated as yet another Dependency Injection container, which is wrong by definition. Let’s take look at the tree mistakes developers and architects are making with their OSGi ecosystem’s design.
Java 14 is coming with a new feature - NPE’s
getMessage()method will return a description which object is null and, therefore, a cause of an error. The feature is called “helpful NullPointerExceptions” which sounds very nice. But is it really helpful? How is it working? Are there some downsides?
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
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?
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.
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