Fork me on GitHub

Syntax highlighting for kindergarteners

Syntax highlighting makes it easier for kindergarteners to program.
Douglas Crockford

And Crockford has pretty good arguments what he exactly means by that (of course a little joking):

https://plus.google.com/118095276221607585885/posts/XXkzgJEoE9v

The point that he makes is that the current way of syntax highlighting that all IDEs and editors provide don’t add any kind of additional value for the reader of the program except of looking childish and that is exactly right.

He resolves this issue by adding a feature to JSLINT which highlights the scope of the syntax.

Let’s take a look at regular syntax highlighting:

regular-colors

This is how jslint applies highlighting:

jslint-colors

I must say that I am pretty shocked that we haven’t been using this highlighting earlier because for experienced, professional programmers regular syntax highlighting is pretty pointless.

The way that Crockford recommends doing highlighting actually adds real value for the reader.

You can test some code live here.

Posted in Programming | Tagged | Leave a comment

Misleading Interview Question Guides

I saw something really random about programming interview question guides on the web which often completely misses the point of the whole interview question. The problem is that they distract from the essence of the question.

An example:

We have a list of n elements and we have x unknown duplicates within that list: find the duplicate elements.

If a question is so simple as this one then the answer to this is for sure not to come up with a random algorithm but to ask the right questions.

Instead you see average programmers brute forcing tens of different versions to solve this problem in the comments section, on the given website. That is just stupid because every programmer must be able to solve this anyways. That is not the point.

Only the boundaries for this algorithm are interesting which determines the algorithm.

So expect programmers to first ask for boundaries rather than trying to randomly solve this. If a programmer is not suspicious about this question then something is wrong.

Possible boundaries, as always:

  • We have enough CPU time but tiny memory (QuickSort + Binary-Search, or + lineare Search comparing neighbours)
  • We have very little CPU time but huge memory (Counting-Sort)
  • We have in both cases plenty (Counting-Sort)
  • We have both cases very tiny amount available (if n huge probably MergeSort for external devices + blockwise linear neighbour comparison, or b-tree index look up)

That is the actual question: What are the boundaries.

Posted in Thoughts | Tagged , | Leave a comment

Book Review: Maintainable JavaScript

Maintainable JavaScript

I typically read technical books only from people who I trust. And with trust I mean that I have the feeling that what this person says makes sense and is not expressed from a personal point view but the person tries to bring together all faces of the problem and tries to rationally make a decision based completely on arguments with consistent logical structure. In this case I saw a presentation of Nicholas Zakas on Youtube which made a good impression to me.

I think Nicholas Zakas made a pretty good job in this book by bringing together all relevant practices in the professional JavaScript development field. He’s tries to bring together all threads and options and let you decide what’s best for your project. It is focused to be really practical and he’s even going down to the level describing how to write ant build scripts for your JavaScript projects.

Topics are: programming styles, statical code analysis, organizing complete projects, testing, integration with continuous integration with scripts, minification and many more details.

As a more seasoned web programmer I already knew most of it but it was still a good read and gave me new ideas applying best practices in my projects. I wished he would have told more stories from his practical experience which have led to problematic results. Even I have in my relatively young age a lot of stories about stupid behaviours in projects which led to problems.

For web developers I can recommend reading this book especially if you need a compact view of all best practices.

Posted in Allgemein | Tagged , , | Leave a comment

JavaScript Framework Wars Collateral Damage

You can almost calibrate an atom clock hourly by the intervals which new JavaScript frameworks appear.

One main reasons is that JavaScript frameworks are easy to write.

The other reason is that web developers are all day long on the web and spamming the web with discussion about what “the” best framework is.

To a certain extend you cannot just close the door and escape from these sphere because you have to constantly educate yourself and keep track of trends.

The downside is that you get constantly hypes to no end by childish web developers which dancing around their Macs with their Starbucks coffees in their hands.

The web was never constantly hyped that much than in the last years. In the 60s to 70s the big topic was the software crisis. In the 80s object oriented programming was the next big topic which dominated the press. Today in my opinion it’s JavaScript frameworks. They dominate the complete web due to the overrepresentation of web developers on the web who generate this junk-heap of battles for “the” next thing.

The important thing is that you have to trust you own judgement to distinguish between short lived hypes and persistent trends.

But the pure amount of stuff that get’s into your view makes it more and more difficult to reliably make predictions about the durability of frameworks or any other software or technology.

I just recently experienced it by myself. I was writing a project where I programmed the client side code purely in plain JavaScript. After it was getting messy I created in some kind of way my own little framework. After that I wasn’t really happy with that although it was working pretty fine.

However I tried to rewrite it entirely in Ember.js which was a big mistake. Because the project it relatively new, the api wasn’t really stable and the documentation was for my standards way too shallow and didn’t go really in depth in terms of practicality showing reasonably complex real world examples. Additionally the framework was just too big and somehow intransparent. It pretty much dismissed itself naturally with these factors.

Due to this experience I jumped over to Backbone.js and rewrote the code again. At some point when I was almost done I got pretty angry while I was using the framework because the abstractions were too weak and shallow that it was pretty much pointless to use this framework. It neither had any elegance nor the elaborateness of any good framework. So I wasted another full two days to learn this framework.

At this point I was about to return to my original JavaScript code because it was the most straightforward. Anybody would be able to understand it because it was really straightforward without the superficial surface that Backbone.js provides distract you from your actual work.

I took these frameworks because my judgement let me alone. I took the one which were the most hyped frameworks without actually making a judgement. At the other side I could argue that I only lost probably a week before I dismissed these framework where others spend years before seeing their mistakes, if the see what they’ve done wrong at all.

After this sobering experience I took a look at a very less hyped – but at this point more mature – framework. I won’t tell you which one because it’s not important. The important thing is that this framework had either a very light abstraction but it had the right abstraction. You just need to hit the nail on the spot.

I don’t say that the time I spent on those frameworks was wasted. I think it only will be wasted in the future – if I’m going to make the same mistakes again.

Be always careful with hypes. Especially within the short lived web developer community. Things in this area are exciting because they are very dynamic but it’s requires a huge amount of experience to judge what is important and what’s not.

Posted in Experience, Programming, Thoughts | Leave a comment

Code Smell: Commenting to the Death

Comments are an art form. Sometimes too much.

// Specifiying length
var length = persons.length;
// Initilizing sum variable
var sum = 0;

Thanks for telling me.

function doIt() {
    // Comment Comment Comment Comment Comment Comment Comment Comment
    // Comment Comment Comment Comment Comment Comment Comment Comment
    // Comment Comment Comment Comment Comment Comment Comment Comment
    // Comment Comment Comment Comment Comment Comment Comment Comment
    // Comment Comment Comment Comment Comment Comment Comment Comment
    ...code

    // Comment Comment Comment Comment Comment Comment Comment Comment
    // Comment Comment Comment Comment Comment Comment Comment Comment
    // Comment Comment Comment Comment Comment Comment Comment Comment
    // Comment Comment Comment Comment Comment Comment Comment Comment
    // Comment Comment Comment Comment Comment Comment Comment Comment
    ...code

    // Comment Comment Comment Comment Comment Comment Comment Comment
    // Comment Comment Comment Comment Comment Comment Comment Comment
    // Comment Comment Comment Comment Comment Comment Comment Comment
    // Comment Comment Comment Comment Comment Comment Comment Comment
    // Comment Comment Comment Comment Comment Comment Comment Comment
    ...code
}

Methods are free. Some don’t know. Maybe.
Objects are free either. Some don’t know. Also maybe.

Posted in Programming | Tagged , , | Leave a comment

Pseudo Tail Recursion

There is something really awful in most imperative programming languages which is the absence of tail recursion optimization.

It means that if the last thing you do in a function is to call the function itself then the recursive call is compiled into a goto, just a jump.

Why the hell should anybody need that?

First of all anything you loop can often be expression more elegantly as a recursive call, it’s often easier to work with recursion. Let’s look at an example:

function fac(n) {
    if (n === 1) {
        return 1;
    }
    else {
        return n * fac(n - 1);
    }
}

This is a recursive function to compute the factorial of a number. Let’s look at the loop version:

function fac(n) {
    var fac = 1;
    for(var i = 2; i <= n; i +=1) {
        fac *= i;
    }
    return fac;
}

Let’s look at an example in a functional language like Erlang:

fac(0) -> 1;
fac(N) -> N * fac(N-1).

I chose Erlang because it really elegantly handles recursion because it provides a powerful polymorphic pattern matching for function signatures. From top to bottom the function signatures are pattern matched until it fits your parameters and then it’s called. Awesome.

As you immediately recognize the loop version is the most cryptic one and keep in mind that this is a really simple example. As it gets more complicated like traversing trees and doing more elaborate stuff then the elegance of recursion fully comes to light.

But what I don’t like in imperative programming languages is that I have to think about the stack. Let’s look again:

function fac(n) {
    if (n === 1) {
        return 1;
    }
    else {
        return n * fac(n - 1);
    }
}

is expanded to:

-> fac(3);
--> 3 * fac(2);
---> 2 * fac(1);
----> 1
---< 2 * 1
--< 3 * 2
-< 6

When you get to 1 to the deepest path then the stack is unwind and the sub tree returns its result to the callee function up to the root which return the final value.

I think this is nice in terms of the theoretical representation of the tree but it’s much more complicated than working with tail recursion optimization and keeping track of just one state without having to think about the stack. You have to keep this tree which consists of a function call chain in your head all the time, this is for my taste way too error prone.

Here JavaScript and Closures come in to save the day:

var factorial = 6;

function fac(n) {
    if (n > 1) {
        n -= 1;
        factorial *= n;
        fac(n)
    }
}
fac(factorial);

You think the syntax is the most ugly yet? You are absolutely right, that’s what you get when you have badly designed language.

But still, we used JavaScript’s closures doing recursion without having to keep track of the stack. We just have to focus on the current value of factorial in every recursion step.

As your recursion gets more complicated and you keep changing more stuff than just one variable then this is the more transparent form.

Posted in Programming | Tagged , | Leave a comment

SCHICK.js

I just started a new small library with handy JavaScript functions called SCHICK.js

The first function is to query arrays and objects:

var element = SCHICK.q(OBJECT | ARRAY, 'path.to.element');

Example:

var genius_object = {
    "0": {
        "name"   : "Alonzo Church",
        "address": {
            "location": "USA"
        }
    },
    "1": {
        "name"   : "Alan Turing",
        "address": {
            "location": "England"
        }
    }
};

SCHICK.q(genius_object, '0');
// { name: 'Alonzo Church', address: { location: 'USA' } }

SCHICK.q(genius_object, '1');
// { name: 'Alan Turing', address: { location: 'England' } }

SCHICK.q(genius_object, '0.name');
// Alonzo Church

SCHICK.q(genius_object, '1.name');
// Alan Turing

SCHICK.q(genius_object, '0.address.location');
// USA

SCHICK.q(genius_object, '1.address.location');
// England
Example to query an array:

var genius_array = [
    'Some string',
    ['first element', 'second element'],
    {
        "name"   : "Alonzo Church",
        "address": {
            "location": "USA"
        }
    },
    {
        "name"   : "Alan Turing",
        "address": {
            "location": "England"
        }
    }
];

SCHICK.q(genius_array, '0');
// Some string

SCHICK.q(genius_array, '1');
// [ 'first element', 'second element' ]

SCHICK.q(genius_array, '2');
// { name: 'Alonzo Church', address: { location: 'USA' } }

SCHICK.q(genius_array, '3');
// { name: 'Alan Turing', address: { location: 'England' } }

SCHICK.q(genius_array, '1.0');
// first element

SCHICK.q(genius_array, '1.1');
// second element

SCHICK.q(genius_array, '2.address.location');
// USA

The next interesting enhancement would be to allow wildcards:

SCHICK.q(genius_array, '*.address.location');
Posted in Programming | Tagged , , | Leave a comment

Interpreting The Mess

I just recently finished another project which made me think again.

So let’s describe a typical day and I’ll tell you where the problem is.

You come into the office and you still have 10 things to do from yesterday. You have received about 20 emails about many little things that need to be fixed and you have to write them back and everybody is responding something that you have to read again.

During the day also many people are calling which forces you to add more stuff to your to do list although you try to solve issues on the spot. But often you need to tell the people to write you an email so you can work on that issue later and write back. So another issue which you need to keep track of in your head.

Of course you also get support calls and mails during the whole day which you have to filter and decide their priority and also write again emails or call customers back.

Suddenly people come in or call you because they need some small registration until tomorrow for upcoming trainings. It only need to store the data inside the database and send an email confirmation back to the person who registered and the training managers.

Now your calendar is popping up because in 15 minutes you have a meeting with someone and the conference the room next door is already occupied. So you need to find another free room for the meeting because you cannot bother your two co-workers with an one hour meeting at your desk.

You go out for 5 minutes to check if a room is free and come back and you had also 2 missed calls.

However the project that you were working on has two days left but the registration page needs to be up until tomorrow morning. So you start working on that.

You just hack a script together because you need to go on with your to do list and you know that you will have via emails many back and forth discussions until the page will be ok to go live.

The next day they call you because they also need an email confirmation link and to generate an Excel export for all attendees.

Alright so you need and protected area and you need to find out how to generate an Excel file. So you add all that stuff and the page is getting hairy and ugly but it finally works.

A few months later you are already working on many other things but a new co-worker came in and needs to change something on that small registration app you wrote.

Now I come in and try to come to a conclusions why everything is so messed up.

Next time I will tell you my conclusions.

Posted in Thoughts | Tagged , , , , | Leave a comment

3-Dimensional Fun

As a nerd I really like the aesthetics of plots of three dimensional functions. I’ll sometimes just look for cool looking ones or just try to build one that looks neat.

Here I just add some cool looking ones and some which are well know on the web because they had hit also mainstream media.

  • The really chaotic looking function is the Rastrigin Function.
  • The one which looks like rockets flying through holes I just made up.
  • The one which looks like a black holes I just shifted +60 to move it above the grey zero surface which Google plots.

Posted in Allgemein | Tagged , | Leave a comment

A letter from an unethical computer scientist

Dear web user.

We know better who you are than yourself do.

We can detect over time who you actually are. And with “who you are” we don’t mean who you are trying to be in front of other people – no we can track your actual behavior.

What you buy, what you read, what you sexual preference is, to whom you talk to, what you are talking about, what you are searching on the web, which web-sites you visit and many more things.

Oh, so you might ask who is we and who we think we are? You might ask that we don’t have the right to do all that?

You are wrong. You just don’t know it yet because our statistics say you will still use our stuff.
Continue reading »

Posted in Thoughts | Tagged , , , | Leave a comment