I say this a lot, but programming
in particular and computation in general is all about the data. If there is no
data to work on, the power of a computer is steered towards being irrelevant.
One of the most important, easy
and beginning lessons to learn in any programming course is that of the data
types. Data types are ways to tell the compiler what kind of data a certain
variable is supposed to hold. The rest is done by the compiler, you don’t usually
have to get your hands dirty with bits and bytes of the boorish (occasionally)
memory. More often than not, following are the fundamental data types that you’ll
find in a programming language:
- Integer
- Float
- Char
- Bool
- String
- Hex
- Octal
The philosophy behind these data
types is that any kind of data, singular or compound, simple or complex; can
and have been represented gracefully by these fundamental data types. And
whenever there is a need for abstraction, encapsulation, etc (read pillars or
OOPs), one has been given the gift of object oriented programming.
That right there is a happy and
healthy set up using which great many people have achieved great many things.
But there is one thing that differentiates programming which is extremely accurate
and programming which is simply magical. And that thing is, providing context
to your data.
You see not all integers share
the same personality. What they have in common is their structure, but they can
differ tremendously in context, relevance and usage. For example, an integer is
what represents the length of an array. And it is an integer only that can be
used to represent someone’s bank balance account. In fact an integer can
represent data from so many fields and cases, that having them all be
classified as being just integers is unfairly ignorant of us. Our way of
providing variables with some meaning is by having appropriate names. This
approach does well when the degree of diversity in your program and the
population of variables in each category is not beyond the comprehension of average
human conscious. But when the variety and volume of your project starts
burgeoning, then variable names are no longer enough to provide context to
variables. One such example of this that I have struggled with recently is the
source code of the Linux kernel. The data types of the variables used in some
of those files were initially so unfamiliar to me that suddenly the C programming
languages seemed so labyrinthine. But later I realized later that those oddly
new data types weren’t new and unknown at all, but were aliases for our good and old fundamental and complex data types (integers, floats and structures, etc). These
aliases were put in place as a careful and elaborate effort to keep every
component of the code in context, and hence the whole project, polite.
How is all of this achieved?
Using the typedef keyword. The typedef keyword basically gives a new name to an
already existing (inbuilt or programmer defined) data types that serves as an
alternative (not a replacement) for that data type. For example if I wish to
have bank balances of several people in my program then I can choose to give an
alternate name BALANCE to the data type integer and use it to define all my
bank balance variables.
typedef int BALANCE;
BALANCE b1, b2, b3;
Similarly, I can have other nick
names depending on what I need. To see an example of this concept in action,
click here. That’s it for now. Ceep Koding.
Nicely elaborated. Thanks.
ReplyDelete