General Programming Hacks
C++ is not a perfect language. As such sometimes you must program
around the limits imposed on you by the language. In this chapter we present
some of the simple, common hacks which you can use to make your programs
simpler and more readable.
Hack 1: Make Code Disappear
The Problem: Writing code takes time and introduces risk.
The Hack: Don't write code. After all the code that you don't write is the
easiest to produce, debug, and maintain. A zero line program is the only one you
can be sure has no bugs.
A good hacker knows how to write good code. An excellent hacker figures
out how to not write code at all.
When you are faced with a problem, sit down and think about it. Some
large and complex problems, are really just small, simple problems hidden by
confused users and ambitious requirements. Your job is to find the small simple
solution and to not write code to handle the large confusing one.
Let me give you an example: I was asked to write a license manager which
allowed users who had a license key to run the program. There were two types
of licenses, those that expired on a certain date and those that never expired.
Normally someone would design the code with some extra logic to handle
the two types of licenses. I rewrote the requirements and dropped the
requirement for licenses that never expired. Instead we would give our
evaluation customers a license that expired in 60-90 days and give customers
who purchased the program a license that expired in 2038
3
.
Thus our two types of licenses became one. All the code for permanent
license disappeared and was never written.
In another case I had to write a new reporting system for a company. Their
existing system was written in a scripting language that was just to slow and
limited. At the time they had 37 types of reports. With 37 pieces of code to
generate these 37 reports.
C++ Hackers Guide Steve Oualline
My job was to translate these 37 pieces of code from one language to
another. Instead of just doing what I was told, I sat down and studied what was
being done. When my boss asked why I wasn't coding, I told him that I was
thinking, a step I did not consider optional.
It turns out that I was able to distill the 37 different reports into just 3
report types. All 37 reports could be generated using these three types and
some parameters. As a result the amount of code needed to do the work was cut
down by at least a factor of 10.
Remember the code that you never write is the quickest to produces and
the most bug free code you'll ever make. Hacking something out of existence is
one of the highest forms of hacking.
Producing Lines of Code
I was once tasked with updating a large web based
reporting system written in Perl. At this time
management decided to measure lines of code written to
see how productive its programmers were.
Because of the “design” of the Perl syntax, the difference
between bad programmers and good one is amplified.
The first week, I cleaned up the obvious inefficiencies
and removed a lot of redundant and useless code. My
score for that week was about —1,700 lines produced.
So the program got smaller even though I added lots of
comments and a couple of new features.
For next few weeks I continued to reduce the size of the
program. The big change came when I took out the old
style, “call function, check for error, pass error up the
call change” logic and replaced it with exception based
error handling4
. That change lost us 5,000 lines.
My manager asked me why they should be paying me the
big bucks since my #lines produced / week was negative.
I told them that that was precisely why they were paying
me the big bucks. Because it takes a really excellent
programmer to produce new features in negative lines of
code.