Monday, November 09, 2009

Programming Paradigms


This post is part of my ongoing effort to familiarize the readers with the commonalities & variabilities of C++/Java/C# programming languages. A tentative outline of this series can be found at Contents.

The first recognized programming language arrived in 1950s. O'Reily has a nice poster on the history of programming languages. At the beginning programs were very monolithic. The data was global and available to the whole program. Modification to a data item affected the entire program. It was extremely hard to maintain a large piece of code. Various paradigms have been introduced over the years to ease the development of complex programs with higher maintainability and reusability of code. There are other paradigms which represent different concepts for the programmatic elements. In this series of post, we will very frequently state procedural and object oriented programming paradigms. So they demand a bit more explanation.

  1. Procedural
  2. Programs are developed in a collection of procedures or sub-routines. There is an entry procedure (e.g. main in C) that calls other procedures to perform the tasks. The procedures form a chain and together accomplish the intended operations.
    Procedural programming is a type of Structured programming where each procedure has local data and modification of the local data does not affect outside of the procedure. But procedure has access to global data and can modify them which in turn may change the state of the entire program.

  3. Object Oriented
  4. Not surprisingly objects are the most fundamental components for OO programming. An object is a data structure consisting of data and a set of functions to manipulate that data. OO defines a set of objects and interaction among these objects. In the implementation the objects are defined in classes. An object is an temporal instance of a class and each object is unique in its running environment. I have drawn a simple class diagram (Fig. 1) and an example object diagram (Fig. 2) for students and teachers who supervise these students in a school.




 
    Principles of OO
    Abstraction: An skeleton view of the concrete object. Properties important for the external users are separated from the unimportant properties.
    Encapsulation/Information hiding: Implementation details are kept hidden from the external users such that changes should not effect the users.
    Modularity: System partitioned into highly cohesive and loosely coupled components.
    Hierarchy: Ranking or ordering of abstractions. There are a lot of sub-principles how to achieve these principle which is out of the context of this blog.

I will explain the implementation buildup of object orientation in our referenced languages when we will write our first program. The seminal book1 written by Grady Booch and co. is highly recommended for in-depth knowledge of OO concepts.

References

  1. Grady Booch, Robert Maksimchuk, Michael Engle, Bobbi Young, Jim Conallen, Kelli Houston. Object-oriented analysis and design with applications, third edition, Third edition. Addison-Wesley.

Sunday, November 08, 2009

Why to Learn Programming Languages


This post is part of my ongoing effort to familiarize the readers with the commonalities & variabilities of C++/Java/C# programming languages. A tentative outline of this series can be found at Contents.

Why Programming Language:
My first programming experience was a shock for me. On the same day when our first programming lecture in Pascal took place, we had to write a pascal program in the lab. I had no idea why I was doing what I was doing. My first tutorial was more shocking. Let's skip this now :)

We use natural language to communicate with each other. A letter is a written form to carry information to others which, if asked properly, may bring some responses back from the receiver. The language has some defined syntax (Grammar) which must be interpreted to some Semantics (meaning) by the receiver. So a common syntax and semantic pair is necessary to successfully establish the communication. The principle also holds for programming languages.

Machines are piece of hardware which understand only 0 and 1, called bits1. Sequences of bit combination form the byte code instruction and data which are encoded and decoded to perform the intended information. Now how can a human communicate with a machine? Programming language comes in action. Like natural language, a programming language defines some syntax with some intended semantics which is converted to machine readable format by the compiler.
A programming language defines syntax and semantics to instruct the computer to do some defined tasks.
Why C++:
C and C++ are definitely the most widely used programming languages on the Earth. If you are a computer science student whether you want or not, you will come across one or both of them in your career. So a fair knowledge of C/C++ is a prerequisite for a good software engineer. Since C++ is an extension to C, they are very often refer as a single language and called C/C++ (see-see-plus-plus)

With C/C++ you can control the system thanks to its support for low level capabilities even with inline assembly language 2 compiler. So for system level programming C/C++ is important. For driver and kernel development C/C++ are ubiquitous. Both Java and C# need a lot of resources to run the virtual machine on top of which your program will run. It is very frequent in the embedded system domain that you have insufficient resources to load Java or C# VM and you will prefer C/C++ for those systems. There are even special C/C++ compilers for those resource critical systems.

You need cross OS development in the Windows and *nix worlds. In addition, you want to perform system level operations. C/C++ can support you the best. There are various libraries available which let you build your program on multiple OS platforms. For Graphical User Interface (GUI), you can use QT (http://qt.nokia.com/) or GTK+ (http://www.gtk.org/) that enable you a consistent view of the user interfaces on many systems.

If you want your application to run super fast, you need C/C++. They are still the dominating languages in the game programming and 3D-Visualization domains. Though now-a-days, many popular games are coming out in other languages. I myself is currently working with 3D visualization in .Net platform using DirectX technology.

C++ is very frequently called a federation of languages rather than a single language. It supports various programming paradigms. It is a natural extension to C and Assembly. You can code procedural, functional, object oriented programs. Template Meta Programming (TMP) is a new programming paradigm that is made popular by its support for templates. C++ is undoubtedly harder than Java and C#. It has a high learning curve. You need many years of die-hard efforts to become expert in this language. Therefore, think first whether you really need C++ before you start learning it. But if you can reach at that level, you are a master of your systems.
For system programming you can hardly ignore C/C++.
Why Java:
Java is the most widely used language for cross platform development. "Write Once, Run Everywhere" is the motto of Java from the beginning. Though there are controversies how far it achieved that, nobody can ignore that Java's stronghold for the multi-platform development.

Java is a pure object oriented language. Every data entity and function belong to a specific class. There is no direct notion for scattered global data which has reduced complexity of procedural programming a lot. Garbage collector reduces the overhead of maintaining the program resources. The language defines a lot of standards and reference implementation which cover almost all aspects of programming concerns from simple desktop applications to large enterprise development. Since Java programs run on top of Java Virtual Machine (JVM), programmer has less control to manipulate the hardware directly which, in turn, provides a much higher security model for a system. Sun has, in addition, released the source code of complete Java stack as open source software under GNU public license reducing fear for vendor lock-in thus increasing its acceptability to the enterprises and open source enthusiasts.
Java is very strong in cross platform middleware development.
Why C#:
You are a Microsoft enthusiastic. You have almost no chance to program on other platforms. You are not forced to program in C++ by your employer. You don't need to control your system. You like pure OO programming language. Speed in development is important for you. Collaborative development of expertise on multiple languages is necessary for your team.You should use C#.

Microsoft has invested its large workforce for .Net development. C# is the main language for this framework. It is a very neat language. MS has tried heart and soul to reduce programmatic complexity in C#. Those who are disappointed with Java's lack of high performance visualization support, can effectively program in C# while equally enjoying advantages of managed environment. There is an easy migration path to transfer existing code base to the .Net platform. You can even run your code in dual (parts in managed and parts in unmanaged) environment facilitating gradual migration to the .Net platform.

Microsoft developed a version of C++ for .Net platform known as C++/CLI. It has simplified the execution of existing C++ components on .Net runtime. It is a combination of C++ and new constructs for .Net which has again steep learning curve. So for new .Net development, I discourage use of C++/CLI and rather recommend C#. Microsoft Visual J# is a Java compiler which compiles Java code for .Net runtime. But MS has suspended support of J# language in the future release of Microsoft Visual Studio. So it is not safe right now to start a new project in J#.

Though theoretically .Net is platform independent, MS released version 1.0 of the Shared Source Common Language Infrastructure, also called RotorVM which can be built only on FreeBSD (version 4.7 or newer), and Mac OS X 10.2. Commercial use of it is prohibited. Mono (http://www.mono-project.com/) is an open source implementation of .Net framework on Linux, Windows and Mac OX platforms. It currently supports C# 3.0, Visual Basic 8, and several other programming languages. Similar to J#, IKVM is a Java implementation built around Mono which compiles Java code to .Net platform.
C# is the premium language for the future application development on Windows
If you are a new leaner, you may start with a managed platform like Java or .Net. In this way, you can restrict yourself from learning unnecessary staff. Management of resources is a big headache for programmers. In Java and C#, this responsibility is left to the garbage collector. With standardized attribute (annotation in Java) support, the program can be made highly configurable. Both platforms has brought a large set of standardized classes to ease the development, deployment, and maintenance of software systems.
C++ is more widespread. But C# and Java are easier than C++. Java is open sourced and has implementation for virtually all platforms. C# has addressed GUI programming in managed environment better than Java but full implementation only on MS platforms.

For the hobby programmer, I recommend the following-
C# programming with Visual C# Expression Edition on Windows is fun.


References

  1. John B. Anderson, Rolf Johnnesson (2006): Understanding Information Transmission
  2. The Art of Assembly Language