Friday, July 7, 2017

I want my Rhino back! Switching to Nashornless.

In JDK 8 default scripting engine had been changed. Nashorn replaced Rhino. I understand all positives of this change and how good the use of InvokeDynamic is, etc.
But this change may be also seen as a breaking change. Which is quite normal in world of JavaScript btw :D And do not expect compile time errors! It is almost like node experience...

Ok, your old Rhino-specific code will not run with Nashorn. However, I have been using it for years, and truly speaking missing it a lot. I wrote test tooling scripted by Rhino, glue code that connects ETL to Java or dynamic backend of application, allowing website maintenance with no redeployment. Still, it is possible to go back to old good JavaScript implementation. So let's make our Rhino great again!

We could, of course, use it like we did it before JDK 6: add just a lib to our project and evaluate script by means delivered by specific Mozilla implementation.

If we are more familiar with Java scripting in JSR-223 style we would need extra code that is Sun specific part, already removed from recent Java. But fortunately someone already did the job for us and provided alternative JSR-223 wrapper that uses original Rhino and provides all extra classes that are required by standardized engines.
Let's just add dependency to pom.xml file:

And yes, now we can use old, good, nashornless Rhino again! Just one more thing to remember is to use right scripting engine instead of "javascript".

new ScriptEngineManager().getEngineByName("rhino");

Thursday, June 8, 2017

No pride, no prejudice: Effective Java

While reading programmers' blogs you can be exposed not only to shiny technical stuff, as well you can meet author's private opinion. It's positive, as human being we want to share our opinions with others. But some opinions seem weired - and only possibility is to get used to that. One blogger dissed proggrammers' meetups, other advocated abortion, another one prefered null checks over Optional, million others actively hate statically typed languages. Not mentioning Emacs fans...

Some time ago I read a blog post in which author rejected idea of reading "Effective Java". Blogger had explained that book must be clearly outdated as 2nd edition was released in 2008 and he is not going to loose time for reading things like string concatenation vs using StringBuilder.

Fortunately this classical book is easy to defend, even for not such-a-proficient-blogger like me.
Well recognized Java bestseller is not really about outdated micro-optimations on low layer (which btw may also be a good topic to know). It is not about what is different from C in Java.

This book introduces readers into variety of interesting topics.
After reading you will (not only) be more aware of why immutability is important in contemporary software, how to structure data better, or how to use compiler to ensure type safety to work for your benefit. It will encourage you to use language constructs that support safe coding (eg. using enums), name things correctly (like use static factory methods) and depict constructs that are problematic (eg. clone). And explain variance in generics... :D

My personal observation is that Bloch's book was for some Java guys a first step into typed functional programming with Scala or even Haskell. I have seen lot of smart who followed this path. They could of course years ago assume that their knowledge of Java and enterprise technologies was good enough and they do not need to read one more book on `efficiency`.
I think that they had a very good and respectable attitude to their own knowledge.
Be like them. Do not believe you already know everything, rather as a software engineer you must be open to learn, read and try new things out. And sometimes read 10 years (or even more) old books.

Wednesday, March 1, 2017

Generating model with inheritance from swagger specification

The post on generating java model with inheritance from swagger specification is on Zooplus company blog.

BTW I must admit I like more code formatting options on ghost platform than what I have here with blogger.

Saturday, January 21, 2017

The great red dragon and the novice programmer

As a novice software developer at some point of time you will find enlightenment. You will understand what is the source of Power. Power of perfect code development. Power of ideal expression ideas to text. SOLID and clean, intentional and free of any accidental complexity.

You will desire that Power. Like the Pilgrim admired the power of The Great Red Dragon. But still you need a transformation. Pilgrim needed his victims to see his beauty, as he himself admired the Red Dragon. You will instead look at code you work with. The real code. Ugly. Legacy. Half year old code written on 2 releases backward Spring version. You will show your vict... sorry, colleagues that bad code. And yourself as a powerful Red Dragon. But... You're not yet him.

You need your transformation. I was there too. Now I know. Please keep in mind one thing. Consult your more experienced colleagues before you start making that code better. Example from my transformation. I spent some time years ago to refactor piece of code using collection to keep objects of various classes. Then cascade of instanceOf... This was looking for me as an unacceptable implementation. Unfortunately, my employer would have been more happy if I had focused on my tasks instead of refactoring code that was correctly working since years. From his perspective my hard work was not considered as productive. Now I know I just was not able to recognize which part of system needs what kind of refactoring.

Here I must refer to Procent's excellent talk with Jarek Palka (in Polish). You can find out a lot of important hints regarding working with legacy code. Which is code you have just commited. One of hints is why compulsive refactoring may affect your projects and may cause inability to deliver.

After years I learnt that system I was working with was actually quite good thanks to good design and correct, efficient, event driven architecture. As a young developer, influenced by Clean-Code evangelism, I was focused on local parts, not on wider scope. Lot of implementation details were given to novice programmers and far from ideal. They SHOULD be better, but system was good enough to resist local code ugliness. Please try to learn what is important in your system first, before you start assuming that whoever wrote some piece of code was an ignorant or does not even know the programming language. Remember you have not yet transformed into The Great Red Dragon.

Sunday, December 4, 2016

Using groovy for tests

While I already has some exposure to dynamic languages - also those working under control of JVM, until now I had not touched Groovy, despite its presence on JUG meetings, conferences etc. I could of course see lot of value in tools like Spock, Geb or recently popular Gradle. However that was for me more a nice-to-have, rather than a must.
So with pleasure I am now discovering really nice points of using Groovy - as language that powers the tests and complements the Java main sources. It turns out that even with no frameworks mentioned above, you can benefit just from using Groovy in your test. Here are the reasons:
  1. You already know almost all of syntax. Old good Java code will probably just work. It is not like JavaScript, which is similar to Java just in name. Rest things you need you can find in documentation - which is quite good and answers most of my questions.
  2. Forget semicolons, alias imported objects, use list and map literals, multiline strings, use def. Almost like Scala.
  3. No more @VisibleForTesting. Groovy allows you to break object members visibility. Maybe it is not best idea to look into internal state of objects, but now you can. At least tested methods do not need to be kept in package visibility.
  4. Power assert. This looks just like Java assert. But output on your console when there is a failure... I just love this feature. Just take a look:
    Assertion failed:
    assert apiResponse.code() >= 200 && apiResponse.code() < 300
           |           |      |      |  |           |      |
           |           404    true   |  |           404    false
           |                         |  retrofit2.Response@42d0a255
           |                         false
  5. BigDecimal everywhere. 42.0 is not a float any longer. If your code has something in common with money you can feel relief.
  6. @TypeChecked. Why not just use compiler? With Groovy you can! It is so nice when machine corrects your errors.
I am not convinced whether Groovy is language of future :> But definitely worth trying.

Friday, October 7, 2016

IT Aristocracy to guillotine! How to treat IT head hunters?

Recently JUG in Warsaw published a newsletter containing quite an unusual link to an article created by a professional IT recruiter. The author is asking a very interesting question - why is she and her colleagues regulary offended by potential candidates, who are belonging to group of best educated engineers, holding lucrative position on job market. Top rated IT people. The "IT Aristocracy". I was touched. So I decided to appeal, to you readers.

Dear programmers!

I know you get 100 calls from desperate head hunters each week. I am aware, they are disturbing our flow of mind and taking us from zone away so we need to spend counterproductively next half hour to recover. But, hey, isn't that you who posted your phone/email on linkedin and N other jobboards along with shiny CV full of keywords? Is it more or less ridiculous than guys posting their ID into facebook? Have you expected that none of invited to your professional/social network recruiters ever call you? They may of course have sometimes be prepared to talk to you less than expected. Not knowing all that 3,4 letters acronyms and not knowing technology stack. That's right. They have no clue. That is why YOU are doing the job worth 6x more that what people you talk to by phone earn. They just have not dedicated half of their lives to profound depths of technological magic.
Nothing bad will happen also when we expose unprofessional behavior on the other side and talk about problems in our two-way communication. I am also aware not everyone is able to do it in such an elegant form as Rafal did.
Sometimes all of us have bad day, or just have been contacted by company which fired them 3 years ago. I can understand that each of us could behave less professionally in some conditions. Me too. I am sure that soft-skilled recruiters are also able to understand and forgive that.
But primitive, vulgar offence is not "unprofessional behavior"! That is not acceptable! Das ist unglaubich!!! That kind of freaks must be stigmatized!
Otherwise we, as the "computer people" will receive back from our society just hatred for being vulgar jerks. Just imagine that could have happened just before this or next government is run out of (our...) money and seeks for new taxpayers. Who will be the most unpopular group in our society? Whose heads will that revolution demand? Are you ready for fiscal guillotine?
One day, sooner or later, you will be looking forward to change your job. One day you will wish the head hunters to call you, and treat you not like just one more CV from million. That people are really able to help you get where you want. Unless you're a dick.
TL;DR If you are frustrated buy the rubber duck! Never offend head hunters.

Thursday, May 19, 2016

Changing merge request target branch in gitlab

Yes, it's possible with API usage.
There is even a script that chamges target branch with minimal configuration.

Gitlab is increasingly popular alternative for Github, commonly used by corporations that are able to save monthly fee for Github. It's open source project and front end and its usability seems to be still behind commercial and successful predecessor.
In my opinion Merge Requests are functionality that is exceptionally affected by suboptimal design. You just need to get used to it and train yourself not to click big green 'Click me' button, which causes MR to be merged. And changing the target branch of merge request seems to be impossible.
Thanks to careful readers among my colleagues, I have found out that this option exists, however only way to do it is to use Gitlab API. The API (similarly to Jenkins) utilizes concept of private token. It is a secret that allows a user to authenticate using unique token instead standard username and password. Maybe it is not most secure way, but certainly that is easy and convenient. Each HTTP request to Gitlab API that is performed in some user context, must contain that private token. It may be sent as a header or url parameter. Depending on your deployment configurationone of that ways may or may not be disabled ;> Url parameter worked for me quite well.
To edit a Merge Request we need to know project it relates to, as well as MR identifier, which is not same as visible in GUI (that one is referred to as iid, unique in project scope).
Operation will take 3 HTTP calls. First is ment to retrieve project id by its name.
GET /projects/search/:name_to_find
I assume there are no more than one projects found by given name.
Second will convert id from GUI to MR id.
GET /projects/:id/merge_requests?iid=42
or you can select manually from all opened MRs.
GET /projects/:id/merge_requests?state=opened
Knowing project id and MR id we can now update target branch by sending simple json object carrying new target branch as a value.
PUT /projects/:id/merge_request/:merge_request_id
{ "target_branch": "the_target_br" }
In my case I had to remove plural form `s` from merge_requests in url (that was different from API documentation, but I assume it's change in some other version of Gitlab). I was happy this option was possible, however I must admit I had to write the script to make the changing user friendly.