Alessandro's blog

Thoughts on software development

Feb 24, 2016 - Fail fast, code less

I’m embracing the “fail fast” philosophy in every project I can. What is it exactly? It is about gracefully failing when something goes wrong, and do it as soon as possible. A suggested and more in-depth article is the original “fail fast” (pdf) by Martin Fowler.

I have discovered that this approach works really well in modern Agile development, as it helps in reducing bugs and improves robustness. This sounds good but how to do it in practice? I would like to share my experience with you.

Configuration

Most applications need configuration, from a file or other sources. Often it gets complicated, long, and may be in a format prone to errors (ini files, java .properties). Environment variables can get messy too: you add one in the software and forget to set it in the CI server.

Then defaults come into play. Default number of threads for a thread pool may be a good idea. And then you are tempted to add more default values, for example for date intervals, paths, and so on. I say defaults, when used, must be explicitly stated in the logs.
The problem with defaults is when the rules to decide them are arcane or complex, or a default value does not exist (API keys are a good example). The application cannot work correctly, so it’s best to stop when a configuration property is invalid or missing.

Log with Five Ws in mind

Every configuration issue must be logged before stopping. This holds true for other issues as well. Logging is not as easy as it seems, I encountered many unhelpful lines like An error occurred. When writing a log statement it is important to remember the five Ws principle.

  • Who: stacktraces identify the source of the problem, don’t print them but always add the exception as a parameter in the log statement
  • What: add details about arguments and/or variables being processed and a meaningful description of the operation
  • Where: usually the log library provides class, method and row number
  • When: log staements grab the timestamp automatically
  • Why: discovering this is our job

A well written log line will save you lots of time debugging. Remember:
Bad: Configuration error.
Good: Configuration error in config.xml, property 'numThreads' must be an integer, value '45t' not allowed.

Global error handler

Wrap units of execution in try-catch block (or the equivalent in your language of choice). For example web requests or background threads should be wrapped. First, you are free to throw exceptions without the fear of wreaking havoc on the rest of the application. Second, you log the exception. This helps a lot when using third party libraries. Not every library correctly log unhandled exceptions, or they may get logged on another file or stream and pass unnoticed.

What’s the price?

The real price to pay for the ability to “fail fast” is not the time it takes to write log statemens or exception handling, as it may seem. The real price is gathering requirements. It’s hard to decide what to do in less trivial situations. Analyzing situations can be time consuming, client’s responses may not be immediate. Such small decisions may have a huge impact on the rest of your project. I think that having a rock-solid running software with a validated configuration will save lot of headaches and time in the long run.

I hope these suggestions will provide helpful.

Feb 15, 2016 - ConEmu: a better windows terminal

One of the projects I am working on lately involves a very basic Windows tool: command line terminal.
It is rather painful to work with cmd.exe on Windows: no resizing, no easy copy of rows, no tabs, no coloring… so I did some research and found this gem: ConEmu.
The description is quite interesting: ConEmu-Maximus5 is a Windows console emulator with tabs, which presents multiple consoles and simple GUI applications as one customizable GUI window with various features.

ConEmu looks pretty cool!

I gave it a try, it is a huge step forward. ConEmu really does it job very well in two spots I needed:

  • Tabs (hit CTRL + Tab to switch between consoles) and tabs placement
  • Easy selection and copy of text (not the clumsy block selection of cmd.exe) with both mouse and keyboard

Configuring tabs is not intuitive. I scratched my head a few times before figuring it out. From Settings window, go to Startup->Tasks and press [+] button to add a “command group”, insert the following commands. The split screen magic is done using -new_console argument.

> cmd.exe /k "%ConEmuBaseDir%\CmdInit.cmd"

cmd.exe /k ""%ConEmuBaseDir%\CmdInit.cmd"  -new_console:s50V"

An alternative way would be to manually edit ConEmu’s config file (default C:\Program Files\ConEmu\ConEmu.xml) and add the following piece:

<key name="Task11" modified="2016-02-11 09:54:19" build="160124">
    <value name="Name" type="string" data="{Shells::Due}"/>
    <value name="Flags" type="dword" data="00000000"/>
    <value name="Hotkey" type="dword" data="00000000"/>
    <value name="GuiArgs" type="string" data=""/>
    <value name="Cmd1" type="string" data="&gt; cmd.exe /k &quot;%ConEmuBaseDir%\CmdInit.cmd&quot;"/>
    <value name="Cmd2" type="string" data="cmd.exe /k &quot;&quot;%ConEmuBaseDir%\CmdInit.cmd&quot;  -new_console:s50V&quot;"/>
    <value name="Active" type="long" data="1"/>
    <value name="Count" type="long" data="2"/>
</key>

Jan 12, 2016 - All about MQTT protocol

Summary

MQTT is a publish-subscribe messaging protocol, designed for devices with low resources and small bandwidth available. It’s widely used in “Machine to Machine” and “Internet of Things” communication. Each client is connected to a central server, called broker. The broker manages the dispatch of messages. Messages are published by clients on different hierarchical topics, and received only by clients who subcribed to the particular topic. Examples of topics are “/device/sensor”, “das-sade321/asds da/”.

Topics cannot contain the characters + # and / since they have special meanings. The forward slash / is used as level separator. The + is a wildcard used in subscriptions and it means “one level”. The # is a wildcard used in subscriptions and it means “any number of levels”.

The protocol itself does not specify a payload format. One interesting feature is that the broker can notify when a client disconnects using a mechanism called Last Will and Testament. Protocol specifications (v3.1.1) are freely available online.

MQTT Essentials

If you wish to read more I suggest proceeding with “MQTT Essentials” by HiveMQ team.

  1. Introducing MQTT
  2. Publish & Subscribe
  3. Client, Broker and Connection Establishment
  4. MQTT Publish, Subscribe & Unsubscribe
  5. MQTT Topics & Best Practices
  6. Quality of Service 0, 1 & 2
  7. Persistent Session and Queuing Messages
  8. Retained Messages
  9. Last Will and Testament
  10. Keep Alive and Client Take-Over
  11. MQTT over WebSockets
  12. Wrap-Up

Public brokers

There are some brokers that are public. They can be used by anyone for testing and development purposes. Be careful not to publish sensitive data!
Here it is a list of publicly available MQTT brokers. (checked on 2016-02-27)

Address Port Description
broker.mqttdashboard.com 1883 MQTT, not encrypted
iot.eclipse.org 1883 MQTT, not encrypted
iot.eclipse.org 8883 MQTT, encrypted TLS v1.2, v1.1 or v1.0 with x509 certificates
mq.thingmq.com 80 MQTT / Websockets, not encrypted
mq.thingmq.com 1883 MQTT, not encrypted
mq.thingmq.com 8883 MQTT, encrypted SSL
mqtt.kgbvax.net 1883 MQTT, not encrypted, max message size 64 KiB
mqtt.kgbvax.net 8083 MQTT / Websockets, not encrypted, max message size 64kiB
test.mosquitto.org 1883 MQTT, not encrypted
test.mosquitto.org 8883 MQTT, encrypted TLS v1.2, v1.1 or v1.0 with x509 certificates
test.mosca.io 80 MQTT / Websockets, not encrypted
test.mosca.io 1883 MQTT, not encrypted
test.mosquitto.org 8080 MQTT / Websockets, not encrypted
test.mosquitto.org 8081 MQTT / Websockets, encrypted TLS v1.2, v1.1 or v1.0 with x509 certificates

Dec 24, 2015 - fr-08: .the .product

Many years have passed since I first discovered this “demo”, and it still fascinates me.
I’m not part of any “scene” and I don’t remember how I stumbled upon this amazing piece of art. But sometimes I really like to watch it again.
Futuristic architecture featured in the demo - Courtesy of pouet.net

It’s an animated 3D scene rendered in DirectX, with music and credits, about 4 minutes long.
Find it here: http://farbrausch.com/prod.py?which=17 (the download link is broken at the moment)
Download it from here: https://files.scene.org/view/demos/groups/farb-rausch/fr08_final.zip
What’s so special about it? It’s an executable file smaller than 65536 bytes. About an average .torrent file, less than a Word document, way way smaller than a mp3 song.

If you know a little about programming and 3D graphics you are going to ask yourself how all of this can be compressed into 64KiB. Textures take space, meshes take space, music, meta data, all takes space… compression alone isn’t enough.

In one of the dedicated sites lies the explanation. There’s no compression.

  • Textures are not stored, they are generated: the executable stores the algorithms used to paint textures. (this is done in the initial loading)
  • Meshes are generated with primitives (cubes, spheres, triangles) attached together and animated.
  • Music is a MIDI track rendered by synthesizer.
  • The executable is stripped (all extra metadata is removed)
  • Data is stored and/or packed in custom formats
  • There was still enough space for writing credits

It was Windows XP times when the demo came out. Now with Windows10 and DirectX11 the demo works but the full screen is cropped and I see the window title and borders ruining the black background. Anyway it’s awesome. That’s exactly what I admire: knowledge, hard work, good team and quality.

About me

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