Skip to main content

How Functional Programming Can Help You Organize Your Code Effectively

I
have seen it time and time and again. A novice programmer sits down at the computer with the goal of writing the next big computer application. However, they do not have much experience putting code to screen and end up writing an ever-expanding network of while loops and if statements akin to a maze of paths through an unkempt garden. If one wanted to implement a gazebo in such a garden, they would surely have to rewrite much of the logic. If you have ever found yourself writing code like this, fear not. In this post, I will explain how you can use functional programming concepts to more effectively organize your code.

Functional programming? What’s that?


When you wrote that maze of loops and if statements, you wrote in a style known as imperative programming. In imperative programming, operations are executed line by line, and each operation modifies the state of your program (your variables) in some way. To contrast this, functional programming revolves around the concept of functions, which are discrete chunks of instructions that can be reused throughout a program. You might have heard of these before but never bothered to learn how they work. However, you should consider them to be an indispensable tool in your programming arsenal. Functions mitigate a number of problems, such as the existence of duplicate code and the inability to test particular features of the code. Most importantly, functions in functional programming do not modify global state—they merely run a sequence of instructions on an input and produce an output. Even when interspersed with imperative code, functions present a powerful utility for the betterment of your code.

What’s wrong with duplicate code?


You might be perfectly comfortable with writing the same code in multiple places if it gets the job done. However, this is not a good way to program, and there’s a very simple argument why. Say you’re writing a program that appends the same string to an input in multiple places. For example, your program could be keeping an inventory of the various things you have stored away. The user inputs something such as “apples”, and the program will print “You have 2 apples.” A naive way to implement this would be the following:

string haveText = “You have”
  int apples = 3
  int pears = 2
  int bananas = 10
  while true do
    string input = getInput()
    if input == “apple” do
      string plural = (apples != 1 ? “s” : “”)
      print(haveString + “ “ + apples + “ “ + input + plural)
    else if input == “pear” do
      string plural = (pears != 1 ? “s” : “”)
      print(haveString + “ “ + pears + “ “ + input + plural)
    else if input == “banana” do
      string plural = (bananas != 1 ? “s” : “”)
      print(haveString + “ “ + bananas + “ “ + input + plural)
    else
      print(“You have no “ + input + “s.”)
  end
end

The problem with this code is that we perform the same operation (haveString + “ “ + something + “ “ + input) three times. This becomes a lot to manage. For instance, say you wanted to change the output to something of the form “There are 3 apples in your inventory”. You would have to update the code in three locations in order to implement this change.

How does not having functions affect testing?


Testing one’s code is a great way to make sure it does what it is expected to do and does not contain any bugs. In a purely imperative programming approach, you would have to start your application and test it manually in order to make sure your code works. While this might work fine for a small program, in larger programs, it is often preferable to automatically test parts of the code rigorously in order to make sure those individual pieces work. If the individual pieces of a program work, it is less prone to errors due to programmer mistake. In order to implement this form of testing known as “unit testing”, it is necessary to use functions. Let’s look at how to fix up the above code with a function.

Using functions


To implement a function, we need to extract the code we would like to put into the function from what we have. A good option for the above code would be the following:

function makeOutputString(itemName, number) do
  string plural = (number != 1 ? “s” : “”)
  return “You have ” + number + “ ” + itemName + plural + “ in your inventory.”
end

The variables “itemName” and “number” are the arguments, or inputs, to the function. The keyword “return” tells us what the function outputs. Hence, this function takes the name of our item and the count of our item and gives us our output string. How convenient!

With that function we defined, we could simplify our code to something like the following:

fruitCounts = {
  apple: 3,
  pear: 2,
  banana: 10
}

while true do
  string input = getInput()
  if input is in fruitCounts do
    print(makeOutputString(input, fruitCounts[input]))
  else
    print(“You have no “ + input + “s.”)
  end
end

Not only does this simplify the code, it makes editing and testing the code very simple as well.

Wrapping up


I know this has been a short post, but I hope you’ve learned a thing or two about functions today. Try using them the next time you program and notice how much more readable and organized your code becomes!

Comments

Popular posts from this blog

[English Translation] ハレハレヤ - 羽生まゐご ft. flower

訳 ハレハレヤ 羽生まゐご ft. flower view on atwiki Lyrics & Translation: 夜の街迷いし穢れの乱歩 何処から来たのよ見窄らしいね ねぇうちにおいで温めてあげるよ Lost in the town at night walking aimlessly in depravity Where did you come from? You don’t look all too great Hey why don’t you come to my place, I’ll warm you up 今までよく頑張ったよね ここらで休んでみませんか ゆっくり話をしませんか It must have been hard for you getting this far Why don’t you rest here for a while? We can have a nice conversation to pass the time とりあえず今夜は安心さ 足跡は雪が消していた 声はひどく痛んだ 乾いた乾いた For the time being you can feel at ease The snow has covered the tracks we left Your voice sounds like it hurts you to speak Your throat is parched, your throat is parched 遠くの狐がこんこんと 僕たちを探しているようだ そっと息を潜めた このままこのまま行こう A fox far away is barking in the night He appears to be searching for us We reduce our voices to a whisper Let’s keep on like this, keep on like this 凍てつく雪の中で 確かな熱を帯びた 呼吸をして声を焼いて 燃えた燃えた禊の火 In the midst of the freezing snow You had within you ...

[English Translation] メルティランドナイトメア - はるまきごはん ft. 初音ミク

訳 メルティランドナイトメア はるまきごはん ft. 初音ミク view on atwiki Lyrics & Translation: 案外そんなフューチャー 先天的なフューチャー 案外そんなフューチャーだよ Unforeseen is such a future A connate, innate future Unforeseen is such a future, you know わんつーさんはい、いちにさん そろそろ起きたらどう 驚く顔なら知ってるよ いっつもそういう顔をする One two san hai, ichi ni san How about you get up sometime soon I know you’re surprised, from your face I can tell That’s the kind of face you make every time Welcome to the メルティランド ここはひとりもふたりも無い場所 Welcome to the メルティランド 美しい夢だけが遺る場所 貴方はドアを開けたの 僕の世界のドアを選んだの 十年前から待っていたわ Welcome to the メルティランド Welcome to the Melty Land There isn’t one nor are there two people in this place Welcome to the Melty Land A place where only beautiful dreams are left behind You were the one who opened the door You chose the door that leads into my world I’ve been waiting for ten years Welcome to the Melty Land 疑ってしまうような 不可侵のスターリーナイト ずっと前の空想が 今日の君の白昼夢 時間すら 止まったら 見え始める君のナイトメア 正常なグッドモーニング 人生のハッピーエンディング 僕達は何ひとつ叶わないのな...

A New Paradigm for Programming Languages

Programming these days has become a matter of learning a spectrum of different languages that all run on different runtime systems and all do their jobs slightly differently. Some programming languages, like C++ and Rust, run directly on "the metal", some, like Java and Elixir, run on a virtual machine, and others, such as Python and JavaScript, are run by an interpreter. Oftentimes the runtime environments of these languages greatly influence their design philosophies, and hence the way code is written for them, yet they often need to accomplish the same sorts of tasks. For example, to create a web server, each language has its own host of web sever frameworks to offer, each with its own quirks. It is rare for the same framework to have been targeted for multiple languages and function exactly the same in all of them. This generally isn't a problem when one chooses to develop in a single language, however many projects in the professional world tend to span multiple lang...