Common Sense Driven Development

Nowadays every day or week we’ll getting new framework or tool everyone is hyped about. https://dayssincelastjavascriptframework.com/ is a great example of trolling JS people about that. Development is a lot about this new and exciting technologies but day to day life is not as simple as using the cutting edge, shiny things.

The double edged sword of Cargo Cults

For the definition I’ll fall back to good old Wikipedia:

(…) attempt to emulate more successful development houses, either by slavishly following a software development process without understanding the reasoning behind it, or by attempting to emulate a commitment-oriented development approach (in which software developers devote large amounts of time and energy toward seeing their projects succeed) by mandating the long hours and unpaid overtime, when in successful companies these are side-effects of high motivation and not requirements.

As managements issues are important, I’d like to focus more on first part of the definition.

There are from time to time new tools and practices released and world is getting crazy. I’d say React.js is one of them. Other may be Netflix Cloud tooling or good old Docker and Kubernetes on the Dev/Ops side.

And don’t get me wrong, I like them all. The difference between what you can use and what to use to make your project successful. It’s context of making decision being more important than decision itself.

Having technology solving your problem is great but you may fail because of very steep learning curve. Tool may be not supported in few months or new version will be released and you’ll have nice and shiny legacy code even before release.

What to look for

  1. Make sure you’re not trying to use the same hammer for every nail – there Is a lot of technologies and some are better in some tasks than other. Like PHP and multithreading or long running processes. You don’t want to do this to yourself. Maybe better solution will be to get people to learn a bit of Java of node.js to make this subsystem?
  2. Support – is the library you want to use “mainstream” enough for you to use it and be sure it will still exist in few years. From other hand ask yourself if you really need to use library for some very simple functionality you can write in about 20 seconds.
  3. Learning curve – Check with your team new solution can be understood and implemented correct way. As an example I can take CQRS and Event Sourcing, which are quite complicated topics and used mostly in enterprise environments. Anyway people often think it’s silver bullet for their problems and going through with it. Often they are right but as it needs time for people to learn about it’s problems it’s better to take middle ground and tart with just emitting events before switching to ultimate solution.
  4. Look at yourself first – There is a lot of companies and a lot of ideas. None of them is a silver bullet. There are also old, “bad” ideas. Like monolith. And those bad ideas are good in some cases. Like when you have quite big application to write in small team.
  5. Take authorities with grain of salt – aka Cargo Cult of the person. It happens when opinion of one person becomes opinion of the community. You know examples of that from global politics. And I’m not saying those people are wrong. They are just preaching one solution which they like. And it doesn’t really matter if it’s correct solution of programatically correct. Their acolytes will quote them in every meeting. Argument of need and correctness of the solution will be pushed back because of argument of well known person having opinion.

There is only one correct answer – it depends

I’d assume there is as many styles of coding and tools as developers in the industry. Some are better than others. Some are evolving and getting better and better. Some are legacy at the idea level but still generating revenue for the company.

Bottom line is that there is no single answer to a problem. Context of the problem changes everything and I think it’s the most important thing to look at when making technical and process decisions. And then choose which hyped tech use in the next project.

CPS #1 – Spring Boot, Logback configuration

This entry is part 1 of 1 in the series Copy/Paste Snippets

Welcome to Copy/Paste Snippets!

It’s the first of the series of articles without much narration and a lot of code snippets. Goal is to have nicely searchable list of snippets of commonly used classes and/or configuration files.

If you find error or improvement please let me know and we improve it :)

pom.xml

Configuration file src/main/resources/logback.xml

Use it in you class

Imports:

And at the top of your class:

From PHP to Java

So I’m a Java developer for some time now and I’d like to share with you few things about the process I went through. I want to point few differences in the process, libraries and way of working between the languages.

A bit of background

I’m a senior developer and I was writing Java for some time already before I switched. Usually side projects but not that small. Turns out, as I was already writing quite complicated code structures in PHP 5.6/7 there was no big change when compared PHP to Java.

Another nice surprise was Symfony and Doctrine to be PHP clones of Spring and Hibernate. Knowing both frameworks helped me a lot with catching up with RAD in Java.

To be hones a lot of currently hyped tools for PHP (not Laravel) are based on Java tools. Just like composer being replacement for Maven/Gradle.

So what’s the difference?

First is deepness of the language. There is way more than associative array. Java, for each collection, has multiple implementations with different characteristics so you need to learn what is what.

OO model has also way more to offer. At the beginning you can play with PHP style of composing classes but when you have enum as first class citizen of the language you just want to use it :) Inner classes, static classes and I haven’t event started talking about streams or generics. On top of that there is concurrency, parallelism and need, or lack of need, of being thread safe.

A lot of stuff and I’m still discovering new keywords of the language from time to time.

Is it that bad?

Not really. Writing PHP in Java is very easy. It works a bit worse the other way around. Simple Spring apps are easy and fast to write. As I landed in the team of experienced developers it took me few weeks to learn what I was missing and is used in day to day work.

I think the biggest change for me was move from Composer to Maven. As PHP don’t require a lot of build features Composer is nice and simple. Maven on the other hand is a 20 years old monster with XML configuration. It’s not bad but it takes a while to get used to. After some time and few mistakes it’s pretty straight forward in use and there is a lot of tutorials.

Documentation

At the beginning of every journey into new language you’ll follow the Stack Overflow Driven Development methodology. And it’s working very well in Java as it’s one of the most popular languages in the world. 99.9% questions you may ask will be answered there.

When it comes to libraries it’s more hit and miss. Sometimes documentation is amazing and you can use it to basically copy/paste base solutions. Otherwise you end with digging through big manuals written in very corporate way and without many examples. From time to time you’ll probably end up reading the code as well. In my opinion tho, it’s the best solution anyway. I learn a lot faster by reading other people code than by reading dry manuals or books.

Tooling

As I mentioned before, Maven and Gradle are monsters of build. You need them as it’s not only about FTP files to the server ;) You need to compile your code and run it as binary package. Learning curve is steep but in few weeks of daily tasks you’ll learn a lot. Or at least enough to know where to look for template for new project.

IDE is great help. As you have a lot to choose from for free, like Eclipse of Netbeans, I say inteliJ from JetBrains is simply the best. Community Edition let you work on Java for free as well but without some features. I own my own licence for Ultimate version and it’s the best £20 I’m spending a month. Especially as a polyglot, where I’m using quite few languages supported by it.

What next?

Kotlin! You probably heard about it when Google made it one of officially supported languages for Android. It will soon be supported fully in Spring. With full interop with Java and more Scala-like markup it’s amazing tool to code. And it’s fun.

JVM has also Groovy, Clojure, Scala and a lot of other languages to play with. They will let you learn more about coding and still use your new favourite platform.

Summary

It’s more a journey than travelling from place to place. As I learned quite few things about language and libraries there is still a lot to do. Stay tuned. I’ll post another part in few moths on the next iteration of my learnings.

From PHP to Java #2 – Development speed

In my previous article I was hyped about getting insane throughput in my first app. Today I started another app and development speed, using quite difficult architecture, surprised me again.

Spring Boot

As you could expect after previous entry I’m focusing on Spring Boot as it delivers everything more or less out of the box so I don’t need to worry that much about additional tools. It’s also similar-ish to Symfony2/3 so I feel close to home.

Building new app today

Last Thursday I visited London’s Java Meetup which was about CQRS applications, event sourcing and how to use it in microservices. Great talk which motivated me to try to build Event Sourced app.

There is nothing better for the weekend than building application in language and environment you don’t really know what well using difficult technology you’ve never used before. Should give me weekend or two of fun playing with it.

Sadly my project, Java and tools said

no

what’s actually good thing.

Spring & Axon Framework

It’s now 14:23 and I’m basically done with fully event sourced API part with enough materializers to create all views needed for the MVP. It’s kinda crazy for me.

Configuration of Axon Framework (CQRS framework for Java) is very easy. Few services registered in container … I mean Beans registered in Application Context and it’s done. Domain objects working as Command Handlers, listening to events and JPA (like Doctrine) read layer based on MySQL. Everything worked on first run and as it suppose to.

Event sourcing has great part in development speed and I don’t need to care about database at all and I don’t even think about it before I finish all domain processes. After that there is time to reflect what I want to show to the user. What views I’ll have in the application. And how I want to put those data so they will be easy to read.

Amazing. Again.

Conclusion

I’d really like to find some downsides, things I won’t like in Java. I know about Websphere and old “Application Servers” ways and that they are terrible but it’s past. Currently I feel like Java is taking back ground after years of being “slow” and “too corporate”.

I remember doing research years ago, when I was looking for language to learn after seeing no future in Delphi. I’ve chosen PHP as it was growing and for being nice and fast to develop websites. Java felt “too big for task”. Today it seems to be other way around.

From PHP to Java #1 – First production-ish application

So I’ve got a task to check difference in performance between Silex app, Symfony 3 with all goodies and JAVA/Spring Boot. Results surprised even me.

TLDR; because there will be separate post about that – PHP is obviously slower than Java and with proper use of opcache there is no difference between Symfony 3 with JMS Serializer and Doctrine and 60 lines unmaintainable app in Silex.

But it should be about Java. Stack Driven Development with Spring Boot took me maybe 1 or 2 hours in comparison about 1h with Symfony. As I’m still newbie I’m not optimising what so eves as I have just slight idea what I’m doing. App is working exactly the same way. Let’s do testing then.

Environment

I’m having all the apps on my poor Vagrant VM with not so much processing power and I’m using DB which is somewhere in the infrastructure. No cache, no in-memory storage. Everything is as dumb as possible ;)

For testing I’m suing Gatling.io. Great tool with Scala based DSL for test scripts.

We’re set up so let’s do some testing. Goal is to kill all the apps and check what’s the most they can handle.

The test

So PHP died before 50 req/s. It was so disappointing I haven’t checked exact number. Note I’ve done all optimisation on opcache and fpm normal user would do. Nothing crazy tho.

Let’s do Java then. same 50 req/s. No sweat. graph flat at response time about 10ms. Boring. As naive I was pushing by 25 on iteration of test. It took some time but finally got some struggle after reaching 250 req/s. At this point I’m shocked and confused. Mainly because even if I got longer response time I’ve got all the response data when PHP started to throw 500’s and 504’s in my face.

Next step my friend from the office came over and we’ve done some system optimisations on my VM. Then I’ve just

(╯°□°)╯︵ ┻━┻

Last successful test we’ve done got us average 360 req/s. On my VM, on small and slow-ish Mac Mini. And no errors. after that point, when I bumped test to over 400 req/s I simply killed TCP driver on my mac, not application. T bloody CP connections couldn’t be made on that rate on my host. App was fine.

Conclusion

I don’t want to say PHP is bad. It’s still good for frontend and not that high load. But if you really want performance you shouldn’t waste time on PHP IMO. Spring Boot feels like Symfony, all the good tooling is basically done for you.

You’re gaining like 7-8 times better performance out of the box with rock solid stability (I haven’t got even one 5xx from Java app).

Well, choice is yours if you gonna like it.

From PHP to Java #0 – Beginnings are hard

As I’m slowly working my way up from PHP to Java. I think I can write few words about my experience on that topic.

For now I know basics of basics of the language. It’s not that different and more or less the same with different markup in every language. You can’t reinvent loops of variables :P

Difficulty starts now when I want to write simple app in good way. Currently i’m using Symfony 2, Doctrine, Tactician Command Bus and Twig. Switching to do the same thing in Java means I need to choose similar libraries from java world. Another reason I have to use them is market. To get a job you have to know your tools. You have to know the best tools. And you have to know them well.

Now I’m in the stage when “I have no idea what I’m doing” but after research I decided what will be my starting point.

Web framework – Spring Boot – simplified version of Spring. No XML in favour of annotations and code configuration. From what I see it had good feedback from community and it’s similar enough to Symfony 2 to let me get some results in reasonable time.

Database – Hibernate – The heaviest ORM in Java, probably. But it was a source of inspiration for Doctrine 2 and few years back I was working with it’s .NET clone so I hope I will have only problems with configuration and markup.

Command Bus – Axon Framework – Here is the tricky part. Axon is a huge step from DDD light where state is saved on every request to Event Sourcing. Maybe it’s possible to simplify but I’m not sure at this point. Also Google and Stack told me it’s production proven by banks etc so it can be good direction even when learning curve will be very steep.

Templating – Thymeleaf – Default for Spring Boot. Not bad XML-like markup. For what i need it’s Googleable to extend templates and create reasonable DRY structure of template files.

It’s a lot for a first step into new technology. I spent last month-ish trying to figure out if I can make it in smaller chunks but I can’t see a way where I’m not doing dummy “hello Hibernate” examples.

Plan is to write clone of my ftrack-ddd app on the stack I mentioned in this post. As much as it will be possible I’ll be writing posts about my progress.

Programming assingment #1 – Booking tickets

This entry is part 1 of 1 in the series Programming assignments

Programming is fun. Often I have problem to find a issue I want to solve. With that in mind I start to publish my programming assignments I’m making for myself.

Today we will have simple microservice to manage concurrent ticket bookings for “events”. Let’s say Iron Maiden announces small concert for 100 people on all their social media channels with specific time of booking. Our problem is to handle big spike of traffic at the time booking become available.

Acceptance criteria:

  • Application creates event with poll of tickets of specific size
  • Application allows to reserve a ticket(s) for 1 hour
  • Application allows to transform reservation into real ticket
  • After Reservation times out and spot is available for other user
  • It’s impossible to reserve more tickets than available in the pool
  • After last reservation is transformed into ticket event is closed

I’m leaving proper communication of error out of scope but it’s always good to have framework which allows to communicate about state of the system to external clients.

I’m leaving rest up to you how you want to implement solution for this problem.

As always in software there is no one good answer as it’s “possible” to handle this kind of problem basically in any language with out without external libraries or services.

If you have any questions don’t hesitate to ask. I’ll do my best to answer as soon as possible.

Enjoy and good luck coding!