Monday, September 29, 2014

Book review: SQL Antipatterns

In SQL Antipatterns, Bill Karwin does a great job in explaining how to make efficient use of the relational model, instead of abusing it like it's usually done. Many developers abuse relational databases using antipatterns such as ID required, entity-attribute-value, index overkill, and so forth (you should read this book without second thought). Some developers go as far as trying to implement a search engine based on the LIKE keyword.

All those are examples of not using the relational model properly. If we want to bypass referential integrity or save everything in one table, then we should not use a relational DB in the first place. Cursing on the performance of a relational DB when it's not properly used is very wrong.

Sunday, May 4, 2014

Being a technical reviewer (again)

A few months ago I experienced (for the first time) how it feels to be part of a technical reviewing team. I reviewed a packt introductory book about Design Patterns.

Today I'm glad to see that another packt book, of which I was for once again one of the technical reviewers, has been published. The book is called Mastering Object-oriented Python. It's a book that focuses on writing OOP code in Python 3.

I would recommend this book to all Python programmers, both beginners and advanced. It covers all aspects of the language (to mention a few: special methods, unit testing, decorators, serialization, etc.) and shows different possible designs, explaining the pros and cons of each design. What I really like is that the code in the book is written in a Pythonic style, and the author makes a good job at explaining how Python differs from Java/C++.

A few warnings: This is a big book (~ 600 pages). You can read the whole thing, but I believe that it will be much more useful as a handbook. Also note that the book assumes familiarity with Python 3 and Design Patterns.

You might wonder why I accepted to do again a technical review. Some people find technical reviewing a waste of time. But I disagree. To become a good programmer, you need to read a lot of code instead of just writing. In fact, programmers tend to read much more existing code than write new code. If reading code is important, reading good code is much more important. And I believe that the code in this book is well written.

Saturday, April 19, 2014

BASH: syntax error near unexpected token `('

After making some portability and readability improvements to shell-utils, I used BASH, sh, and dash to test it. While sh and dash were fine, BASH returned the error:

line 358: syntax error near unexpected token `('
line 358: `ls ()'

That is strange. BASH usually introduces shell portability issues because of the extra features it provides, so I would expect to have a problem with the other shells.

It turns out that BASH did a pretty good job with reporting the source of the error. Note that shell-utils redefines a few everyday commands as functions, to make them more verbose and secure safe (eg. ls becomes ls --color=auto, rm becomes rm -i, etc.). But usually those everyday commands are already defined as aliases in .bashrc. Aliases are evaluated before functions, and defining a function that has the same name as an alias is not allowed. And that's what BASH is trying to tell me in the error message. Commenting all aliases in .bashrc fixed the issue :)

Saturday, March 8, 2014

Joy of Coding 2014 - My impressions

I haven't been to a conference for years, but this year I decided to join Joy of Coding. And I don't regret it!

The conference started with a keynote by Dan North: "Accelerating Agile: hyper-performing without the hype". Dan described what he learnt about Agile while working in the trading domain. The most interesting advice that I kept from his presentation is that being a good programmer is not enough: What really makes a difference is to become a domain expert. For example, if you are working as a stock market programmer, your superior programming skills don't matter if you have no clue about what the numbers mean.

Next, I joined the "Let Me Graph That For You: An Introduction to Neo4j" workshop, by Ian Robinson. The first part of the workshop was an introduction to Graph Databases and Neo4j. At the second part we used Neo4j and its query language Cypher to create a few graphs and query them. I am impressed by how easy is to get started with Neo4j. I find its web interface very intuitive. We had a few questions for Ian (S = Sakis, I = Ian, O = Other conference participant):
  • S: Is there any relation between Neo4j and RDF?
  • I: Not really. In RDF you typically end up with more connections because everything is modelled as a triple. But there are libraries that can extract a Neo4j graph as RDF.
  • S: Are all the common graph algorithms (eg. SPF, BFS, Bellman-Ford, etc.) available?
  • I: Most of the well-known graph algorithms are available, and furthermore there is a Java API that is exposed and can be extended with your own algorithms.
  • S: Is Cypher case-sensitive?
  • I: Partly. The identifiers of a query are case-sensitive, but the rest parts of the query aren't.
  • O: Are there any cases where RDBMS should still be used instead of Graph Databases?
  • I: If you have tabular data and you want to focus on set theory operations (eg. union, intersection, etc.) an RDBMS is preferable.
 I'll definitely look more into Graph Databases and Neo4j.

The next keynote was "Contravariance is the Dual of Covariance", by Erik Meijer. I'm not very familiar with Reactive Programming and Rx, thus I couldn't follow everything. But at least I enjoyed the jokes and the funny examples that Erik used. Using Scala as a reference, he explained the meanings of covariance and contra-variance, and showed how they can be used to create reusable code.

The second workshop that I joined was "An Introduction to Actors and Concurrency", by Michel Rijnders and Matthijs Ooms. The first part of the workshop was basically an introduction to Erlang, thus nothing special if you are familiar with Prolog. The fun started at the second part, where we experienced how straightforward is to communicate asynchronously through the network using Erlang's actor model and message-passing primitives.

Last but not least was the keynote "The Tao, of the Joy, of Coding", by Dick Wall. This was by far the most inspiring keynote. Dick, using ancient Chinese philosophy as a reference, talked about many interesting topics, including programming honesty (saying "I don't understand this" and learning from your colleagues is a good thing), looking back as a programmer (eg. if you used your programming skills to find a cure for a disease you really changed the world), and getting a life (being proud about working until 2 AM is a very bad mentality).

All in all, a great conference that I will keep in my agenda every year!

Thursday, November 28, 2013

My first book review

During the last month(s) I participated in the review process of a book about Design Patterns in Python. I am happy to see that the book has been released. The title of it is Learning Python Design Patterns.

Reviewing a book is definitely not harder than writing one, but that doesn't mean that it's not challenging. The comments must be clear in context, useful, and not offensive. The process requires a considerable amount of time since all comments/remarks/notes must be backed by research and reliable references.

Nevertheless, I enjoyed the whole procedure which thanks to the people of packt publishing (packtpub) was flexible and clear. Plus, I'll receive a free hard-copy of the book that I reviewed and a copy of my favourite packtpub ebook. Not bad!

In the future I hope that I'll have the chance (and the time) to write my own book about a topic that I like.

Sunday, July 28, 2013

mbed USB voltmeter

USB communication with mbed is easy. That's because USB is the default protocol used to transfer programs from a computer to an mbed.

In this prototype I measure the analog value of a pot, I convert it to voltage, and finally I use USB to print it on my computer's screen. That's a simplistic voltage meter! Here's the schematic:


USB voltage meter schematic. Image courtesy of Bert Van Dam


And a picture of the actual circuit:

USB voltage meter circuit

Note that the pot is used just for testing purposes. It can actually be replaced by any other part. Just make sure that you're not trying to measure voltage > 3.3 volts, or your mbed is in danger!

The code

First the header file. It defines the USB connection, the analog input pin used to read the pot's value, a few constants, and a function that will be used for converting the pot's value from [0 - 1] to the voltage [0 - 3.3] volts.


And the source file. It contains the body of the conversion function, two terminal control commands for clearing the screen and moving to the top, and the commands for reading the pot's value and printing it (together with its voltage) to the terminal through the USB port.



Demo

I'm on a GNU/Linux system, where the mbed is identified as /dev/ttyACM0. A simple tool to use for connecting with it is screen (eg. sudo screen /dev/ttyACM0). Pardon me for the bad quality of the video, it's the first time that I used YouTube capture with my phone and it didn't work as well as I expected :)


 

Acknowledgements

Special thanks to my good friend and Arduino hacker efxa, for without his parts it wouldn't be possible to play with mbed.

References

Saturday, June 15, 2013

Numerical string sorting in Ruby

The problem: You have an unsorted array of strings (product codes, area codes, etc.) containing both letters and numbers (for example 'A28', 'A3', 'A1', etc.) and you want to sort them numerically (that is, 'A1', 'A2', ..., 'An'). Ruby offers a sort method, so let's see what it does...

Nope, that's not what I want. But no worries, enumerable is here to help me. Enumerable is a great mixin inspired by functional programming. Functional programming relies heavily on data transformations: The input is a data structure, we transform the data of the structure by applying one or more functions, and the output is a new data structure. In this particular example the input is an unsorted array of strings, I want to apply a function that will change their order, and the output will be a new array with the strings sorted numerically.

What is the data transformation in this case? A string is a data structure that can be treated as an array in most programming languages, and ruby is not an exception. Therefore the problem can be solved by using as a sorting key the numeric part of a string and treating that key as a number.

And we are done :)