Programming Paradigms


Wikipedia has an article on programming paradigms. Wikipedia says: “Programming paradigms are a way to classify programming languages according to the style of computer programming”. A programming paradigm is a style of building the structure and elements of computer programs.

We have a lot of posts on C#, so let’s first look at what paradigm C# belongs to. C# is a multi-paradigm language that includes imperative and object-oriented paradigms, together with a certain level of support for functional programming with features like delegates (allowing functions to be treated as first-order objects), type inference, anonymous functions and Language Integrated Query (LINQ).

There are four main types of programming paradigms: imperative, functional, logic and object-oriented, according to Kurt Normark in an article referenced by Wikipedia.

However, “Uncle” Bob Martin says in his talk about the Future of Programming on YouTube at 55:40 that software hasn’t changed. At 57:11 he says the code is still comprised of assignment statements if statements and while loops. There have been no radical advances in software technology. He goes on to say at 59:15 that we programmers need to increase our level of professionalism. Increase our level of discipline.

Imperative

The ‘first do this, next do that, then that’ is a phrase which describes the imperative paradigm. It is similar to descriptions of everyday routines, such as food recipes and car repair. With imperative languages, it is “imperative” that you specify every detail of every step of the way, instead of “make a chicken dinner”.

Declarative

Declarative programming is a paradigm that expresses the logic of a computation without describing its control flow. You don’t provide the detailed steps. Ir is what to do, not how. Many languages that apply this style attempt to minimize or eliminate side effects by describing what the program must accomplish instead of the how-to accomplish it. Common declarative languages include those of database query languages (e.g., SQL, XQuery), regular expressions, logic programming and functional programming.

Functional

“Evaluate an expression and use the resulting value for something”. Functional programming is in many respects a simpler and cleaner programming paradigm than the imperative one. The reason is that the paradigm originates from a purely mathematical discipline: the theory of functions. For example, you could say that the weather is a function of temperature, air pressure, cloud formation, humidity, precipitation, sunshine and wind velocity. The weather depends on these inputs. Recall this syntax from math class: y = f(x) or f(x, y).

Logic

The logic paradigm is dramatically different from the other three main programming paradigms. The logic paradigm fits extremely well when applied in problem domains that deal with the extraction of knowledge from basic facts and relations. The logical paradigm seems less natural in the more general areas of computation.

Object Oriented Programming (OOP)

OOP is a paradigm that sends messages between objects to simulate the temporal evolution of a set of real-world phenomena. Data, as well as operations, are encapsulated in objects. In other words, an object is a way of encapsulating state and behavior. State are things like fields, attributes or instance variables. Behavior is what you do with that state and includes things like methods. Information-hiding is used to protect the internal properties of an object. Classes represent concepts whereas objects represent the actual phenomena.

As Shivprasad Koirala expresses it at Code Project: “as functional programmers where we used to remember methods and function names like “Add”, “Subtract”, “Multiply” and “Divide”, in OOP we just need to remember class “Maths” and all functions just pop out when we create the object of the class.”

Static vs Dynamic

There are two types of programming languages: statically-typed or dynamically-typed. A language is statically typed if the type of a variable is known at compile time. For some languages this means that you as the programmer must specify what type each variable is (e.g.: Java, C, C++ C#); other languages offer some form of type inference, the capability of the type system to deduce the type of a variable. Resolution of types, members, properties and methods is done at compile time. If you try to access a method or property that cannot be found we get immediate feedback from the compiler. C# is a statically-typed language, but it does offer type inference (var) and dynamics. Another topic worth studying (when you get a chance) is reflection.

A language is dynamically typed if the type is associated with run-time values, and not named variables/fields/etc. This means that you as a programmer can write a little quicker because you do not have to specify types every time (unless using a statically-typed language with type inference). Examples of dynamic languages are Ruby, JavaScript, and Python. These languages can be a little bit easier to write code with, but on the other hand, because we lose compile-time checking, we have to write more unit tests

The Future A Video

The word on the street is that functional programming is the way of the future in programming paradigms. There is a video on YouTube that discusses The Future of Programming by “Uncle” Bob Martin.

4 Paradigms – A Video

Here is a video called 4 Programming Paradigms In 40 Minutes by Aja Hammerly at RubyConf 2017. She illustrates a program that makes money change with a drawer full of coins and bills as if you were working at a retail store. She goes through some code. She talks about OOP, functional programming, Ruby, Prolog, and Assembly, as you can see in the list below.

  • 2:49 Objected-oriented programming w/Ruby
  • 10:48 Functional programing w/Racket
  • 19:46 Logic programming w/Prolog
  • 32:08 Procedural programming w/Assembly

A University Course

If you want to deep dive into programming paradigms in video format you could atart with the university course lecture in video format called Introduction to Programming Paradigms course by Jerry Cain over at YouTube. The website Academic Earth has a bunch of sourses in video format. Click the View Course button beside the course Computer Science III: Programming Paradigms to watch the same video.