General Discussions

Literate Programming

If you really want to get the story of the code you write, follow literate programming for the initial few days. There is a high probability that you might continue once you are used to it which is obviously a very good programming practice.  Literate programming was introduced by Donald Knuth. His idea was to write a program as the thoughts flow.  However the syntax of the program hinders that thought process.

Knuth suggested writing the story of the code as the thoughts flow and embedding suitable code snippets in it. You get the whole program if you pull out only the source code from the story.  Let us see a simple example of how to do literate programming for adding two numbers in C programming language.

#+Title: Program to Add Two Integer Numbers
#+Author: Prakash B H
#+Email: prakash.hegade@gmail.com
#+Date: 31 May 2016

* Introduction
This document explains how to write a program to add two numbers using C programming language. The program is written using the literate programming technique. The documentation is carried out using the =emacs= editor in =org= mode. The code can be extracted from the document using the =tangle= commands.

* Program to Add Two Numbers
We begin the program by including the necessary headers which will bring together all the required libraries.

#+BEGIN_SRC C
#include <stdio.h>
#include <stdlib.h>
#+END_SRC

We next write a module to add two numbers. We pass the two numbers as parameters to the method. We perform the addition operation on the supplied two numbers and return the result.

#+BEGIN_SRC C
int add_numbers(int num1, int num2)
{
int result = 0;
result  = num1 + num2;
return result;
}
#+END_SRC

The next task is to write the main function which initializes the required variables and calls the function to add the two numbers. Let us put down the initialization part.

#+BEGIN_SRC C
int main()
{
int num1 = 10;
int num2 = 10;
int result = 0;
#+END_SRC

Next we call the function where the variable =result= captures the result.
#+BEGIN_SRC C
   result = add_numbers(num1, num2);
#+END_SRC

We then finally print the =result= to check if the operation was performed correct.
#+BEGIN_SRC C
   printf(“The result of addition is %d\n” result);
#+END_SRC

Closing of the main function
#+BEGIN_SRC C
   return 0;
}
#+END_SRC

* Conclusion
The story above presented a C program to add two integer numbers.

When we tangle this story, what we get is everything and only those which are embedded between

#+BEGIN_SRC C

#+END_SRC

The ‘C’ tells that it is a C program. We then have our C program ready to compile and run. By writing the story we actually capture all the details of the program. Following this method, needless to say the readability and maintainability of the code increases. You can give this method a try!


Learn More! Check out:

design-of-a-programmer-cover-LowRes

Design of a Programmer

Advertisements

Let me Know What you Think!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s