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
Sunday, March 16, 2014
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
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
Sunday, October 13, 2013
The Purpose of this Blog
If your child is interested in computer programming, I have started this blog to assist you. This is why there are some articles on which is the best first language for a beginner. The slant is for beginners, but there is some information for programmers in general.
I will also share items of interest related to software.
Robert
I will also share items of interest related to software.
Robert
Self Documenting Code and Ecclesiastes
In a previous article, Cost of Software Maintenance, I denigrated self-documenting code. Because so many people believe in it, I should discuss it further.
Code changes, but documentation is usually not updated to keep pace with the code changes. Documentation becomes increasingly outdated. This is one reason given for skipping documentation. Another reason is the lack of time or money when first writing the code. Limited resources is a legitimate reason to minimize documentation. Many believe that time-to-market is so important that documentation must be sacrificed. Frankly, many programmers have trouble writing. You do not see many English majors coding software.
Alexis Ohanian said in Without Their Permission that he and his partner worked day and night for a month to create a website that was "only slightly embarrassing." He also said, "...if you are not a little embarrassed by what you launched, you waited too long. It need only be good enough to be useful" (page 62). He is not alone in saying this. I remember hearing the president of Rogue Wave saying at a conference that if you spend too much time adding quality to your product, you will lose market share to a company that can get a less refined product out to market more quickly and take the market.
On the other hand, Crossing the Chasm by Geoffrey A. Moore describes the problem companies have of breaking out of the early-adopter ghetto. When your product barely works and customers want a solid product, your growth is blocked. Early adopters want the latest technology and they are willing to wait for quality to improve. Lack of documentation can make it impossible to improve the product and expand your market share. I saw one company try to out-source documentation of existing software to a company in Albania because the foreign programmers were cheaper. It did not work. Company growth was stymied because new staff took too long to decode the code and the outsourcing of documentation failed.
If use of self-documenting code will solve the documentation problem, then that would be great. But what is self-documenting code? The best approach is to use a documentation generator that creates HTML documentation from properly formatted comments. Javadoc for Java and Pydoc for Python are examples. The worst approach is number one on this list: using meaningful names for variables and functions. Meaningful names, with no comments, is very cheapest form of self-documenting code. The following section explains how this breaks down.
Ecclesiastes
The book of Ecclesiastes says in 1:15, "What is twisted cannot be straightened; what is lacking cannot be counted." The problem with meaningful names is that a number of programmers suffer from twisted logic and a faulty-world view. Let me give you a real example, altered to protect the identities of those involved.
I was asked to modify Product X to issue new commands to hardware devices. Only one device at a time was turned on and I needed to tell the code to find which device was turned on and then command it. I found an undocumented function named getActiveDevice. After I modified the code I went to the customer site to test it against the real devices. The code changes did not work because the undocumented function getActiveDevice did not keep track of which device was turned on (my interpretation of Active), but it kept track of which device had a display visible on the GUI. (The GUI changed the devices that were visible depending on which tab was selected.) The function should have been named getVisibleDevice. The world-view of the previous programmer was not a view of the customer product, but a view of the programmer's GUI code. The real world was not used as a point of reference by the previous programmer, so the variable names and function names were divorced from reality and were less than useless.
You cannot count on variable names or function names to tell you what is going on in the code if the coder used twisted logic or a faulty-world view. If there is no documentation, and the code has no comments, and when you object you are told that the code is self-documenting, then they are "shining you on." Documentation generators like Javadoc or Pydoc require comments. If self-documented code is done properly, there are meaningful comments in the code that document generators turn into HTML documents. You cannot count on meaningful names alone to document the code.
Robert
Code changes, but documentation is usually not updated to keep pace with the code changes. Documentation becomes increasingly outdated. This is one reason given for skipping documentation. Another reason is the lack of time or money when first writing the code. Limited resources is a legitimate reason to minimize documentation. Many believe that time-to-market is so important that documentation must be sacrificed. Frankly, many programmers have trouble writing. You do not see many English majors coding software.
Alexis Ohanian said in Without Their Permission that he and his partner worked day and night for a month to create a website that was "only slightly embarrassing." He also said, "...if you are not a little embarrassed by what you launched, you waited too long. It need only be good enough to be useful" (page 62). He is not alone in saying this. I remember hearing the president of Rogue Wave saying at a conference that if you spend too much time adding quality to your product, you will lose market share to a company that can get a less refined product out to market more quickly and take the market.
On the other hand, Crossing the Chasm by Geoffrey A. Moore describes the problem companies have of breaking out of the early-adopter ghetto. When your product barely works and customers want a solid product, your growth is blocked. Early adopters want the latest technology and they are willing to wait for quality to improve. Lack of documentation can make it impossible to improve the product and expand your market share. I saw one company try to out-source documentation of existing software to a company in Albania because the foreign programmers were cheaper. It did not work. Company growth was stymied because new staff took too long to decode the code and the outsourcing of documentation failed.
If use of self-documenting code will solve the documentation problem, then that would be great. But what is self-documenting code? The best approach is to use a documentation generator that creates HTML documentation from properly formatted comments. Javadoc for Java and Pydoc for Python are examples. The worst approach is number one on this list: using meaningful names for variables and functions. Meaningful names, with no comments, is very cheapest form of self-documenting code. The following section explains how this breaks down.
Ecclesiastes
The book of Ecclesiastes says in 1:15, "What is twisted cannot be straightened; what is lacking cannot be counted." The problem with meaningful names is that a number of programmers suffer from twisted logic and a faulty-world view. Let me give you a real example, altered to protect the identities of those involved.
I was asked to modify Product X to issue new commands to hardware devices. Only one device at a time was turned on and I needed to tell the code to find which device was turned on and then command it. I found an undocumented function named getActiveDevice. After I modified the code I went to the customer site to test it against the real devices. The code changes did not work because the undocumented function getActiveDevice did not keep track of which device was turned on (my interpretation of Active), but it kept track of which device had a display visible on the GUI. (The GUI changed the devices that were visible depending on which tab was selected.) The function should have been named getVisibleDevice. The world-view of the previous programmer was not a view of the customer product, but a view of the programmer's GUI code. The real world was not used as a point of reference by the previous programmer, so the variable names and function names were divorced from reality and were less than useless.
You cannot count on variable names or function names to tell you what is going on in the code if the coder used twisted logic or a faulty-world view. If there is no documentation, and the code has no comments, and when you object you are told that the code is self-documenting, then they are "shining you on." Documentation generators like Javadoc or Pydoc require comments. If self-documented code is done properly, there are meaningful comments in the code that document generators turn into HTML documents. You cannot count on meaningful names alone to document the code.
Robert
Saturday, October 12, 2013
Cost of Software Maintenance
Software maintenance is the dirty laundry of programming. Every programmer wants to write code. No one wants the mundane task of maintaining code. But if a product is going to survive in the market place, it must be used for an extended period of time and eventually need maintenance. Software maintenance includes expanding the functionality of a system to meet new requirements, so maintenance is more than fixing defects.
Changing a software system takes time, which costs money. Frequently, the staff making the changes was not present during the creation of the product, so changes are made by people with no knowledge of the system. Saying code is self-documenting is good for a laugh among mature programmers. (The difference between an experienced programmer and a mature programmer is that a mature programmer has learned from experience. It is astounding how many experienced programmers make amateurish mistakes because they have never learned from their experiences.) Lack of documentation is one of many factors that increase the cost of maintenance.
Canright's Costs of Software Maintenance
The Wages of Sin is Death
Code with poor maintainability features, after suffering many modifications by a changing parade of programmers, will turn into a Big Ball of Mud. You might want to read the original article: http://www.laputan.org/mud/. You might enjoy hearing a lecture on the topic (66 minutes) on YouTube: http://www.youtube.com/watch?v=h6Y9aJhqO78 by the author, Brian Foote. This video lasts an hour and it is worth your time.
If you enjoyed reading Thomas Pynchon's Gravity's Rainbow , William Faulkner's Absalom, Absalom! or James Joyce's Ulysses (three illogical, hard to read books), then you might enjoy software maintenance on a Big Ball of Mud. But most programmers suffer frustration when working on projects with hard to read code. At this point maintenance can break down.
The ultimate cost of unmaintainable software is the death of the software product. If you do not have replacement products, you can lose your customers and go out of business.
If you have developed a product that is so fragile, twisted, and unmaintainable that it is sure to die, then you need to unload it. You need to sell it to an unwise investor before the product implodes. Did you notice how many dot com acquisitions disappeared?
Robert
PS: I will elaborate on unloading your start up. Consider Douglas Crockford, the inventor of the JSON data format and founder of Electric Communities. Hundreds of millions of dollars were invested in this company. He sold out. Eventually the company went bankrupt and was sold for peanuts. He got out in time. You do not want to be the owner when the start up goes bankrupt!
List of Related Articles
Software Maintenance and Variables March 16, 2014
Software Maintenance and Message IDs April 2, 2014
Unique Method Names for SW Maintainability August 26, 2017
Avoid Bit Fields October 1, 2017
Changing a software system takes time, which costs money. Frequently, the staff making the changes was not present during the creation of the product, so changes are made by people with no knowledge of the system. Saying code is self-documenting is good for a laugh among mature programmers. (The difference between an experienced programmer and a mature programmer is that a mature programmer has learned from experience. It is astounding how many experienced programmers make amateurish mistakes because they have never learned from their experiences.) Lack of documentation is one of many factors that increase the cost of maintenance.
Canright's Costs of Software Maintenance
- Assume a basic quantum of maintenance cost, Q.
- If no documentation, add Q.
- If code is riddled with poor logic, add Q.
- If the project lacks debugging tools or features, add Q.
- If the code has no error handling, add Q.
- If good programming practices (for maintainability) are omitted, add Q.
The Wages of Sin is Death
Code with poor maintainability features, after suffering many modifications by a changing parade of programmers, will turn into a Big Ball of Mud. You might want to read the original article: http://www.laputan.org/mud/. You might enjoy hearing a lecture on the topic (66 minutes) on YouTube: http://www.youtube.com/watch?v=h6Y9aJhqO78 by the author, Brian Foote. This video lasts an hour and it is worth your time.
If you enjoyed reading Thomas Pynchon's Gravity's Rainbow , William Faulkner's Absalom, Absalom! or James Joyce's Ulysses (three illogical, hard to read books), then you might enjoy software maintenance on a Big Ball of Mud. But most programmers suffer frustration when working on projects with hard to read code. At this point maintenance can break down.
The ultimate cost of unmaintainable software is the death of the software product. If you do not have replacement products, you can lose your customers and go out of business.
If you have developed a product that is so fragile, twisted, and unmaintainable that it is sure to die, then you need to unload it. You need to sell it to an unwise investor before the product implodes. Did you notice how many dot com acquisitions disappeared?
Robert
PS: I will elaborate on unloading your start up. Consider Douglas Crockford, the inventor of the JSON data format and founder of Electric Communities. Hundreds of millions of dollars were invested in this company. He sold out. Eventually the company went bankrupt and was sold for peanuts. He got out in time. You do not want to be the owner when the start up goes bankrupt!
List of Related Articles
Software Maintenance and Variables March 16, 2014
Software Maintenance and Message IDs April 2, 2014
Unique Method Names for SW Maintainability August 26, 2017
Avoid Bit Fields October 1, 2017
Saturday, July 20, 2013
Python, Best Language for Beginners
Basic is a great language for teaching programming. The BASIC programming language
was created at Dartmouth College as a vehicle for teaching programming
to beginners. But when I look at projects at the GitHub website, you
will see that Python is on the list of most popular languages, but Basic
is not: https://github.com/languages
Based on features, Python is a good first language. Its style of coding is closer to that of Java than to C/C++, but it permits procedural programming while Java is strictly object oriented. Python's popularity together with its other strengths make it the best language for beginners.
I had previously recommended Basic as the best first language for beginners. How can you go wrong with Basic? Only if Python provides more practical opportunities. Kirk McDonald, president of PubMatic, an ad tech company in Manhatten, wrote an Op-Ed piece in the Wall Street Journal, May 10, 2013, titled, "Sorry College Grads, I Probably Won't Hire You." I'll quote him: "...please learn a little computer programming." He also said, "Dabble in a bit of Python." So the president of a tech company recommends Python, not Basic. (You can google the title and read the article online.)
I had previously recommended Python Programming for the Absolute Beginner by Michael Dawson. I continue to recommend it for beginners. You can go to the publisher's website and download the version of Python that is compatible with Python modules called Livewires and Pygame, which come with the Python installer used by the book. You can also download the source code from the book. This book is an excellent resource for the beginning programmer.
Robert
Here is an article with more explanation of why people like Python:
http://www.mihneadb.net/post/python-the-best-first-programming-language/
Here is another article saying Python is the best language for beginning programmers:
"Python squeezes out JavaScript, C as best starter programming language", January 23, 2014
http://www.itworld.com/application-management/401419/python-squeezes-out-javascript-c-best-starter-programming-language
Surprisingly, the C programming language was 2nd on the survey. I last mentioned C a a beginning language on April 13, 2013:
http://canrightonsoftwareandprogramming.blogspot.com/2013/04/the-c-programming-language.html
Additional blog posts:
Python GUIs with PyGTK and Glade February 26, 2014
July 9, 2014
"At the time of writing (July 2014), Python is currently the most popular language for teaching introductory computer science courses at top-ranked U.S. departments.
Specifically, eight of the top 10 CS departments (80%), and 27 of the top 39 (69%), teach Python in introductory CS0 or CS1 courses." This quote is from the following blog:
http://cacm.acm.org/blogs/blog-cacm/176450-python-is-now-the-most-popular-introductory-teaching-language-at-top-us-universities/fulltext
Based on features, Python is a good first language. Its style of coding is closer to that of Java than to C/C++, but it permits procedural programming while Java is strictly object oriented. Python's popularity together with its other strengths make it the best language for beginners.
I had previously recommended Basic as the best first language for beginners. How can you go wrong with Basic? Only if Python provides more practical opportunities. Kirk McDonald, president of PubMatic, an ad tech company in Manhatten, wrote an Op-Ed piece in the Wall Street Journal, May 10, 2013, titled, "Sorry College Grads, I Probably Won't Hire You." I'll quote him: "...please learn a little computer programming." He also said, "Dabble in a bit of Python." So the president of a tech company recommends Python, not Basic. (You can google the title and read the article online.)
I had previously recommended Python Programming for the Absolute Beginner by Michael Dawson. I continue to recommend it for beginners. You can go to the publisher's website and download the version of Python that is compatible with Python modules called Livewires and Pygame, which come with the Python installer used by the book. You can also download the source code from the book. This book is an excellent resource for the beginning programmer.
Robert
Here is an article with more explanation of why people like Python:
http://www.mihneadb.net/post/python-the-best-first-programming-language/
Here is another article saying Python is the best language for beginning programmers:
"Python squeezes out JavaScript, C as best starter programming language", January 23, 2014
http://www.itworld.com/application-management/401419/python-squeezes-out-javascript-c-best-starter-programming-language
Surprisingly, the C programming language was 2nd on the survey. I last mentioned C a a beginning language on April 13, 2013:
http://canrightonsoftwareandprogramming.blogspot.com/2013/04/the-c-programming-language.html
Additional blog posts:
Python GUIs with PyGTK and Glade February 26, 2014
July 9, 2014
"At the time of writing (July 2014), Python is currently the most popular language for teaching introductory computer science courses at top-ranked U.S. departments.
Specifically, eight of the top 10 CS departments (80%), and 27 of the top 39 (69%), teach Python in introductory CS0 or CS1 courses." This quote is from the following blog:
http://cacm.acm.org/blogs/blog-cacm/176450-python-is-now-the-most-popular-introductory-teaching-language-at-top-us-universities/fulltext
Sunday, June 30, 2013
Instructions for installing PyDev
Here are instructions for installing PyDev into the Eclipse IDE. This a link to a detailed set of instructions that include the installation of the Eclipse IDE, followed by the installation of the PyDev plug-in. This is a 35 page document. The Chrome web browser will open it, eventually. Mozilla might have trouble opening it (my copy of Mozilla proclaimed the file was damaged, but it was not). Microsoft Internet Explorer will eventually open the PDF file. It takes time because the file is large.
PyDev was developed by a company called Aptana. They have an IDE called Aptana Studio 3 that comes with Python loaded in it. This is another version of the Eclipse IDE. It will work just like Eclipse, if you are familiar with it. You do not need to add the PyDev perspective in Aptana Studio 3. The Web perspective will run Python code just fine. You do need to setup the Python interpreter. Refer to the instructions for installing Pydev to see how that is done. See the section, Configuring the IDE, pages 14 to 18.
IDE's are helpful for debugging code. It is great that you can use Eclipse with Python.
Robert
PyDev was developed by a company called Aptana. They have an IDE called Aptana Studio 3 that comes with Python loaded in it. This is another version of the Eclipse IDE. It will work just like Eclipse, if you are familiar with it. You do not need to add the PyDev perspective in Aptana Studio 3. The Web perspective will run Python code just fine. You do need to setup the Python interpreter. Refer to the instructions for installing Pydev to see how that is done. See the section, Configuring the IDE, pages 14 to 18.
IDE's are helpful for debugging code. It is great that you can use Eclipse with Python.
Robert
Subscribe to:
Posts (Atom)