Monday, October 22, 2012

Logic for Programming and Comments on C Programming

On two occasions I have handed a copy of The C Programming Language by Kernighan and Ritchie (K&R) to a bright young person.   I had a chance to speak with one of these young people later on and found that he had been stopped in his tracks when he got to Boolean algebra.  He totally ground to a halt when he hit bit-wise operators. 

I wrote a hand-out on the logical operators, AND and OR to help young people who are studying programming.  I have included the bit-wise operators used in C and I have posted this hand-written set of truth tables on the internet.  There are a lot of bright young people who are capable of teaching themselves how to program, but they have to really want to learn how to do it in order to overcome obstacles.

Then again, young people should not have too many obstacles.  The K&R book is brilliant, but it is terse and might not be a good fit for everyone.  C Programming, A Modern Approach by K.N. King has more explanation and might be a better match for some people.  On the other hand, The C Programming Language has a companion book that is a valuable complement:  The C Answer Book: Solutions to the Exercises in 'The C Programming Language' by Tondo and Gimpel.  Scott Gimpel is the creator of FlexeLint, a useful static analysis tool.

Overall, I believe a young person learning to program will benefit from some coaching, especially when learning to program in C. 

Robert

A previous article on C programming is:
"A Fix for K & R Chapter 1", July 2, 2012, Monday

Saturday, October 13, 2012

Notepad++ and Python

Python is very sensitive to white space because it interprets code blocks by indentation level instead of curly braces.  It is vital to use uniform spacing.  I agree with those programmers who use 4 spaces for Python indentation and avoid use of tabs.  You can setup Notepad++ to help maintain this standard.  Here is how:

Open Notepad++.  Select Settings > Preferences > Language Menu/Tab Settings
Leave Tab Size at 4, but check the box that says "Replace by space".
Then go back to the main menu and select View >  Show Symbol > Show White Space and Tab.
If you look closely you will see faint dots for the spaces.
If you open a file that has tabs you will see them displayed with arrows so you know you have them.

You can do a Control-f to open the find window.  It has a radio button in search mode that has "extended" and allows you to search for the \t character, which is a tab.  You can use the "replace" tab in the find window to replace all tabs (\t) with 4 spaces.  This is a convenient way to clean up files with tabs.

If you work on projects with multiple people you will see all manner of tabbing and spacing, which can break Python code.  Python code is sensitive to indentation errors.  It is important to be consistent.  This sensitivity to indentation errors makes Python code brittle in a large programming environment.

James Coplien had this to say:  "Back in my days working in the Bell Labs Software Production Research organization ... we had  some back-of-the-envelope findings that suggested that consistent indentation style was one of the most statistically significant indicators of low bug density."[1]

Robert

[1] Forward by James Coplien, p. xxii, in Clean Code, A Handbook of Agile Software Craftsmanship by Robert C. Martin

Installing Python

Here is a tip for when you install Python.  After you have googled python, gone to their website, downloaded the version you want, and launched the installer, you will get to a box that has a list of features to install.  At the bottom of the list it says "Add python.exe to path" and there is a red X in the box next to that choice.  Right-click on that choice, read the options on the drop-down box that appears, and check the box that says "install all features."  You do not want to leave that red X in that box.

You do want the installer to add python to your path variable.  If you do not have the installer add python to your path, you will have to do it yourself.  Here is how you test to see that your  path does have python in it:  Open a Windows command window and type PATH and hit a carriage return.  The OS will print the path and you can read it and look for "C:\Python33" or whatever version is latest.  Or, you can just test to see if the path variable is set properly by entering "python --version" and see if the command works.  The output will by "Python 3.3.0" or something similar if you get another version.

If you already know how to program, The Quick Python Book is a good introduction to the language.  My preference is to edit Python code with Notepad++ and use a command window for running code.  The Quick Python book acts like you will do all your work with the IDLE Python GUI.  I just use the IDLE tool to test small commands.

Python is a nice language.  I went back to look at PERL and I can see why people prefer Python to PERL.  PERL forces you to put funny symbols called sigils in front of variables.  I forgot about that.

Robert

Saturday, October 6, 2012

Install Tkdiff on Linux

If you want a visual comparison between two files on Linux, then vimdiff is available for you, but the visual presentation is very ugly.  Vimdiff comes with vim.   Fortunately Tkdiff is available for Linux and it is easy to install if you know how.

Here are directions for installing Tkdiff on Linux.  My primary machine is a Windows box, so I download using my Windows box and then move files over to my Linux box. If you download from the internet using your Linux box, the directions might be very similar, but a little different.

Use this link to download Tkdiff for Linux:
http://sourceforge.net/projects/tkdiff/files/tkdiff/4.2/tkdiff-4.2.tar.gz/download

Use WinZip extract once to get from tkdiff-4.2.tar.gz to tkdiff-4.2.tar
Use WinZip extract again to get from tkdiff-4.2.tar to folder called tkdiff-unix which has these files:
CHANGELOG.txt
LICENSE.txt
README.txt
tkdiff

Move the entire folder to your linux box, although you really only need the tkdiff file.  When you place licensed software on your linux box, I think it is appropriate to have the license on the machine.

Change tkdiff to Unix format, and put it in the bin directory.  Here are the commands to do this.  Of course, when you execute su to become root, you must know the root password.

dos2unix tkdiff
su
cp  tkdiff  /usr/local/bin/


Now you can go to some directory and execute
tkdiff  file another_file
and see the difference.

You will be please to see the visual improvement over vimdiff.  If you downloaded directly onto the Linux box, the tkdiff file might not be in DOS format.  The program dos2unix fixes the carriage return differences between Unix and Windows.

I will also mention that there is a Windows version available here.
http://sourceforge.net/projects/tkdiff/
The Windows version has an installer called TkDiff-4.2-Setup.exe
I prefer Kdiff3 for comparing files on Windows.  I installed Tkdiff instead of Kdiff3 on Linux because Tkdiff is generally much easier to install on Linux.

Robert

May 2017: Tkdiff is nice because you can put it on media and install it on a computer that does not have a decent visual tool for diff'ing files.  However, if you are on a Linux computer, look and see if the kompare (on KDE systems) or meld  (on Gnome systems) tools are available.  Then you won't need Tkdiff.