OO vs Functional vs Dynamic vs Static

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:

Language != Paradigm

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

(OO vs Function) 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­at­ed with, but there is a sep­a­rate dimen­sion to be con­sid­ered – Dynam­ic typ­ing vs Sta­t­ic typ­ing. The two dimen­sions over­lap, for exam­ple, C# and Java are both OO and sta­t­i­cal­ly typed whilst Ruby and Python are both OO and dynam­i­cal­ly typed.

So, what do sta­t­ic or dynam­ic typ­ing mean any­way? To answer that, let me go back to the basics and revis­it some of the terms com­mon­ly used when describ­ing ‘type’s in pro­gram­ming lan­guages:

Type

A type is a meta­da­ta which describes the kind of data stored in a chunk of mem­o­ry, a type is usu­al­ly asso­ci­at­ed with a list of oper­a­tions that can be per­formed on the data (meth­ods in OOP, or func­tions in pro­ce­dur­al).

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 strong­ly 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­t­ic or dynam­i­cal­ly typed, see below.
  • Once assigned, a vari­able will always behave as a cer­tain type until it is reas­signed.

Both C# and Java employ a strong­ly 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­ing­ly, 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 inter­view:

Anders Hejls­berg — The thing you real­ize about typ­ing is that it’s a dial. The high­er 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 direc­tion.

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 strong­ly typed.

Sta­t­ic Typ­ing

In a sta­t­i­cal­ly 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­cal­ly typed lan­guages, though C# 4.0 would soon intro­duce sup­port for dynam­ic typ­ing too.

As you’ve no doubt guessed already, a sta­t­i­cal­ly 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­match­es at com­pile time, there are actu­al­ly many rea­sons why it’s ben­e­fi­cial to you:

  • Sta­bil­i­ty – type errors can be caught auto­mat­i­cal­ly by the com­pil­er, reduc­ing the chance of a bug slip­ping into pro­duc­tion.
  • Readability/Maintainability – more infor­ma­tion is pro­vid­ed 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 intend­ed usage of a vari­able.
  • Bet­ter devel­op­er tools – the IDE (or tools like Resharp­er) 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.

Dynam­ic Typ­ing:

A pro­gram­ming lan­guage is dynam­i­cal­ly typed if its type check­ing is per­formed at run-time. In dynam­ic typ­ing, types are asso­ci­at­ed 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 val­ue the vari­able cur­rent­ly holds (see duck typ­ing).

C# 4.0 is going to include a dynam­ic key­word to add sup­port for dynam­ic typ­ing to cov­er some of the niche areas such as inter­op­er­abil­i­ty.

Parting thoughts…

There are a lot of con­fu­sions around the dif­fer­ent ter­mi­nolo­gies on the top­ic 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­o­gy of “strong­ly typed,” “sta­t­i­cal­ly typed,” “safe,” etc., and found it amaz­ing­ly dif­fi­cult… The usage of these terms is so var­i­ous as to ren­der them almost use­less.

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

Your choice of Par­a­digm or Mod­el or Approach or Style is based on the answer to the fol­low­ing ques­tion:

How Can I Best Rep­re­sent This Prob­lem?”

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­tion­al. If the prob­lem is dynam­ic, chang­ing and flex­i­ble, Dynam­ic. If the prob­lem is sta­t­ic and will scale up rapid­ly, Sta­t­ic.

Further reading:

Full 8-part inter­view tran­script with Anders Hejls­berg

Stack­Over­flow ques­tion – what are the key aspects of a strong­ly typed lan­guage?

Stack­Over­flow ques­tion – dynam­ic type lan­guages ver­sus sta­t­ic type lan­guages

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