Wednesday, December 24, 2014

Using printf in C++ Instead of cout

If you study C++ from textbooks you will see the cout function used to print.  Later you might be surprised to see people using the older printf function from the C programming language within C++ programs.  When I first ran across this practice I asked why the older function was used and I was told that it was more efficient.

It is hard to find supporting evidence for this claim, so here is a link to "In defense of printf" by Kenny Kerr, which has this quote: "...cout has a serious performance problem."  The author presents a chart of performance measurement.

I remember the book Efficient C++: Performance Programming Techniques by Bulka & Mayhew recommending printf over cout, but I have lost my copy of this book and cannot verify my recollection.

So if you see someone using printf in a C++ program, be aware that it is an acceptable practice.  For a student's purposes, cout is fine.

Robert

Saturday, December 20, 2014

Dijkstra at the University of Texas Austin

I was surprised to learn that Edsger Dijkstra had been a Professor Emeritus in the Department of Computer Sciences at The University of Texas at Austin.  I thought of Dijkstra only as a Dutch computer scientist.  UT has a nice tribute to Dr. Dijkstra on the web.  The world famous Dijkstra taught students at U.T.!  Wow.  That tribute article provides a very nice, detailed description of his career and the importance of his ideas.  The university has scanned many of his papers and makes them available online.  His famous article, “Go To statement considered harmful," is available as paper EWD 215: A Case against the GO TO Statement.  It is a short paper, 4 pages plus the cover page.  I recommend it as a short, very readable paper.  His "Notes on Structured Programming" is much longer, but very influential.  The university maintains an archive of Dijkstra's papers.

If you are curious about computer science, I cannot think of a better way to explore the field than to explore Dijkstra's writings.

Robert

Sunday, October 5, 2014

The R Programming Lanuage

If you are interested in learning the R programming language, then I will share with you what I am doing to learn R.  I have chosen to use Introductory Statistics with R by Peter Dalgaard.
http://www.amazon.com/Introductory-Statistics-R-Computing/dp/0387790535/

I am enjoying the book.  The problems used in the book are from the life sciences.  The author tells you to install the ISwR package.  ISwR actually stands for "Introductory Statistics with R," so it is written for this book.  The book is not perfectly clear on how to install the package, so I'll explain that.

After following the book's instructions on how to install the R programming language, you open the R programming IDE, you look for the menu item that says "Packages > Install Packages." You are asked to pick mirror site.  I picked the one in Texas (TX), then you have a long list of packages to peruse.  Find ISwR and select it.  The only tricky part is that when it asks you if you want to install the package in a private library, you must say "yes."  If you say "no" on a Windows machine, then the installation of ISwR breaks.  Just say "yes."

Have fun learning R!

Robert

Saturday, September 13, 2014

Programming Language Popularity

If you want to learn a second computer programming language, how do you choose which one to learn? One factor in making your choice might be the popularity of the language. If you want to get paid for knowing a language, you avoid languages that provide few opportunities.  I think your first language should be based on a combination of popularity and its ease for learning.  It is hard to beat Python as a good 1st language.  You can peruse the popularity lists and see how Python stacks up in popularity.

PYPL PopularitY of Programming Language Index

From their website:  "The PYPL PopularitY of Programming Language Index  is created by analyzing how often language tutorials are searched on Google : the more a specific language tutorial is searched, the more popular the language is assumed to be."
https://sites.google.com/site/pydatalog/pypl/PyPL-PopularitY-of-Programming-Language

This is relying on the wisdom of the crowd.

The RedMonk Programming Language Rankings

The "RedMonk Programming Language Rankings" are derived from a correlation of programming activity on GitHub (usage) and Stack Overflow (discussion).  This is a measure of language usage.

Go to this website and look for the latest ranking:
http://redmonk.com/sogrady/category/programming-languages/
Besides rankings, there are articles.  An article at the time of writing this post is "Will Python Kill R?" (R being a statistical analysis language)
http://redmonk.com/sogrady/2013/11/26/python-r/

The TIOBE Index

The monthly TIOBE Programming Community Index shows the top 10 languages' popularity graphically and the top 20 languages with a rating. The numbers are based on searching the Web with certain phrases that include language names and counting the numbers of hits returned.  This is a measure of interest in a language.
http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html

The Transparent Language Popularity Index

This ranking is also based on internet searches. This website makes the open source software used for the ranking available for you to download, study, and use it yourself if you wish.
http://lang-index.sourceforge.net/

Trendy Skills

This website measures Skills that employers seek in the IT industry. This is the only list based on employment
http://trendyskills.com/
This webpage is interactive. You can click on the bottom to view by absolute popularity, popularity by percentage, and salary.  The salary numbers might be misleading.  If a preponderance of jobs for a language are in California, then that salary will be available only in California.

Trendy listed a language I needed to look up:  "S".  S is a statistical programming language.
http://en.wikipedia.org/wiki/S_%28programming_language%29
I am familiar with a major variant of S, called "R," but S was news to me.

No matter how many languages you know, you are always wondering what you should learn next.  These popularity lists might be helpful to you.

Robert

Wednesday, April 2, 2014

Software Maintenance and Message IDs

I am going to describe a couple of very bad programming practices for handling messages.  These bad practices increase the cost of maintenance and lead to processing errors.  See my article on the cost of software maintenance.

Here is the background:  It is common for one software program to send messages through sockets to another program.  It is common for the messages to have a message ID and message data.  It is common for the receiving program to receive multiple messages and process the data differently for each message.  It is common to test with a series of "if" statements.  We will use this example:

if message.id == "MyVerySpecialMessage":
    myVerySpecialMessageFuntion(message.data)

The first mistake in handling messages is to test on a substring of the full message ID string.  In Python it would look like this:

if message.id.find("VerySpecial") >= 0:
    myVerySpecialMessageFuntion(message.data)

This will work, but there are two problems.  One is that a new message could be added later that has the message.id of "TomsVerySpecialMessage".  Testing on the substring "VerySpecial" would catch this message as well and feed the data to the wrong function.  The other problem is that when searching to find where in all the lines of code the message ID "MyVerySpecialMessage" is processed, you will not find this line of code because it does not use the full ID that you are using for searching.  Searching code with functions like grep are the only way we can work with undocumented code.  Using substrings of a message ID is an insufferable aggravation for software maintenance:  don't do it.

The second mistake in handling messages is to use an else statement to implicitly handle and process a message.  An else-if statement should always be used to explicitly process a message.  The else statement should only be used to report unexpected and not-handled messages.

Here is a simple example.  There are two message IDs:  "MessgeOne" and "MessageTwo".  The poor practice of processing with an else-statement looks like this:

if message.id == "MessgeOne":
    messgeOneFuntion(message.data)
else:
    messgeTwoFuntion(message.data)

If a message with an ID of  "MessgeThree" hits this code block, it will go into the wrong function.

The proper way to handle the "MessgeOne" and "MessageTwo" example is as follows (elif is the Python way of saying "else if":

if message.id == "MessgeOne":
    messgeOneFuntion(message.data)
elif message.id == "MessgeTwo":
    messgeTwoFuntion(message.data)
else:
    reportError( message.id)

Avoid bad programming practices.  They are not outgrown.  I have seen programmers with decades of experience making exactly these mistakes.  Begin with good programming practices and stay with them.

Robert

Sunday, March 16, 2014

Software Maintenance and Variables

A variable for every purpose, and a purpose for every variable.  This is a useful maxim for writing maintainable, coherent software.  A variable is more than a piece of computer memory used for storing data.  Variables with useful names and coherent usage contribute to the meaning and structure of a program.  Changing the meaning of a variable is a symptom of lazy coding that degrades the quality of the code.

Here is an example of "a variable for every purpose." I worked on one software product that had two resources (A and B) that were used with equal numbers: NumberOfA was equal to NumberOfB.  The programmer made the mistake of using one variable for two purposes.  Instead of using NumberOfA = 16 and NumberOfB = 16, he used NumberOfUnits = 16.  Perhaps he did not make this mistake on his own.  Perhaps he was directed by a code reviewer to eliminate a variable for the sake of efficiency.  Some years later, the customer decided to double the number of type B hardware units in the system.  So I had to read every section of code that used NumberOfUnits and change it to either NumberOfA or NumberOfB.  Changing the variable declarations and initializations was easy.  But the way this code was written, the variable NumberOfUnits appeared in hundreds and hundreds of places, and every instance of this variable had to be studied and modified.  Every time I had to guess, due to a lack of comments, whether it was A or B hardware units that were being commanded, an element of risk was introduced into the product.  What could have been a simple change, changing from NumberOfB = 16 to NumberOfB =32, turned into a laborious task that went on for hours.  Time is money, and lazy programming is expensive in the long run.

Here is an example of "a purpose for every variable."  One complex product I worked on had a class with an instance variable that was used for two different purposes.  The same variable was used in two different operations (methods), and used for two different purposes.  This was done out of sheer laziness.  This worked at first because the operations did not overlap in the original releases of the code.  Later on, however, new requirements from our customer required the first operation to be used again, after the second operation had changed the value of the shared instance variable.  Because one variable was used for two purposes, the code was now broken and a hunt for the bug ensued.  Once the problem was discovered the solution was to simply add a second variable for the second purpose.

There is nothing clever about eliminating a variable.  Following the maxim, "a variable for every purpose, and a purpose for every variable," saves money.  There is a difference between efficiency and laziness.

Robert

Wednesday, February 26, 2014

Python GUIs with PyGTK and Glade

PyGTK is a system for developing GUIs in Python.  PyGTK runs on Linux and on Windows.  Glade is a free graphical GUI builder for use with GTK.  Glade can run on Windows or Linux.  It is important to know that PyGTK does not yet run with Python 3.  I uninstalled my Python 3 and installed Python 2.7.5 because PyGTK will run with Python 2.7.  You must have a compatible Python when using PyGTK.  I got Python 2.7.5 from this website.  Then I downloaded the pygtk-all-in-one-2.24.0.win32-py2.7.msi installer from this website.  This package comes with Glade.

You can write GUI code by writing code in PyGTK in the same fashion you can write code to create GUIs with Tkinter, which now comes bundled with Python.  But once you get used to using a graphical GUI designer like Netbeans, you will want to use a tool like Glade.

There are tutorials available for PyGtk.  One example of a good tutorial that comes with sample code is the tarball  pygtk2tutorial.tgz that can be downloaded from the website http://www.moeraki.com/pygtktutorial/index.html  Look for the link named "Gzip'd Tarball of Tutorial (1.42 MB)".  Once you unzip it, go into the folder pygtk2tutorial and double-click on index.html to read the tutorial with your web browser.  The example code is in the folder pygtk2tutorial\examples.

This kind of support makes Python a great language, for beginners and professionals alike.

Robert