Alessandro's blog

Thoughts on software development

Dec 7, 2017 - What to get after Wine?

What to get after Wine? I’m not talking about drinks, but instead I will talk about the Wine project. The project is a kind of “emulator” that let you run Windows applications on Linux.
It is not a real emulator, like the name itself explains with a nice recursive acronym: Wine Is Not an Emulator. It’s a compatibility layer that translates Windows system calls to Linux ones, so the applications really believe to be run inside Windows ;-)

This is so much of a cool idea that Microsoft did the same thing backwards: Windows Subsystem for Linux. I would have expected a joke involving drinks or desserts, at least.

After installing WSL from Control Panel, I downloaded Ubuntu bash from Microsoft Store. Python and Ruby work perfect!
I’m running Jekyll and [Jupyter] (https://www.digitalocean.com/community/tutorials/how-to-set-up-a-jupyter-notebook-to-run-ipython-on-ubuntu-16-04) on my Windows10 laptop.

Bash terminal runs on its own Linux like filesystem, to get accesso to the computer’s disk simply navigate to /mnt/c/ directory.

I’m very surprised of this Microsoft product. It is not clear to me why they decided to support another Operating System architecture in Windows (maybe the popularity of Cygwin?).

Oct 17, 2017 - Mining Monero for free

Monero is cryptocurrency which uses a Proof-of-Work algorithm that runs on regular CPUs and GPUs. Proof-of-Work, in layman terms, means that there must be miners who “mine” each block, and get rewarded when they solve a particular “equation”.

Mining is a profitable activity, but you must pay your electricity bills… unless someone else offers you free CPU cycles! In this article I will show how to mine Monero for free.
But who and why is offering free computing power? There are some competitors in the market who want to hook developers in. Once you start with and know a platform, you will probably stay there for future projects.
In the meantime, let’s try the platforms!

Azure

There’s a Free plan with Microsoft Azure, they offer 200$ / 170€ to try their service. It is about 1 month of a 2 vCPU Windows server, or 2 weeks of a 4 vCPU Windows server.
Just install a miner and start free mining!

AWS

The Free tier of Amazon AWS offers 1 year of a 1 vCPU Windows server. Unfortunately it’s throttled, so for most of the time it won’t mine. There’s a workaround though: write a script that launches the miner, checks the hashrate every some minutes and if it’s too low, kills it and waits a couple of hours for the CPU to get back to full speed.

Which miner?

The fastest CPU miner I know of is xmr-stak-cpu. Follow the intructions to enable Large Page Support, it boosts performance a lot.
I sent a pull request to enable stdout flushing, so it’s possible to read its status even when running in background through a pipe. To have the miner report its hashrate set the following two options in config.txt:

"verbose_level" : 4
"h_print_time" : 120

If you know other platforms that offer some kind of free trial of their cloud computing, let me know ;-)

If you liked the idea and feel like tipping here’s my address ;-)
49WuxuU83x7ERhTU5fAvhhJKpsCaSujpLD4EQgS2jYedduimvSnRS7TPieiKvpHNhuLxEoD5Kq18oGGBWjdss1yJPsHhv1q

Jan 20, 2017 - Clean Code

I just read Clean Code by Robert Martin, a very insightful book on software development. I thought about it for a while and decided to write a little review.

TL;DR version

The book is an interesting read, even if you already know some of the ideas contained. Do you professionally develop software? Go read this book immediately!

Long version

Let’s start with a quote I particularly like: “Programming is a craft more than it is a science. To write clean code, you must first write dirty code and then clean it.
It may sound obvious, but it isn’t. The act of “cleaning” requires a precondition: you must know by experience when the code is dirty and decide to clean it up. Such experience comes from passion, from hard work and from lessons learned from master craftsmen. The author tries its best to be one of them.
As our experience grows, the meaning of “clean” will change to reflect the new lessons learned. Let’s look at some excerpts.

Software systems should separate the startup process, when the application objects are constructed and the dependencies are “wired” together, from the runtime logic that takes over after startup. […] One way to separate construction from use is simply to move all aspects of construction to main, or modules called by main, and to design the rest of the system assuming that all objects have been constructed and wired up appropriately.

We all know some of the usual suspects around: Spring, OSGi Blueprint, Unity… There are many frameworks for inversion of control and dependency injection around. What we get from them is the ability to work with dependencies without checking for NULLs, all object are created and ready to be used. What if something is missing or not constructed? We are going to fail fast of course!
We need dependency injection even if our applications supports one kind of database or a single remote service. Because we will need at least a second database engine: a mock database engine for testing purpose.

Test code is just as important as production code. It is not a second-class citizen. It requires thought, design, and care. It must be kept as clean as production code. Tests are very important becase tests enable change. Tests let you know that you didn’t break the expected behaviour. Tests let you know that you didn’t break the expected behaviour of tested code, that’s why test coverage is important too.

Another quote I like about tests: Test F.I.R.S.T.
Fast - Tests should be fast. They should run quickly.
Independent - Tests should not depend on each other.
Repeatable - Tests should be repeatable in any environment.
Self-validating - The tests should have a boolean output. Either they pass or fail. You should not have to read through a log file
Timely - Unit tests should be written just before the production code that makes them pass.

There’s nothing as relaxing as the line of green dots of passing tests. Red ones should be fixed immediately, because we want everything green to feel great. How much time would you want to wait after fixing the code to see everything green again? No more than some seconds, I bet.

Tests should be structured as follows:
The BUILD-OPERATE-CHECK pattern is made obvious by the structure of these tests. Each of the tests is clearly split into three parts. The first part builds up the test data, the second part operates on that test data, and the third part checks that the operation yielded the expected results.

Also known as Arrange Act Assert pattern. Almost any testing framework offers its own way to hook into test lifecycle to setup and clean any resource or data required. Assertions can show customized messages when they fail.

Open-Closed Principle: Classes should be open for extension but closed for modification. We want to structure our systems so that we muck with as little as possible when we update them with new or changed features. In an ideal system, we incorporate new features by extending the system, not by making modifications to existing code.

Is there anyone not yet familiar with Decorator and Adapter patterns?

There’s a lot more in the book than these concepts. For example, there are examples of “bad code” and how the author proceeds in refactoring it.

Sep 13, 2016 - Load a resource into a String in Java

Recently I needed a simple method to load a file from resources into a String. Even if it looks like a very simple task, it usually involves many awkward transformations and exceptions catching.
This solution uses a Scanner to read the entire InputStream into a String object.

private String loadFromResources(String resourcePath) throws Exception {
    try (InputStream is = getClass()
            .getClassLoader().getResourceAsStream(resourcePath)) {
        Scanner sc = new Scanner(is);
        sc.useDelimiter("\\A");
        return sc.next();
    }
}

The code takes advantage of Java7 AutoCloseable interface and try-with-resources statement to reduce boilerplate code. In case of exceptions the underlying input stream is closed automatically.

It works by setting \A as a delimiter. This regexp Pattern looks for the “beginning of the input” token which of course can’t be reached and causes all the input to be picked up by next().
Using the actual class’ classloader works very well in OSGi environments too (e.g. Apache Karaf, JBoss Fuse).

About me

My online presence can be proven by the following social accounts ;-)