Language Nonspecific: Back to Fundamentals

by Kn1ghtl0rd

Programming today has become a divided front.  On one side you have the Microsoft .NET programmers and on the other side you have the Linux/Java/web programmers.  When someone decides they want to start writing software they are faced with one important question: which language to learn first?  Although this question is "important," it should not be the focus of an aspiring developer.  In my experience as a developer, I have found out one very important thing.  If you are a good programmer, a great programmer even, it doesn't matter what language you use because you could use any one of the hundreds of languages available.  It all comes down to fundamentals and understanding how to think like a coder.  By restricting yourself to a specific language you are limiting the type and quality of work you can create.  Understanding coding structure, logical analysis, and above all having the hacker mind will allow you to utilize the tools that best fit the scenario and not have the language define your path.

The first language I ever learned was RPG IV for the AS/400 computing system.  Granted, this is an old language that hasn't changed much in 15 years but it is a well documented, structured language that gave me a base to learn how to be a good programmer, not just and RPG programmer.  Once you learn one language and understand the fundamentals you essentially know any language you want.  I can pick up a new programming language with a small learning curve in syntax and execution that lasts only about a week.  I am going to share my technique for learning programming languages and how you can utilize the fundamentals of software design to allow you to unchain your software and become language nonspecific.

The first step is to pick a well documented language that is easy to read.  Why choose one easy to read?  Because you will remember it better.  If English is your primary language, and you read a quote in English, you will more than likely remember it.  Now read the same quote in Spanish and try and remember it.  So with absolutely no knowledge of Spanish, you will not only forget the quote but probably misquote and mispronounce it when you try to recall it.  The same basic theory can be used in programming.  Say you learn Visual BASIC or Gambas, two very easy to read languages.  You have a command like this:

Dim intCash as Integer

Now you know that the command is defining (Dim) the variable (intCash) as an integer.  So now read the same line in C:

int intCash;

It is basically the same.  You recognize the Int as being a data type of integer because you remember the Int from Visual BASIC.  The same goes with any other command you have.  It is all a matter of reference.  So this solves the language issue, but now what about programming structure?  The most important thing is to think modular.  The smaller you can break tasks down the easier it is to manage them; it also makes one of the fundamental Object-Oriented Programming (OOP) theories easier, re-use of code.  By making things small and nonspecific you can take those pieces and plug them into just about any application that uses that same process.  For instance, take a program that takes two numbers, divides them together, then does calculations based on that output.  Here is a code example:

int A = 2;
int B = 14;
int C;

int main() {
  C = A/B;
  if (C > 7) {
    printf ("C is greater than 7");
  }
else {
  printf("C is less than 7");
 }
}

This is a pretty straightforward little code block.  Now you may be saying, why would I modularize something so small to begin with?  Well, you don't have to try and slim it down or anything like that.  Just try and think in pieces.  So instead of the code above, you could write something like this:

int A;
int B;
int C;

int main() {
C = divide(A, B);
  if (C > 7) {
    printf("C is greater than 7");
}
else {
  printf("C is less than 7");
 }
}

int divide(int a, int b) {
  int c;
  c = a/b;
  return c;
}

So yeah, there is more code than the other program but now you are able to plug in any two numbers and divide them in any sequence that you want.  Not only that but you can reuse the divide function in any other app you wish.  Now you are modular.  So the next time you need to divide something you don't have to figure out whether you want to divide "A" by "B", or vice versa, and then change it down the road.  You can instead change the input because the function will always be the same.  This tiny little function is a very basic example of making your program modular.  It is also probably not very practical but for demonstration purposes it is easy to understand.

The next thing that is important when learning to program is to understand classes.  Most languages give you basic classes to work with.  Every data type, whether they are integers or strings or Boolean, are all classes.  Each class has specific properties to it and tasks that can be performed to them.  You cannot divide a Boolean object because that is not a method in that class.  So by taking this idea of data types you can create new types and you an do things specific to that type.

As an exercise, pick an object in your house that has multiple parts and multiple functions it performs.  For this article, I will choose a radio.  A radio has multiple parts; buttons (on/off, AM/FM, etc.) and multiple functions: tune up or down, volume up or down, etc.  So your programming language doesn't have a stock radio class and instead of defining each part when you write your code you decide to write a class instead.  Here is an example of a simple radio class:

class radio;

float tunedTo;
float minimumStep;
int minimumFrequency;
int maximumFrequency;
int maxVolume;
int currentVolume;
bool modType; // false = am - true = fm
int presetSTation();
int pre;

function tuneUp() {
newFreq = tunedTo + minimumSTep
if newFreq <= maximumFrequency
  tunedTo = newFreq
else
  print 'max'
  break
}

function tuneDown() {
newFreq = tunedTo - minimumStep
if newFreq >= minimumFrequency
  tunedTo = newFreq
else
  print 'min'
  break;
}

function toggleModulation() {
if modType = true
 modType = false
 minimumFrequency = 530
 maximumFrequency = 1700
 minimumStep = 10
 print 'am tuning';
else
 modType = true
 minimumFrequency = 87.5
 maximumFrequency = 108.0
 minimumStep = .5
 print 'fm tuning';
end if
}

function selectPreset() {
tunedTo = presetStation(pre);
}

function volumeUp() {
if currentVolume < maxVolume
  currentVolume++;
else
  print 'volume already at max';
  break;  
end if
}

function volumeDown() {
if currentVolume > 0
  currentVolume--;
else
  print 'volume already at zero';  
  break;  
end if
}
end radio;

So as you can see from this small class, pretty much every part of a basic AM/FM radio is included and each function that the radio can perform is defined.  Now in your program, to tune up your radio all you have to do is invoke the tuneUp() function instead of defining what the radio is tuned too, what it can be tuned too, and how many steps to tune before stopping.  All of this is already defined in the class and every object that is of the type radio will be able to do the same things.  This is the essential piece of programming that you need to understand to be a good programmer because classes allow you to be modular and still be able to have complex data manipulation without all the headaches.  Not only can you do things to a single radio object but you can use two of the same type and do calculations on that.  So you could essentially test one radio against another to make sure they are doing what you want.

This is just the tip of programming fundamentals but by learning this stuff first you will save yourself a lot of debugging and coding time.  Maybe not initially but when you have a good sized library of custom functions and classes at your disposal you will essentially be able to write programs like putting together a puzzle.  The only thing that will be custom to your application will be the logic behind it and how those pieces fit together in the implementation in question.

A note on logic is to try and not be redundant as much as possible.  It is easier to do that if you are modular.  You don't need to add the same things a bunch of times to get the same answer.  Do it once and then reuse it.  Another way to make sure your logic doesn't become a crap shoot is to have good naming conventions for variable.  It makes your program easier to read and for other people to understand.  A good method that I use is called the "Hungarian Notation" which is a way of utilizing object types in variable names so you can keep track of the kind of data you are working with.

For instance, if you are defining an integer data type, put int at the beginning of the variable name and you will never forget that your variable is an integer.  You can modify the notation scheme to suit your personal preference but most programmers will still be able to understand it with a little bit of coaching on your notation style.  The most important thing about programming logic though is to be linear, or as linear as possible.  You don't read a book from back-to-front, bottom-to-top; you read it front-to-back, top-to-bottom.  Remember that when writing software and avoid going backwards in your code, and never ever use GO or GOTO statements!  They are evil and unnecessary if you just think for a minute and try to be linear.

Remember the fundamentals and you will be able to write any type of app in any environment with any language because a computer program ends up being the same thing after compiling, no matter what language you are using.  There are a million ways syntactically to do the same task but by being a good programmer you can be sure that you are doing it correctly no matter what syntax you may be using.

Return to $2600 Index