Archive for the ·

Programming

· Category...

Programming, disconnected

no comments

I’ve spend this weekend on the beach, and between runs on the beach roadway and barbecue with family and friends, I did some work on a prototype, basically because TV sucks, sleeping in the afternoon is not my thing and I can’t stand the sun between 11 A.M. and 4 P.M.. My laptop, for some reason that I didn’t even bothered to research, was unable to connect to the 3G network, so I was left disconnected for the time I was there.

Not being able to google information required to complete the job was a little frustrating (not that all the information I needed was readily available on the internet, but at least one time it would be useful). The task at hand involved programming some JNIs for android and making them render some content on a bitmap to be presented on screen. Though task.

The whole situation was looking badly, but I decided to go ahead and do it and I was able to successfully get it done. The whole experience was one of learning and confirming some ideas on my head for some time:

  • Being disconnected while programming requires you have to figure out a lot of stuff on your own, with sharp focus. I only had a paper copy of the JNI book on my backpack to do research, and that helped me up to some point. Afterwards, I was on my own.
  • Not being able to communicate with your peers via instant messaging or email means that you have to think alone. At the office, it’s easy to go to a more experienced peer and ask him to help figure out what’s going on. While disconnected, I needed to pay more attention to what was happening to solve my own issues.
  • At the very end I was trying to figure out why the screen rendering was not happening. Everything was OK in the backend, but the screen was pitch black. I spend two hours trying to solve this issue with different approaches. So, I decided to enjoy my time a little. At one moment, some idea popped on my mind that helped me figure out the problem, in one of that famous “shower moments”. If I had spent all the time in between in the pursuit of an answer, I would have not found it, because I was looking at the wrong place. Letting the answer come to me was a better solution to my problem.
At the end of the weekend, I’ve had not only stretched my confidence in my capabilities, But I also confirmed the learning that, for some tasks, it’s better to stop, go for a walk (or to the gym, or the swimming pool) and get back on the problem with some fresh ideas. Invaluable lessons.

berp is awesome

no comments

berp is awesome. Run the plain boring implementation of factorial below with it to see how amazingly fast berp is (compare it with CPython.)

def fact(n):
    if n <= 1: return 1
    return n * fact(n - 1)
fact(90000)

Compilation finished!

no comments

Update: I made a script for that. It’s available on github.

I work with come software that takes about 10 minutes to compile, and what I do when I have nothing else to do during the compilation is to read Hacker News, where I usually waste more time than it’s allowed for good productivity. NOT ANYMORE!

Now my compilation line has and extra command that pops-up a windows saying that the compilation has come to an end. Pop-ups are provided by zenity on Ubuntu. You can look for similar solutions on your platform. I’m sure they’re available.

My compilation command looks like this:

make && zenity --info --text "Compilation Finished" || zenity --error --text "Compilation Failed"

Shit my professor says: green software

no comments

I’ve listen enough about green software – or any kind of low-power-consumption software for instance. One says programmers should

carefully look at the object code to minimize transistor state transition – where all the power consumption occurs.

I think this is a bunch of bullshit for two reasons. But first, allow to retort a bit. Low-power-consumption software is applicable in any device that is running on a battery (e.g. your phone), but the concept that’s being taught is completely overstated.

My two reasons:

  • processors like the ARM Cortex A8 on your iPhone 4 are superscalar. Which mean that they reorder the instructions in execution time to accelerate the overall program execution. Which makes the argument invalid. Looking at your object code will only be a waste of time.
  • there are better ways to reduce power consumption on devices. One of them is reducing the power dissipation in the transistor state transition. (Or maybe using a black-hole as a cooling device :) .)

You can argue that it may apply to smaller devices with simpler architectures. Surely I agree, but still who looks at object code these days? Let the compiler do hard work. And how many of the students will really work on these kind of computers? Not many.

Also, in phones the major power consumption doesn’t happen in the processor. The radios (GSM, 3G, GPS, wifi and Bluetooth) and the screen are the greatest wasters. Why focus on bits, when the phone screen alone wastes more than 90% of the phone’s battery? Minimize the time that screen and radios are on, and you will be saving many more watts (and time) than the power saved by perfectly aligned bits.

Another professor says that we should focus in reducing disc spins. There are no discs spins since the invention of flash drives. QED.

The same professor says that reducing the algorithmic complexity of the program will reduce the power consumption. With this I agree. It seems the single improvement in software engineering that will have the largest impact in devices’ power consumption. Reducing an O(n) algorithm to O(log(n)) will reduce the power consumption by one order of magnitude. That’s way more saving than the OCD behavior of aligning bits.

TLDR: carefully looking at bits to reduce power consumption will only work if the processor is not superscalar. Reducing the power consumption on transistors, screens and radios, and reducing the algorithmic complexity of your software are a better way of controlling power consumption, but don’t OCD  yourself over this.

If you have arguments against my case, the comment box is at your service :)

Solved in 6 minutes (well, not really).

1 comment

Reading this post at RethinkDB I decided to take my chances at solving the problem they present to the candidates to work with them, which is:

Write a C function that reverses a singly-linked list.

Took me six minutes to solve it, plus circa 10 minutes to write the test, for a total of 16 minutes. It was not the best in class.

So I took more time to come up with another solution. It took me 30 minutes to solve it the best fashion I could.

Both solutions are shown below.

Solution #1

The first solution is based on recursion. Let’s see:

struct node *reverse_list(struct node *current, struct node *last) {
    struct node *next = current-&gt;next;
    current-&gt;next = last;
    if (next != NULL) {
        return reverse_list(next, current);
    } else {
        return current;
    }
}

Recursion is a beautiful thing. It lets you solve some tricky problems with a very simple code, no visible clutter in the code. Most of us would find it an elegant solution. And this is, for a duct tape solution, for a prototype.

Although elegant, recursion can easily blow your stack away. That’s because recursion uses the process stack a lot, to allocate stack frames. So, for some serious business this code is suboptimal. A few dozens of entries in this list, and the program would crash and burn.

What separates good programmers from the ones that excel at the craft (so I’ve been said) is the itch to fix their own code. And man, that was itching. So I spent some more time thinking and came up with a better idea, one that doesn’t use recursion.

Solution #2

The second solution is as follows.

struct node *reverse_list2(struct node *top) {
    struct node *next, *last = top, *first = top;
    while (top-&gt;next != NULL) {
        next = top-&gt;next;
        top-&gt;next = last;
        last = top;
        top = next;
    }
    top-&gt;next = last;
    first-&gt;next = NULL;
    return top;
}

So, 19 out of 20 miss this solution. I’ve actually have seen some pretty bad candidates in the industry, that make their resumes to appeal to the managers. “XML, CSS, Javascript, Java, C#” and, well, no real knowledge of the basics.

And this seems to be even worse. Some guys get really close and miss at the last point. Don’t get it? The guy attributed the right answer in a variable that will be destroyed at the block’s end. Which is the next line. Failed.

My take on this: being a master of the craft of programming pays off. Hard work pays off. Those who are reading should at least spend more time learning to do the real work instead of trying to fill their resumes with buzzwords.

My full answer is here. Thank you for you time and patience.

Extra bonus: I you came here looking for an answer to the riddle (which you shouldn’t), you can take the answer to the FizzBuzz for free.