Better Practice, Dec. 2018

(singke) #1
48 \ December 2018 \ http://www.phparch.com

finally{}


The Seven Deadly Sins of


Programming: Greed


Eli White


As you are reading this magazine, we are in a time of winter
holidays and typically associated with a spirit of goodwill
towards others. That is embodied often as giving presents to
people as a gesture of that goodwill. Unfortunately, this can
have the opposite effect at times of causing greed to form as
people want more and more given to them. Alternatively, they
may misinterpret it as greed, as a specific green haired character
learns in his holiday tale.

However, how does greed apply to the art of program-
ming? How can a programmer be greedy? Undoubtedly greed
can take many forms.
For one, there is the most obvious: overuse of computer
resources. Just take a look at the processing power needed
to run a modern operating system versus those of a decade
ago. Sure, much of that is truly necessary to provide us with
the multiprocessing power that we now need for today’s lives
and workstyle. However, much of it is spent just adding neat
“swoosh’”effects when you close a window. A perfect exam-
ple: as I type this article in my text editor, it is continually
bouncing up word suggestions for me in a dropdown, trying
to anticipate what I’m going to type next.
(Pardon me while I disable now that, I just realized how
annoying it is.)


“The most amazing achievement of the computer software
industry is its continuing cancellation of the steady and
staggering gains made by the computer hardware indus-
t r y .”

–Henry Petroski

As programmers, we are often drawn by the shiny and want
to add new features to a project we are doing. I fear we rarely
think about the impact that this has on the performance of
the system itself. For PHP developers this can often lead to
code that is using far more memory than it should since we
store entire XML documents in memory to pull one byte of
data out, but then don’t bother destroying it before continu-
ing our program. We write extremely inefficient looping code,
just because we’ve not had to really worry about the perfor-
mance since powerful machines are relatively inexpensive

now. Closer to home, PHP 7 nearly doubled CPU perfor-
mance while drastically reducing memory usage and further
spoiling us.
The worst part can be on the JavaScript side of the house,
especially for us, who often do JavaScript as a second language,
and who won’t realize the performance impact tight loops or
scoping-memory-leaks can have on the browser. (I have some
horror stories I could share there.)
There is also an aspect of greed that highly relates to glut-
tony, which involves greedily wanting to ‘do it all’ and ‘collect
it all.’ Leaving you installing tons of libraries to create all those
features. Causing your codebase to bloat just as your memory
and CPU usage do as well.
Pure joy can exist, and benefits abound, in a cleaner
codebase designed to do exactly what it needs to do, at that
moment. There are benefits in finding easy optimizations in
your code — a way to combine a few loops, a way to skip
the loop altogether, or just a way to minimize what happens
inside. Moreover, be cognizant of your memory usage.
PHP is a fantastic programming language. It’s flexible,
and it makes it super easy to write code and get a task done.
However, it does come with some costs because of that. If you
want to write non-greedy code, you need to put a little extra
effort into it to make sure that it’s playing nice with the system.
Very happy holidays to everyone! May your code compile
correctly every time, and run with the smallest footprint
possible.

Eli White is a Conference Chair for
php[architect] and Vice President of One for
All Events, LLC. The day he found the
non-greedy modifier in regex was a complete
game changer for him. @EliW
Free download pdf