Black Bytes
Share this post!

Category Archives for Programming

Rubocop – Your personal code reviewer

Rubocop is a static analysis tool that will help you keep your code void of a number of style and potential error issues. It is very easy to get started, install rubocop using ‘gem install rubocop’.


Now move to the project folder you want to analyse and run rubocop -l. The -l option tells it to only use the linter module, this will skip a lot of stuff like “extra spaces between parenthesis” and keep the output a lot more manageable for now.

Now you can get started cleaning those up. Since rubocop provides file name and line number, finding that code shouldn’t be a problem. If you are feeling brave, rubocop includes an autocorrect feature (option -a).

You can also disable or set your own rules via configuration files (YAML format). For details on that, I recommend trying the –auto-gen-config option and reading the ‘Configuration’ section of the README.

Other output formats

You may find that the output is a bit verbose, in that case you will be happy to know there are multiple output formats available. One that I like is the ‘simple’ format. You can enable this format with rubocop -f simple.

Other useful formats are: json (-f json), HTML (-f html), and summary (-f offenses). For the html version you will need to redirect the output to a file. Like this: rubocop -l -f html > rubocop.html

Integrating rubocop into your workflow

Hopefully you found that rubocop is a great tool and you would like to integrate it into your workflow. You have a couple of options, you could try using something like guard or a git pre-commit hook. You could also use an editor plugin, if you are using atom you could try out the linter-rubocop package.

You might also like:
Ruby string formatting

MySQL – Slow query analysis

Ever wonder why your application is slow? Then there is a good chance you got slow queries! In this post, you will learn how to enable the slow query log and analyze it to find how what you need to optimize.

The mysql slow log

MySQL (and probably other databases as well) have this feature that allows you to log queries that take too long to execute. It’s disabled by default (in version 5.6) and to enable it we will need to edit the configuration file. Normally this file is located at /etc/mysql/my.cnf

We will add the following lines inside the [mysqld] block:

Make sure that /var/log/mysql exist, if not create it and change permissions so mysql can write to it. Next you will need to restart the server for the changes to take effect, if the server doesn’t come up you may have a syntax error in your config file.

If this is a dev environment you will need to stress you app a bit to generate some data and then you can jump to the next section.

Log analysis

You could just open the log directly, but it’s not the most pretty thing to look at, so we are going to use a tool from the Percona toolkit. You can either download the full toolkit or just the specific tool that you need.

In this case, the tool we need is pt-query-digest which will parse the query log and show us some statistics and detailed info about our queries.

mysql slow

The top queries in the list are probably a good target for optimization.

But what if you can’t change your MySQL server settings? Well this tool has you covered! It has 2 other ways it can read queries from: tcpdump output and the ‘show process’ command.

To use the tcpdump output option you need to do this:

And for the process option:

If you would like to learn more about MySQL administration and performance I would recommend checking out Percona’s site which has plenty of free resources to help you.

I hope you found that useful, please leave a comment if you have any questions or anything interesting to add.

PHP: Top command-line options

Here are some useful php command line options that you will love. Ever wanted to test a quick snippet of code without having to save it to a file and then run it? Well check this out! If you invoke php with the -a flag it will drop you into an interactive session. This is nothing more than a REPL (Read, Eval, Print, Loop) that let’s you type any php code and execute it immediately. It also helps you with tab-completion of variable names and functions.

php command line

You could even use include to load a php file and test functions in isolation, so this is useful in case you don’t have unit test and want to make sure that a function is doing what it’s supposed to or if this function would usually be triggered by something like an ajax call but you don’t have that available yet.

If you are using a text editor instead of a full blown php IDE you will like the next option: php -l will perfom a syntax check on your code without actually running it.

php command line

Another useful one is php –ini which will show you what configuration files are being loaded by php (for the CLI version), this information is also available via the phpinfo() function.


And finally another option you may find useful is: php -S localhost:80 which spins up a dev web server. This is similar to the python version python -m SimpleHTTPServer

Make sure you try those out and let me know in the comments if you know any other cool php tricks 🙂

Related posts:

Redis – the blazing fast datastore

In the sea of NoSQL databases Redis stands out thanks to its speed and features, it can work as a cache or as a persistent database. Redis stores data in key-value pairs completely in-memory, so if your data doesn’t fit in RAM then this might not be for you.


In contrast to memcache, Redis offers access to a few data-structures (strings, list, hashes, sets, and sorted sets) which makes it a lot more powerful than a “dumb cache”. Getting started is very easy because it’s available in most Linux repos.

Installing and using redis

Here is how you would install it in a Ubuntu machine:

After installing it you can run the client redis-cli and it will drop you into a prompt similar to the mysql one. Only that you will be typing Redis commands instead of SQL, we are going to start with some basics. For a full list of commands click here.

That’s how easy it is to work with simple keys, if you want to see all the keys in the database you can issue the command “keys *”. Note that this command isn’t super fast if you have lots of keys so you don’t want to use it often.

If for example you wanted to store user data you could use the Hash structure and namespaces, this how you could do it:

If you prefer a more visual way of working there are web interfaces available, the main ones being phpRedisAdmin and PHPRedMin.

Well, that was a small taste of Redis! To learn more make sure to check out the documentation.

Dynamic Arrays in C – part 2

In the first part we left off laying the foundations for our dynamic array. Now it’s time to test it, this is the code responsible for that:

Continue reading