Preface

Two commonly touted aspects of Python are the readability of its code and how quickly it can be used to creating working applications. Among the complaints regarding C++ are legibility of complex applications and difficulty in debugging the same. The flourishing number of programming languages and flight from C++ to scripting and just-in-time compiler-based languages over the past decade (or two) forced developers to take a deep, hard look at the development process. Why is it that a language that can be so obfuscated as C++ remain able to do what others cannot? I won’t go into the reasons, because that’s a holy war on its own. The end result of the rise of Java, Perl, and other web-development languages that followed, along with the failure of so many languages to topple C++ is a deep retrospective among developers and engineers on the design and development process. Concepts like unit and integration testing, rising and falling popular “styles” of development (e.g. Agile, Waterfall, Extreme Programming), and the examples of both successful and failed open-source development models are changing the way software development happens.

Trains Don’t Turn Easily

Anyone who’s worked with undocumented and badly written legacy code will tell you that it’s a terrible, terrible thing. Poorly designed APIs, non-descriptive variable names, and the fear of regressions in mature code (what might be called the “if it’s worked this long, don’t touch it” effect) hound application developers. Once a library or application reaches a certain point in size or development milestones, fear of breakage spells doom for mistakes made early in the development process. If software were mansions, a warped support beam on the first floor is hard to fix when you’re putting up the third floor. Wooden roller coasters are infamous for this problem: a 2-by-4 off by an 1/8″ at the base can become three feet at the apex.

Once It’s Done, It’s Hard to Undo

The larger the application, the more time needs to be spent in the design phase. Unfortunately, until the code is written, compiled, and tested, some things just aren’t obvious. Unit and integration testing certainly help here, particularly if modular programming is properly adhered to. Nevertheless, compiled languages like C++ make revisiting the design phase after development begins is a painful process. An application written entirely in C++ can be written slipshod to get it working or slowly to do it right, but not both. Writing modules in Python first saves time by making mistakes and pitfalls visible early on and quickly repairable. Because Python is designed to be easily readable (assuming one doesn’t use get too clever with some of the core libraries), it can be used as an intermediate stage between design and implementation to get the algorithms right, tweak the APIs, and generally make sure ideas that are time-intensive to implement will work.

It may seem foolish to write a program twice, but that’s not what I’m advocating at all. Python is such a flexible language that it is possible to write code that closely mirrors C++ and can be reused with a little scripting. In the case of Qt and PyQt (or PySide) this is particularly true, as most of Python and C++’s core libraries can be replaced with their QtCore counterparts.

I will be following this relatively short post with a more extensive discussion on writing legible Python code that readily converts to C++ and how to go about doing so.

Leave a Reply