C as a programming learning language

Recently we were talking with a colleague from my University about how programming is taught nowadays in college.

In my University, the programming language used for teaching how to program is Java. I was discussing with a colleague the disadvantages I saw in using it as a language for teaching programming basics and why, in my mind, it didn’t stack up against teaching a language such as plain C. The following story aims to reproduce the high points in the conversation.

Suppose you are a first year professor, teaching programming fundamentals, and I’m one of your students.

How would you start? If you’re anything like me, the first “hands-on” lab, you’ll probably write a classic “Hello World” program on the whiteboard and ask your students to copy that.

In order to do so, you instruct them to open up their “IDEs”, create a new class and copy that into a “method”…

Wait… what? As your student, chances are that by now, I’m probably wondering what an “IDE” is anyway.

As a teacher on a basic programming course, you’ll probably just tell me that IDEs are used for programming and that I’ll most likely have to use that every time I want to write a program (otherwise, what are we using it for?)

Okay, but “why don’t I have to use an IDE every time I want to use an application in my Windows”? Bam… now you must either explain the difference between code “in development” and code “in production” or ask me just to trust you and please focus on the example on the board for now.

Okay, let’s move on. You said I had to copy what you wrote into a “class’ main method”… What is a “class” and what is a “method”? Ahh, okay. Here, you are trapped. Clearly these are OOP concepts, and you certainly don’t want to start digging into that before your students even write their first “Hello World” application. All you can do here is to ask them, once again, to just trust you by saying that “that’s the way things work in Java”.

So, your students have finally created a project in an “IDE”, created a “class”, defined a “public static” “method” and wrote the snippet you typed at the whiteboard in their IDE’s text editor.

The final code looks something like this:

public class HelloWorld
{
    public static void main(String[] args)
    {
        System.out.println("Hello, world!");
    }
}

They press “Play” on their “IDEs” and it just works. Everything is good.

Quick question: how many things in this code do you think are completely strange to your students? Probably all of it, since it’s the first time they’ve seen a program being coded. That’s okay, but how many of these things do you think are feasible to be explained in a programming 101 course? just println, maybe?

Why? Because in order to explain what a “class” is, what a “method” is, what “public”, “static”, “String[]”, “System” and “out” mean, you have to start digging into OOP concepts from day one. And you don’t want to do that, because chances are 1) you’ll end up confusing everyone and 2) most of that stuff will not really be useful for the course at all!

So… if teaching this way seems to be so complicated, why are we using Java for teaching programming? Well, I think we shouldn’t. C would be a much better candidate.

Compare the previous Java program with the following C equivalent:

#include <stdio.h>
int main()
{
    printf("Hello, world!");
    return 0;
}

Which one do you think is easier to explain? At most, what your will probably end up asking is “what does #include mean?”. You can just answer that it’s a file where printf is defined. That’s it. No strings attached. No “public static void Main(String[] args)”. Just a plain and simple printf.

What about all that “IDE” mess? Well, it turns out you can just use a plain text editor with some syntax highlighting and a C compiler and you are good to go. Compiling C code is extremely simple, if the system is correctly configured. Just issuing a command like the following will be enough for producing an executable file:

gcc hello.c -o hello.exe

It’s that easy. And best of all is that you get an executable. A plain .exe file on Windows. That’s what your students are expecting. That’s what a program is to them. An .exe file! In Java, your compiler would’ve generated a “.class” file, and then you would have to teach your students how to invoke the Java Virtual Machine, explain what a Runtime is and why is it needed to have your code (which is already complied, isn’t it?) run.

Another final advantage? By asking them to compile their code by hand, you indirectly have them learn how to use the Terminal application (believe me, most students don’t know what that is or how it’s used nowadays) and you are laying the foundation for them to learn how to use a non-GUI operating system.

You can achieve all this (and more) without having to ask them to “trust you” or to hold their questions for one to two years, until they have their first OOP course.

You could argue that, even though Java might seem more complicated at first, it’s more popular and thus it could never be a mistake to teach it. I completely agree, but I think it should be taught as a second or third language, not as the first one. Not only does Java require the student to wait until he or she goes through 2 or 3 programming courses in order to be able to fully “get” the language, but also, there are some concepts that they will just never learn this way, such as memory management or pointers.

So, please, if you’re a programming 101 teacher who’s doing Java, I ask you to reconsider. C does not have to be hard, and your students will thank you for conducting a exhaustive, self-contained course.

How to Create Shared Objects under Mac OS X

Some time ago a friend from Colombia asked me how to create a Shared Object under Mac OS X in order to be able to use it from a Python application (via ctypes).

Although the compiler most commonly used on Mac OS X is the GCC compiler, the version supplied by Apple has a handful of modifications which are specific for OS X. In particular, the -shared compiler flag, used to create Shared Objects under GNU/Linux based systems, has no effect.

In spite of this, Apple did introduce its own mechanism for creating Shared Objects along its GCC extensions. Assuming we have a file called mylibrary.c, which contains the source code for our library, we should invoke GCC like so:

gcc -dynamiclib mylibrary.c -o mylibrary.dylib

If the compiling process succeeds, this command should create a file called mylibrary.dylib, which can now be dynamically linked from other applications.

Unlike Linux based Operating Systems, Mac OS X seems to automatically add the current working directory to the DYLD_LIBRARY_PATH environment variable (an equivalent to Linux’s own LD_LIBRARY_PATH), thus it should not be required to modify this variable in order to have our applications link against the newly created library at runtime, unless, of course, we wanted to move the Shared Object to a directory different from where our process will be executing.

Finally, it should be taken into account that this mechanism is just a simplified example. A correct way to manage the library creation process would be by means of make or equivalent software construction applications, which provide several advantages to software developers.

How to turn an Ubuntu desktop into a convenient Linux-based server

If you have a desktop computer and a laptop chances are that, in the long run, you end up spending more time working on your laptop than on your desktop. If repurposing your old desktop into a fully fledged server seems like too much of a commitment (either because you do not have the time to set it up or do not want to give up your desktop), you can still configure it as to offer services to other computers on your network without having to go for a server operating system.

Accessing a computer from your local network usually just means assigning an static IP address to your desktop and have it though that IP address every time. The peculiarity with desktop Linux distributions -such as Ubuntu or Fedora- is that they now ship with an utility called NetworkManager which tries to make it “simple” to manage networks. The problem with NetworkManager is that, in my opinion, it was designed mostly for a mobile scenario, where a laptop moves around and tries to join different networks along the way, but this is rarely the case for a desktop computer, which just connects to the same network every time. Furthermore, depending on your router configuration, it may prove difficult to impossible to have a NetworkManager-enabled desktop PC to have the same IP address assigned every time the computer joins the DHCP network.

In this post we will present a method for completely disabling NetworkManager from your Ubuntu desktop system and, using the old-school Linux networking facilities, have your desktop computer’s network connection set up your way, offering services to other computer without sacrificing Internet access.

Continue reading

A Sudden Change of Plans…

For a looong time I was wondering whether it would be best to start a blog in English or in Spanish. Being fluent in both languages provided me the freedom to pick one or the other.

About three years ago, I decided Spanish was the way to go (being my Mother Tongue after all), but at times, I couldn’t stop wondering if I was sort of crippling my blog by limiting it to the Hispanic society exclusively.

All this wondering was more than just occasional thoughts it seems, since as of today, I finally decided to launch an English blog.

I wanted to consolidate several websites and ideas under the same “roof “, and this is it. Here I plan to publish tutorials, personal info, and thoughts about technology (specifically Computer Graphics and Linux and Mac development) as well as about my company, Algorithmia.

Feel free to look around. I hope you find the site useful. Welcome.