Backticks are evil

I was just starting my first job, and had to implement a RADIUS authentification mechanism for a dial-up system. I didn’t have the discipline and knowlege that gathered trough the years, so I was doing a lot of programming by coincidence.

So, I found a example of a perl script that was called from xt-radius to authenticate users. Didn’t copy it with copy-paste, I’ve tried to write it by hand, and missed the difference between a backtick and a apostrophe.
I ended up banging my head for a few hours until I spoted the difference.

For perl, qx() construct and $() for bash are much more obvious, than `. For a beginner it’s going to make a big difference, for a senior, it’s going to be much more visible and easy to spot.

read more

MySQL and C

read more

Go for quality, not quantity, it will pay in the long run

“How to write 60 tests in 60 minutes” – belive it, or not, this is the title of a document I had to “read”. It’s about using Test::More in development of automated test.
Someone really belives that it’s possible to write 1 automated test per minute. Attention: write, not implement. This means: planing, desinging and implementing. This means: spending time to read the specifications, designing the testcases, thinking on how to best to vrerify each of them and implement them.

Writting a suite of test cases that is really usefull requires a lot of thinking. Yea you can “write” 60 test in 60 seconds

 1 ok(1+1 = 2, "adding 1 to 1 works");
 2 ok(1+2 = 3, "adding 2 to 1 works");
 3 ok(1+3 = 4, "adding 3 to 1 works");
 4 ok(1+4 = 5, "adding 4 to 1 works");
 5 ok(1+5 = 6, "adding 5 to 1 works");
 6 ok(1+6 = 7, "adding 6 to 1 works");
 7 ok(1+7 = 8, "adding 7 to 1 works");
 8 ......
 9 ok(1+58 = 59, "adding 58 to 1 works");
10 ok(1+59 = 60, "adding 59 to 1 works");
11 ok(1+60 = 61, "adding 60 to 1 works");

Hint: vim’s g command helps a lot.

However, the above suite is useless, it tests some random test cases, hoever it doens’t test any corner cases, nor is it complete. It’s just a lot of busy work to justify spending time at work.

“let’s gather some code metrics: number of lines of code, files, etc.” – Why ? Number of lines of code is practicaly irelevant, the files may give a sense of modularity of the code, but still mostly useless.

 1 my $chk_agent        = 'rpm -qa |grep -i agent';
 3 sub check_agent {
 4 	my $agent_1   = qx ($chk_agent);
 5 	my $value_1 = $? >> 8;
 6 	print "$agent_1 \n";
 7 	return $value_1;
 8 }
10 my $agent              =  check_agent();
12 ok( $agent == 0,          "Check agent  installation" );

The above code works, but is mostly useless junk, to fill up 12 lines of code.

This can be written in one line, and it’s more readable and more eficient.

1 ok( qx(rpm -q agent) >> 8 == 0, "Check agent instalation")

True, it’s not going to print the result of the command, but for the for the purpose of this, the result of that command is irelevant. Changing the first variant into the second part results in a productivity of ~ -10 lines of code per (let’s say) hour. Fix 20 such junk pieces of code, and you got -2000 .

Not to say that SLOC is a completely useles metric. It’s a usefull metric when the code quality is good and everybody writes the same type of code, in the same language (or a language with the same power – comparing lisp SLOCs with Java SLOCs would be hilarious).

“The true metric of code is the number of WTF per second heard from the code review room” – it’s another gem, from the same source. True, this has some merit, hoever, it shows the maturiy of the author. If I hear a “WTF” from my team, I imediatly assume that the level of maturity of that person is not high a enough to be part of my team. My favorite definition of maturity is “the ability of expresing one’s feelings while respecting the interlocutor’s feelings”.

read more

Connect to a server behind a proxy

I’ve searched the web for examples of creating a SSH connection via a proxy, and found only a few examples.

From the manual:

ProxyCommand Specifies the command to use to connect to the server. The com‐ mand string extends to the end of the line, and is executed with the user's shell. In the command string, any occurrence of ‘%h’ will be substituted by the host name to connect, ‘%p’ by the port, and ‘%r’ by the remote user name. The command can be basically anything, and should read from its standard input and write to its standard output. It should eventually connect an sshd(8) server running on some machine, or execute sshd -i some‐ where. Host key management will be done using the HostName of the host being connected (defaulting to the name typed by the user). Setting the command to “none” disables this option entirely. Note that CheckHostIP is not available for connects with a proxy command.

This directive is useful in conjunction with nc(1) and its proxy support. For example, the following directive would connect via an HTTP proxy at ProxyCommand /usr/bin/nc -X connect -x %h %p

read more

Computing average values and standard deviation while running a continuous load test

A while a go I had to run some load tests for a few days in a row. I had to run billions of transactions against a system, and keeping all the transaction parameters stored somewhere wasn’t really a option. Plus, I wanted to be able to see what the current average was while the test was running.

read more

Symmetric binary root

I’m looking for a new job, and one of the companies that interviewed with asked me to do a test on a certification site. I took their certification test, and it took me a bit more than required, but I nailed that. However, I was in my comfort zone, using Perl.

I took the test for the second problem I kind of failed. The problem was to write a function that will calculate the smallest symmetric binary root of a integer within O(sqrt(N)) complexity and O(1) space complexity.

The symmetric binary root of a of a given positive integer N is a positive integer A such that N = A * bit-rev(A). bit-rev(A) is an integer obtained by reversing the bits of A.

My initial thinking was that I iterate from 1 to the sqrt(N) and try each solution. Seeing that it fails for some examples, I just iterated to N, to cover all cases, so at least the solution would be complete, never mind the complexity.

Here is what I summited:

1 def bit_rev(N):
2     return int('0b' + bin(N)[:1:-1],2)
3 def symmetric_binary_root_count ( N ):
4     for i in range(N):
5         if (i * bit_rev(i) == N):
6             return i
7     return -1

I did make the most common mistake in a programmers life : offset error. In python range(2) returns [0,1]. But the symmetric binary root of 2 is 2.
The fact that I had to look up how to reverse the bits of a integer in python and the solution involves a string is equally #!#

read more

Data::Dumper, pp, pprint

What’s fun about modern interpreted languages is that you can experiment very easy. Just fire up a console and trow in a few lines and voila you did it ;)

read more

Relevant related posts

I’ve been planing to add a relevant posts list in my sidebar for a while now. But while walking the web, I found up that related links on various sites do not seem to be really relevant.

At first, I was thinking of using the links in the post to extract some of the posts that are relevant, but that would mean that:

  1. older post need to be edited to contain links to the posts that I needed to link
  2. the list would be redundant, there is no need for it if there is already a link in the post.
  3. it would be a manual process

So, I decided to go with the most common path that seems to be used, to use tags. However that would present a problem: I have many posts that are tagged with a specific language, and really common tags like programming, basics, etc. so the links would not be really relevant

I decided to posts that are share tags in the order of the least used tag. We will see how it will work ;)

read more

And then there was ADA

Who would want to use it, I don’t know. Especially with the cryptic messages that the compiler gives.
According to Wikipedia ADA was designed by the DOD to replace a large number of languages that were used across many projects. ADA has strong built-in support for concurrent programming.

read more

In the words of Barney Stinson: “Challenge accepted”

Have you seen the Carlos Mencia mocking a Asian that says I looove tests. That’s me when I think about programming and programming languages in general. I’ve stumbled upon a site named CodeChef , who posts programming problems and accepts answers in may languages. I decided to solve a few of the questions in all possible ways, maybe I will learn something new.Challenge accepted!

I’m going to start with assembler, I’ve written a series of mini programs for dos, a lot of code for 8051 and 8080/Z80, but I didn’t had a chance to do in Linux. I’ve chosen the most popular problem on CodeChef, Life, the Universe, and Everything .

read more

Logarithmic scale tag clouds

I plan on having tags that are common for most of my posts, and a few tags that will keep togeder a few related articles. Also, I hope I will have a lot to share, so a lot of posts, and a lot of tags that have huge count.

This basicaly means that, common tags will have a huge advantage over uncomon tags.

It ocured to me that a logarithmic scale will solve that issue, rare tags will be bigger, common tags will still be bigger than the small ones, but not by much.
It seems that I wasn’t the only one with that idea, I found this article on Wikipedia while researching for this one.

I decided on a arbitray number of 5 tag sizes. All I needed was a function that will map the tag count to one of the sizes.

I ended up with the following function:

note that this will fail if the max tag count is 1, so the actual formula I used is:

read more

BASH named parameter functions

I strongly believe in code readability. I would go so far to recommend that code should not be commented. But that’s another story. I think that code should be in such a way written that even a profane person should be able to understand what is going on.

A while back I had to write a myriad of test cases to test a telecommunication product. Most of the test cases had the same structure with minor differences in the setup.
These differences are hard to spot if you look at a classical bash function that has a few parameters (most of them were creating database entries). On top of that, most of the values were irrelevant for test case, and most of the default values would have been safe.

read more

A programmer’s resume

I had my resume online for quite a while, but never have I spent more than a few minutes to think about how it was formated, just used some “simple” html tables in a static page.
The trouble with tables is that the “simple” stuff is actually sufficiently complicated to write or update. As a result, my resume was most of the time outdated. I decided to solve this, so instead of having a static table to edit, I decided to separate the HTML from the content, and store the content in a YAML file, and use ruby and ERB to generate the updated version of the resume, each time something changed.

Why YAML ? Because it’s pretty easy to maintain and understand, it doesn’t have much overhead so you can easily spell check it (you don’t need to skip over lot’s of keywords).

read more

Real programmers use copy CON (or how a programmer should write his blog)

There is a old joke about programmers, real programmers use “copy con” to write programs. Yes, it’s possible see this video.

I don’t really think of it as a joke, for me that’s a reminder that a programmer should always respect the resources he has at his disposal, and implement things with minimum cost.

That’s why this blog is a collection of static files instead of dynamically generated pages by PHP/Python/Perl/Ruby. If you think about it, it makes sense, a blog is a collection of articles. The only truly dynamic part is the comments system, the rest can be easily implemented as static pages. The editing posts/pages is not really necessary for a IT specialist, there is no web application that will mach a good text editor.

read more