Whilst fish­ing around Stack­Over­flow for inter­est­ing dis­cus­sions I came across this ques­tion on which pro­gram­ming par­a­digm is best for which job. There were some inter­est­ing argu­ments put for­ward, and after doing some more read­ing here are the two things I took away:

Lan­guage != Paradigm

What par­a­digm (pro­ce­dural, OO or func­tional, etc.) you choose to pro­gram with does not nec­es­sar­ily equate to your lan­guage of choice. For exam­ple, C# and Java are pre­dom­i­nately asso­ci­ated with OOP because OO is the par­a­digm both are designed to cater for. How­ever, just because you’re pro­gram­ming in C# doesn’t mean you’re doing OO pro­gram­ming – you can just as eas­ily be writ­ing your pro­gram in a pro­ce­dural or func­tional style. Indeed, exten­sions such as LINQ and Post­Sharp has intro­duced func­tional pro­gram­ming and AOP to C#.

(OO vs Func­tion) vs (Dynamic vs Static)

When talk­ing about lan­guages most peo­ple think about the dif­fer­ent par­a­digms they’re asso­ci­ated with, but there is a sep­a­rate dimen­sion to be con­sid­ered – Dynamic typ­ing vs Sta­tic typ­ing. The two dimen­sions over­lap, for exam­ple, C# and Java are both OO and sta­t­i­cally typed whilst Ruby and Python are both OO and dynam­i­cally typed.

So, what do sta­tic or dynamic typ­ing mean any­way? To answer that, let me go back to the basics and revisit some of the terms com­monly used when describ­ing ‘type’s in pro­gram­ming languages:

Type

A type is a meta­data which describes the kind of data stored in a chunk of mem­ory, a type is usu­ally asso­ci­ated with a list of oper­a­tions that can be per­formed on the data (meth­ods in OOP, or func­tions in procedural).

Strong Typ­ing

Though the def­i­n­i­tion of ‘strong typ­ing’ seems to be vague and can vary depends on who you ask, but there are some com­mon themes that are con­sis­tent through all the ref­er­ence mate­ri­als I have read:

  • A strongly typed sys­tem has type check­ing and will throw an error when it finds type mis­match. When the type check­ing hap­pens depends on whether the lan­guage is sta­tic or dynam­i­cally typed, see below.
  • Once assigned, a vari­able will always behave as a cer­tain type until it is reassigned.

Both C# and Java employ a strongly typed sys­tem where all vari­ables must have a defined type, and there are restric­tion on type con­ver­sion. But inter­est­ingly, Anders Hejls­berg (lead archi­tect of the C# lan­guage) in an inter­view described strong typ­ing as some­thing that can be toned up or down as opposed to an on or off fea­ture. Here is his orig­i­nal quote from the interview:

Anders Hejls­berg — The thing you real­ize about typ­ing is that it’s a dial. The higher you place the dial, the more painful the programmer’s life becomes, but the safer it becomes too. But you can turn that dial too far in either direction.

If all this seem a lit­tle too vague for you, here is a nice and sim­ple test you can use:

If you can con­cate­nate a string and an int with­out cast­ing, then it’s not strongly typed.

Sta­tic Typing

In a sta­t­i­cally typed pro­gram­ming lan­guage, the types of vari­ables must be known at com­pile time. Again, both C# and Java are sta­t­i­cally typed lan­guages, though C# 4.0 would soon intro­duce sup­port for dynamic typ­ing too.

As you’ve no doubt guessed already, a sta­t­i­cally typed sys­tem are more restric­tive and there are greater over­head and effort required on the devel­op­ers’ part to deal with type mis­matches at com­pile time, there are actu­ally many rea­sons why it’s ben­e­fi­cial to you:

  • Sta­bil­ity – type errors can be caught auto­mat­i­cally by the com­piler, reduc­ing the chance of a bug slip­ping into production.
  • Readability/Maintainability – more infor­ma­tion is pro­vided about how the code is sup­posed to work, which helps future devel­op­ers (or your­self!) to under­stand the pur­pose and intended usage of a variable.
  • Bet­ter devel­oper tools – the IDE (or tools like Resharper) will not be able to pro­vide a very use­ful intel­lisense sup­port if it doesn’t know what types to expect at com­pile time.

Dynamic Typ­ing:

A pro­gram­ming lan­guage is dynam­i­cally typed if its type check­ing is per­formed at run-time. In dynamic typ­ing, types are asso­ci­ated with val­ues not vari­ables, which means the set of meth­ods and prop­er­ties you can use on a vari­able is ver­i­fied at run­time against the type of the value the vari­able cur­rently holds (see duck typ­ing).

C# 4.0 is going to include a dynamic key­word to add sup­port for dynamic typ­ing to cover some of the niche areas such as interoperability.

Part­ing thoughts…

There are a lot of con­fu­sions around the dif­fer­ent ter­mi­nolo­gies on the topic of typ­ing, so much so BC Pierce, author of “Types and Pro­gram­ming Lan­guages” said:

I spent a few weeks try­ing to sort out the ter­mi­nol­ogy of “strongly typed,” “sta­t­i­cally typed,” “safe,” etc., and found it amaz­ingly dif­fi­cult… The usage of these terms is so var­i­ous as to ren­der them almost useless.

I hope this blog post will help clear some of the con­fu­sion you have :-) And to fin­ish what I started, here’s the accepted and my favourite answer from the ques­tion which started all this:

Your choice of Par­a­digm or Model or Approach or Style is based on the answer to the fol­low­ing question:

How Can I Best Rep­re­sent This Problem?”

If the prob­lem has objects and rela­tion­ships, OO. If the prob­lem has algo­rithms and trans­for­ma­tions, maps, fil­ters and reduces, Func­tional. If the prob­lem is dynamic, chang­ing and flex­i­ble, Dynamic. If the prob­lem is sta­tic and will scale up rapidly, Static.

Fur­ther reading:

Full 8-part inter­view tran­script with Anders Hejlsberg

Stack­Over­flow ques­tion – what are the key aspects of a strongly typed language?

Stack­Over­flow ques­tion – dynamic type lan­guages ver­sus sta­tic type languages

Stack­Over­flow ques­tion – why is C# sta­t­i­cally typed?

Share

One Response to “OO vs Functional vs Dynamic vs Static”

  1. […] jour­ney begins by look­ing at sta­tic typ­ing, dynamic typ­ing, OO and func­tional lan­guages.  Can expo­sure to a dynamic lan­guage such as Ruby improve the code I write in a sta­tic language […]

Leave a Reply