A collection of thoughts related to the challenges of software engineering

stay connected

Posts Tagged with ‘python’

When you will start your career you will often describe yourself as a "X programmer". For example, I once thought I was a "C++ programmer" (or developer if you think calling yourself a programmer isn't glorious enough).

How wrong was I. We all have our favorite language, generally for irrational reasons, but I think it's paramount to know what's behind the fence of your pré carré. Not only might you discover something you like more, but you will probably become better at what you use every day.

Ladies and gentlemen, allow me to present to you a list of languages I think every person serious about programming and computer science should know.

Each language will be illustrated with an implementation of the f:x->x! function.

C

Not knowing C is like not speaking English in the business world. You're just cutting yourself out of many opportunities.

Many languages are written in C. Linux is written in C. Windows kernel is written in C. The C compiler itself is written in C. Bah, yourself, you're probably just a C program (you just don't know it yet).

C is universal. All modern languages support C interfaces. If you write code examples in C you'll reach a very wide audience.

Knowing C will make you better at pointers arithmetic, memory management and improve your rigor. C does not abide approximation or sloppiness. C is going to be the rough teacher you hate during all your training but love ten years later when you will realize how helpful that experience was.

1
2
3
4
5
6
7
int f(int x)
{
    int a = 1;
    for(int i = 1; i <= x; ++i)
        a *= i;
    return a;
}

Scheme

Haskell or ML would be fine, but I think Scheme's purity will help you worship the gods of map and reduce. If you really want to be radical, you can go with Lisp. There is a jungle of various Lisp and Scheme environments. I would recommend trying PLT Scheme and its rich library.

Programming in Lisp and Scheme will open to you a whole new world where you can approach problems differently. You will become a master at recursion and meta-programming, which has got the side effect of building a stronger brain to help you understand large programs.

You will probably never use the language in a commercial project, but you will find functional programming facilities in C++, C#, Ruby, Python... Really, mastering map and reduce will make you write correct and concise code. Yes, that's a good thing!

1
2
3
4
(define (factorial n)
    (if (= n 0)
        1
        (* n (factorial (- n 1)))))

Python or Ruby

There is, to me, no difference between Python in Ruby. It's basically the same language. Oh sure, you don't define things the same way, but you're thinking similarly.

I personally prefer Ruby a little more (mainly because I'm annoyed by Python strict code indentation rules), but Python is really a fine language (and it's got map and reduce!).

Both languages come with a rich library and powerful web development tools (Django for Python, Rails for Ruby).

They will save your life when you have to write some obscure little script to process a huge amount of text. A task that can take quite a time with a more heavyweight language such as Java or C#.

There is an extensive amount of documentation for both languages and the communities are extremely active. In other words, they are very hot.

Don't let that fool you, they are not just a fad. These tools will make you productive and efficient. You will learn to play with regular expressions and might even lose some bad over-engineering habits you may have acquired with Java or C#.

1
2
3
def f(n):
    return reduce(lambda x, i: x = x * i,
                  range(1, n + 1), 1)
1
2
3
def f(n)
    (1..n).inject{|x, i| x * i}
end

C#

Where is Java? Not in this list obviously. In 2009, I don't think learning Java is a clever move. It's widely used and it's here to stay, but I have absolutely no idea where the language is going.

At Bureau 14, our local Java referent will speak highly of the JVM and its all mighty power, often talking about the cases where self-optimization outperforms C++. When the topic moves on to the core language itself, what it is becoming and why every line is three hundred characters large, his face becomes grim, the tone grave and the conversation ends up in "well, you can run Clojure and Ruby on the JVM you know".

From my point of view, C# and Java are identical. What happens is that C# is moving forward with better features - see, I haven't said more features, I've said better ones - and better support. The only real problem with C# is that it's extremely Microsoft centric. Don't get your hopes with Mono too high, with C# you're riding the Redmond boat.

However, playing with the .NET framework and C# is a good experience. I really like what they have done with XAML and Visual Studio 2008. It's really a powerful way to design a GUI. I also like generics, even if they don't match the power of C++ templates (for a very understandable reason).

In C# you will learn strict typing, large application design and various programming paradigms. You will know how to wield a major industry tool and that's probably going to help you find a job!

1
2
3
4
5
6
7
private static int f(int x)
{
    int a = 1;
    for (int i = 1; i <= x; ++i)
        a *= i;
    return a;
}

C++ isn't in the list

What an outrageous statement. C++ is probably the most powerful language available today and it's not in the list?

It's not that I deem you unworthy of the language or because I think it's not a good.

The problem is that it's going to take you several years to learn it and while understanding the STL is really something great, it's not imperative for all programmers.

In the 90s not knowing C++ resulted in being jobless. Fortunately this has changed as the language is not to be put in everybody's hand. I'm happy I don't have memory leaks and access violations in every program because programmers that don't understand memory management and pointers are now using C# or Java.

However, if you've learnt all the languages above and really want to achieve something impressive and unique, C++ is the language to do it.

It will enable you to write a program that downloads the whole Internet, defragments it, and uploads it back while using only 200 bytes of memory and running on a 4,77 Mhz processor.
Well the binary is probably going to be 100 MiB large and may take two hours to compile, but what's two hours of compilation compared to eternal glory?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
template <int X>
struct factorial
{
    int operator()(void) const
    {
        return factorial<x - 1>()() * X;
    }
};

template <>
struct factorial<0>
{
    int operator()(void) const
    {
        return 1;
    }
};

template <int X>
int f()
{
    return factorial<x>()();
}