Declarative vs Imperative Programming in Kotlin

Modern programming paradigms

Declarative vs Imperative Programming in Kotlin

Hello Engineers...

Programming is ever-evolving with new concepts introduced every day to help programmers in different fields be able to improve and write optimized code for better and more efficient performance.

Whether you're an experienced or a beginner programmer, I bet you've written your code imperatively or declaratively at one point in your career and so today am going to try to differentiate between the two using relatable analogies.

Imperative Programming

Imperative programming is a programming paradigm that uses statements that change a program’s state.

Imperative Programming is like waking up in the morning to head to work then when walking towards your car you notice you have a flat tire so you call your mechanic to explain to you step by step how to swap the flat tire with the spare one and so imperative programming is that step by step process of changing a programs state using a set of statements.

To write code imperatively in Kotlin we use control flow statements like if,When or for in loops using mutable variables.

Show me in code...

val numbers = listOf(1, 2, 3, 4, 5) //Declare a list of integers
val evenNumbers = mutableListOf<Int>() //Declare a mutable list of integers

for (number in numbers) { //Loop through the list of integers
    if (number % 2 == 0) { //Check if each of the integers if 
                           //its divisible by 2 if it return 0 the its an even
                           // and its added to the mutable list of even numbers defined above

        evenNumbers.add(number)
    }
}

println(evenNumbers) //The list of even numbers is printed.

From the code above you can see everything has been specified in a sequence of steps using if a statement and for to loop through the list to get the desired outcome.

Now let's write the same code declaratively to understand the difference.

One of the advantages of writing your code imperatively is, it provides a procedural logic which can be more intuitive for certain tasks that may require sequential steps.

Declarative Programming

Declarative programming is a programming paradigm … that expresses the logic of a computation without describing its control flow.

Using the same analogy we used to describe imperative programming but in declarative programming, we call the mechanic to come fix our car but since he/she is accustomed to fixing cars and so he/she will use tools and expertise that are timely and shorter in sequence.

In declarative programming, especially in a Kotlin program, we use lambda expressions to simplify our code by simply describing what to be done as opposed to imperative programming which focuses on providing a sequence of steps to get the required outcome which can also be associated with functional programming. To achieve this we use collection operations like map, filter,reduce,flatmap etc to easily transform our data without the use of loops or mutable variables that we used in the imperative code above.

Show me in code...

val numbers = listOf(1, 2, 3, 4, 5)
val evenNumbers = numbers.filter { it % 2 == 0 }

println(evenNumbers)

The function takes lambda values and filters them by specifying the condition inside curly braces and the filtered numbers will be stored inside the even numbers list and this is basically what we call declarative programming.

Declarative programming provides conciseness as it requires fewer lines of code compared to imperative code by providing abstractions which allows developers to express complex operations easily.

I hope you have learnt something new from this article and feel free to go ahead and research more on the topic and practice as much as you can.

Keep coding.

Happy coding guys.