Wednesday 26 November 2014

A Very Quick Comparison of Popular Languages


The languages chosen for comparison are BASIC, C and Python. Every language has some common features and some differences, so here comparison done by an example of reading two numbers from the user, adding them together and printing out the result.

BASIC

The program is trivial in good 'ol BASIC: 

10 INPUT A
20 INPUT B
30 C=A+B
40 PRINT C

RUN
 
Time to write: 15 seconds.

Things to explain: 

  • Line numbers
  • Variables
  • INPUT
  • PRINT
  • RUN 
Pros and Cons: 

BASIC is very easy for beginners to get started with, but it is an old, poorly designed language, lacking in almost every modern feature. Visual BASIC adds a lot to "good 'ol BASIC", but it is not appropriate (I believe) to teach a single-platform proprietary language. And it's still not really a good language.... 
 ________________________________________________________________________________

C

#include <stdio.h>

int main(int argc, char*argv[]) 
{
    int a,b,c;

    scanf("%d",&a);
    scanf("%d",&b);

    c = a+b;
    printf("%d\n",c);
}

%> gcc -o add add.c
%> ./add
 

Time to write:   about three minutes, including debugging.

Things to explain:

  • #include, functions (main), return types, argc, argv
  • variables, types (int)
  • scanf (and pretty soon it's limitations and how to work around them)
  • printf, format strings
  • pointers (already!!)
  • compiling, braces and semicolons

Pros and Cons: 

C was designed by top hackers for their own use. It was designed for writing operating systems, compilers and other system tools, and in this role it has become almost totally dominant.

It can provide excellent performance (assuming good choice of algorithm and good C skills) and allows low level hardware access, but these are not normally things required by the beginner. C's use of pointers are a source of frustration and confusion for beginners, but they are essential in even fairly trivial programs (like the one above, albeit in a trivial way).

Further, C's string handling is weak compared to many other modern languages (the scanf function used above is notoriously problematic).

C is a major and very important language, and all programmers should have significant exposure to it. It is however a terrible language to teach beginners. There is too much C that has to be explained, leaving less time for explaining programming.

____________________________________________________________________________________________

Python

import sys

a = sys.stdin.readline()
b = sys.stdin.readline()
c = int(a) + int(b)
print c

%> python add.py

 

Time to write:  about one minute, including testing and debugging.

Things to explain

  • import
  • variables 
  • sys.stdin 
  • readline (reads a string) 
  • int (converts a string to an integer) 
  • print
Pros and Cons

Python has an awful lot of good points:
  • enforces good programming style (indentation is meaningful) 
  • OO available but not enforced 
  • Exceptions used but not enforced 
  • is not a toy or academic language - much real world work is done in Python 
  • allows concentration on algorithms and problem, not on language features and shortcoming. 
  • is cross platform and has a powerful set of libraries
    is safe - it has dynamic run time type checking and bounds checking on arrays 
  • has powerful built-in data types - dictionaries, lists, sequences, functions, sets (in 2.4) 
  • has powerful built-in control structures - simple looping over sequences, map, generators, list comprehensions, regular expressions... 
  • requires less lines of code for any given problem, and is more readable - thus greater productivity.
- But Python is Just a Scripting Language
- But Python is Slooooooow