/Crafty Programming

Crafty Programming

When I gave the title a thought I thought to google it and I was hit by a web game development framework. I just wanted to say that this post is totally not about it.
Since I first started with CAT as a team that serves those who have potential to learn and dig into computer science and IT. I’ve always encountered the same question from rookies which is “What programming language should I start with?
I just had to say this:
“Never start with python or ruby or any similar language, those who learn python as their first language would find it difficult to learn more complex programming languages like C++ or Java. Maybe Java is a good start”. Despite that it’s a true statement it’s also shallow. I’ve had the intention to write such post on CATazine before to illustrate and explain the distinctive nature of programming languages. and discuss different programming paradigms, computational models, concepts and often used techniques. But do not worry I shall not pull you down to the dungeons of programming.
1. Introduction:
Programming is usually viewed as a craft, for example there’s those who are known to be “python” programmers or “Java” programmers..etc. This is a huge mis-conception and restrictive approach, this approach categorize programming by the language and in order to compare by language as a basic category of programming it might take you forever. Moreover, the knowledge of only one language limits the mindset and mentality of a programmer since each computational model or paradigm tend to utilize different computation and logical approaches. possessing the knowledge of such different approaches will serve your programming logic. without such knowledge It is like being taught to build a house out of blocks and take another course to learn how to build a house out of wood. Thus, you won’t be even able to think how to build a house in the first place and would not even to build a house out of both blocks and wood. Eventually, I am confident when I say that by reading this post you will reconsider the way you code.
I’ve been once told this “Programming is the first set in computer science. programming is just a tool which you should be good with”. This means that by being a programmer you are not necessarily an engineer or scientist -with respect to difference- you are just taking your first steps. I thought of this, If those who are good programmers are just starting what about those who only know one language?
2. Computational Models:
First, I should point out that a computational model is different from a paradigm and they’re often confused. a computation model describes the execution progress of a program. On the contrary, a paradigm describes how a program can be structured.
a good start would be declarative (stateless) model which opposes imperative (stateful) model.
2.1 Declarative Programming
Declarative programming enfolds and includes different paradigms like functional or logic paradigms as in Haskell and Prolog. Furthermore, declarative programming can be defined as programming that tends to describe the problem and solution is automated by computer. two different approaches to declarative model exist. First, In command driven programming languages like SQL the solution is described and the methodology used to retrieve data required is insignificant. Another approach is functional programming like Haskell. Functional programs tend to use functions as the basic building block for the language. Thus, a dataset or variable is passed to multiple function by required permutation. The nature of high abstraction under functional code in such a way that a compiler can re-implement a function on the way it see fits as long as it gives same result every time..makes functional paradigm more declarative. Also with side events being handled like garbage collection the abstract automaton of problem solving is abstracted and decoupled from machine nature. A pure functional language is said to return the same values when same argument is passed to same function and this is exactly the definition of a declarative operation.
To this point I suspect that the definition and “howto” of a declarative program is still vague. As I might have pointed out earlier, a declarative model describes the problem “the what” which is what we want to end up solving without solving the algorithm “the how”. Also the reason why declarative programming was called stateless programming is that the result of same function and same argument do not change on any program state.. in fact there’s no visible program state. unlike Imperative model where a result of a function given same argument could differ.
It might be difficult for you to grasp what is special about declarative model from first time. However, declarative model tends to decouple a programming language from platform by abstracting the imperative nature of the machine and applying declarative specification language. As mentioned machines language is imperative. for those who know assembly, you’ll find that all operations are imperative operations and program state could change per each instruction and that’s exactly what declarative model is about.
Making a complete specification declarative language without using some imperative semantics or get by now is considered impossible. That is because a program must deal with the machine nature at some point, a very obvious example to this is Monads in pure functional languages. Monads are used to abstract the imperative nature of I/O. Since I/O is all about request state or operation state it doesn’t meet the basic definition of declarativeness. Eventually, a declarative language is based upon recursion to simplify program structure. There’s no for/while loops or mutable variables.
Lately, declarative programming languages have been heavily used to tackle new models, paradigms and concepts like concurrent programming or scalable programs or AI. The idea that declarative model has high abstraction that you only have to specify the problem and several steps to solve it without worrying about machine state and mutability or race conditions is very programmer friendly. A very sound example is Haskell parallel programming model which is quite similar in its notion to the OpenMP or CUDA. it keeps the code nature as declarative and hide all imperative and states behind it. I can dive into declarative concurrency but for the sake of ease of understanding I shall not.
2.2 Imperative Programming
Unlike declarative programming, imperative programming describes the problem and the algorithm needed to solve it. It has a notion of program or function state and it an abstracted language whose program has the same structure as native machine procedure. In fact, Imperative languages are inspired and were inevitable due to the nature of machines. As I said before that on  lower level Imperative model is oftenly used.
So, In a way most imperative programming languages are abstractions of assembly languages. Example to this is “C – Fortran”. In fact Object oriented model is mostly stateful as well and is based on imperative model such as “Java – C++”.
3. Paradigms
Paradigms are used to distinct program structures used in a language. Most popular paradigms are Functional, Imperative, Object Oriented and Procedural.
Functional paradigm we’ve explained extensively above. There’s various languages which fall under this paradigm which are: Haskell, Standard ML and Scheme.
Example:
elemDrop n xs = if n <= 0 || null xs
                then xs
                else elemDrop (n-1) (tail xs)
an Imperative paradigm is what programmers are often familiar with. we’ve also explained this enough above. Example to this paradigm is C – Fortran.
Example:
int
maxDegree(graph *g)
{
        int i, max;
        if( g == NULL)
                return;
        max = 0;
        for(i = 0; i < g->vertCount; i++)
                if( g->adjSize[i] != 0 && g->adjSize[i] > max)
                        max = g->adjSize[i];

        return max;
}
Object Oriented paradigm tends to make the code more modular. It provides some features that other paradigms don’t provide such as encapsulation, abstraction, inheritance and other features which can make code parts loosely coupled yet highly functional when put together. This is how Object oriented paradigm gets its power. Languages that use this paradigm are “Java – C++ – C#”
Example:
class newClass{
public:

void sort(int arr[],int size){
        for (int i=1;i<size;i++){
                int key;
                arr[i]=key;
                while (arr[i-1] > arr[i]){
                arr[i] =arr[i-1];
                arr[i-1] = key;
                i--;
                }
        }
}

};


More modern language tend to be Multi-Paradigm languages which makes use of different paradigms yet sustain one paradigm as primary paradigm more often Such as:-
Python: object oriented – imperative – functional.
Scala: functional – object oriented.
Ocaml: functional – imperative.
4. Conclusion
Familiarity with a single language or even a single paradigm doesn’t establish a well basis for a  programming skills. programming -as said- is not a craft and thus you’ll need to learn the science behind programming and why programming came in this notion. Actually, learning about different computational models and paradigms will make your code more brief and direct to goal. Object oriented programming will teach you to always keep your code modular and perform abstraction layers. Imperative programming will take you to the dungeons of machine and make you care for the details and therefore learn about them. Functional programming will teach you to be brief and direct and focused on the solution rather than the problem itself. And there’s many many more things you’ll learn by extending you programming background.
This was a brief post about programming computational models and paradigms. I hope you enjoyed it.
further information:
(Keywords)
Turing Machine – Lambda calculus – Pi calculus – explicit and implicit state – garbage collection – evaluation [eager – lazy] – concurrent computational model – coroutines – atomicity – scalability – Relational computational model.