phparchitect-2019-08

(Rick Simeone) #1
28 \ August 2019 \ http://www.phparch.com

Education Station
Writing DRY, SOLID FOSS OOP CRUD Code

with an interface or a combination of an interface and a base
class?
We do not know until we start to write the code. We can
spend hours planning out what we think we need, but until
we put the code into practice, we can’t be sure. We need
working code to validate our assumption and see what we
need to do with our code.
This sitaution is where something like Test-Driven Design
can shine, as we can use it to play with the code and see how
our architecture requirements evolve. Maybe the $faqs prop-
erty should be completely separate service instead of being an
object property. We cannot know until we have a few copies
of this code sitting around to make that call.
As a design principle, DRY, a more widespread practice,
vastly overshadows WET.

DRY
DRY is the idea of Don’t Repeat Yourself, and is commonly
brought up when talking about object-oriented program-
ming. At first glance, this idea seems to run directly opposed
to WET, and DRY itself makes sense. We’re all familiar with
code copy and pasted in multiple files. The more copies of
functionality we have in a codebase, the harder it is to main-
tain.
The basis of DRY is straightforward. When faced with
multiple blocks of code doing the same thing, encapsulate
them so they are in a single place, and call that new encap-
sulation. It could be its independent function or even a class.
Doing so moves the code into a single place, making it easier
to maintain. It also ensures the functionality is the same
across the board whenever we call this code.
Many programs have to read in config files. It is safe to say
that most developers would consider writing the boilerplate
to open a file is tedious. If we have to repeat this, the following
block of code would stick out like a sore thumb, as you can
see in Listing 1.
Ignoring that we could simplify this with fileget
contents(), each of these file reads is almost the same. We
could easily refactor this into a single loadConfig() method
(see Listing 2).


By converting the file reads into a single function, we
ensure any additional config files we need to load, are read
and handled in the same way. This code is much easier to
maintain than copy-and-pasting the file read code over and
over for every new file we need to read.
Notice both WET and DRY were used here to help refactor
the code. The first version of the code had two sets of the
same functionality, and we used WET to help determine what
and how to refactor. DRY is used as an overarching idea that
we should not repeat ourselves any more than we have to.

KISS
KISS stands for Keep It Simple, Stupid, or as one of my
programming professors liked to call it: _Keep it Simple,
Stanley. KISS tells us we should always strive to keep our
code as simple as possible, and we should avoid unnecessary
complexity.
If we look back at our DRY code example, one of the refac-
tors I did was to turn the fopen(), fgets(), fclose() logic
into a single file_get_contents() call. While they functionally
do the same thing, a single file_get_contents() call is much
simpler to implement and much easier to read. We simplified
the code in a way that made it easier for everyone.
KISS goes beyond just figuring out what shortcuts to use in
a language; it also goes into the design and architecture of the
code. Just as micro-optimizations can be the root of all evil, so

Listing 2


  1. function loadConfig(array $config, str $file) {

  2. $data = file_get_contents($file);

  3. $json = json_decode($data, true);

  4. return array_merge_recursive($config, $json);

  5. }



  6. $config = [];

  7. $config = loadConfig($config, 'global.json');

  8. $config = loadConfig($config, 'local.json');


Listing 1


  1. <?php

  2. $config = [];

  3. $fh = fopen('global.json', 'r');

  4. $global = '';



  5. while (!feof($fh)) {

  6. $line = fgets($fh);

  7. $global .= $line;

  8. }



  9. fclose($fh);

  10. $config = array_merge_recursive($config,
    13
    . json_decode($global, true));



  11. $fh = fopen('local.json', 'r');

  12. $local = '';



  13. while (!feof($fh)) {

  14. $line = fgets($fh);

  15. $local .= $line;

  16. }



  17. fclose($fh);

  18. $config = array_merge_recursive($config,

  19. json_decode($local, true));

Free download pdf