Imperative vs. Declarative Languages

Fol­low­ing my pre­vi­ous post on mul­ti-lan­guage (poly­glot) and mul­ti-par­a­digm (poly-par­a­digm) devel­op­ment, I thought I’d con­tin­ue on the same thread for a lit­tle and do some com­par­isons on some of the pop­u­lar types of pro­gram­ming lan­guages.

Definition:

An imper­a­tive pro­gram­ming lan­guage such as C# or Java allows you to spec­i­fy step-by-step how a prob­lem should be solved using a series of state­ments which change a program’s state. It’s impor­tant to remem­ber that whilst Object Ori­ent­ed Pro­gram­ming is how we are all taught to do imper­a­tive pro­gram­ming these days, it’s not the only way – C and oth­er pro­ce­dur­al lan­guages are also imper­a­tive lan­guages.

A declar­a­tive pro­gram­ming lan­guage on the oth­er hand, is a high­er lev­el pro­gram­ming lan­guage which allows you to express what you want with­out spec­i­fy­ing how to get it. SQL is prob­a­bly the most wide­ly used declar­a­tive lan­guage today and in SQL you don’t tell the query ana­lyz­er how to go about fetch­ing the data, you just state what data you want to retrieve and it takes care of the rest. It’s also worth noth­ing that

How they compare:

The ben­e­fit of declar­a­tive lan­guage is that it sep­a­rates the process of stat­ing a prob­lem from the process of solv­ing it. It’s essen­tial­ly an exten­sion of design by con­tract where pro­duc­ers of the declar­a­tive input describe what they need and how they need it, allow­ing pro­duc­ers of the declar­a­tive pro­gram to deter­mine the best way to get it.

Approx­i­mat­ing this in an imper­a­tive lan­guage is pos­si­ble because at heart, every­thing is imper­a­tive. The chal­lenge in doing so is that an imper­a­tive pro­gram oper­at­ing on declar­a­tive input must be pre­pared to check pre and post con­di­tions and have a plan to deal with every even­tu­al­i­ty!

Declar­a­tive lan­guages allows for greater exten­si­bil­i­ty, agili­ty and pro­duc­tiv­i­ty. Think how quick­ly you can cre­ate a table, input some data and then get the data back in some form or anoth­er in SQL and then imag­ine the time and effort it’d require if you were to imple­ment these in C#.

How­ev­er, as a user of a declar­a­tive lan­guage, you have lim­it­ed or no con­trol over how your inputs are dealt with and there­fore have no option but to mit­i­gate the impact of any imperfections/bugs in the under­ly­ing lan­guage and rely on the providers of the declar­a­tive lan­guage to address the issues.