-
6 votes
-
Magnus Carlsen vs Fabiano Caruana, world chess championship match - game one ends in a draw
11 votes -
Strange snafu misroutes domestic US Internet traffic through China Telecom
11 votes -
Exiled: The disturbing story of a citizen made unBritish
7 votes -
Counting down the days in God's waiting room: An 82-year-old writer spends his final years in a retirement home surrounded by the sick and the sorry – and finds it hard to hold back the tears.
6 votes -
Ball sits on edge of cup for thirty-five seconds... and goes in
8 votes -
"As you wish..." Willam Goldman passed away at 87
5 votes -
Meta Discussion: Is there interest in topics concerning code quality?
I've posted a few lengthy topics here outside of programming challenges, and I've noticed that the ones that seem to have spurred the most interest and generated some discussion were ones that...
I've posted a few lengthy topics here outside of programming challenges, and I've noticed that the ones that seem to have spurred the most interest and generated some discussion were ones that were directly related to code quality. To avoid falling for confirmation bias, though, I thought I would ask directly.
Is there generally a greater interest in code quality discussions? If so, then what kind of things are you interested in seeing in those discussions? What do you prefer not to see? If not, then what kinds of programming-related discussions would you prefer to see more of? What about non-programming discussions?
Also, is there any interest in an informal series of topics much like the programming challenges or the a layperson's introduction to... series (i.e. decentralized and available for anyone to participate whenever)? Personally, I'd be interested in seeing more on the subject from others!
17 votes -
Jehanne: Simplicity awakes
5 votes -
Rosie the CNC machinist: American manufacturing as a warfare domain
8 votes -
I found the best burger place in America. And then I killed it.
20 votes -
The gifts we want to give in 2018
5 votes -
How the world’s worst movie could change copyright
8 votes -
Three feet from God: An oral history of Nirvana ‘Unplugged’
5 votes -
A startup company says it will give people free genome reports if they’re willing to answer detailed questions about their health, drinking habits, and more
5 votes -
Circle of Dust - Waste of Time (1998)
3 votes -
A Sashiko embroidery technique tutorial
5 votes -
Poverty in UK causing misery
4 votes -
Who’s behind that beard? Historians are using facial recognition software to identify people in Civil War photographs
8 votes -
11,000 votes may be missing in Florida Congressional race
13 votes -
Unsecured database of millions of SMS text messages exposed password resets and two-factor codes
19 votes -
DeepMind’s move to transfer health unit to Google stirs data fears
11 votes -
The invisible experiences of first-time Gen-X mothers
4 votes -
When it comes to rape, just because a case is cleared doesn't mean it's solved
7 votes -
A young man's ego doomed a much-hyped Jurassic Park game
6 votes -
Lost Disney 'Oswald' film found in Japan
6 votes -
Lambda World 2018 - What FP can learn from Smalltalk by Aditya Siram
6 votes -
Julian Assange has been charged under seal, US prosecutors reveal inadvertently in court filing
27 votes -
A RegEx to match "A B C" where A+B=C
12 votes -
Medium is a poor choice for blogging
42 votes -
Smallest known raptor tracks suggest microraptorine activity in lakeshore setting (South Korea)
3 votes -
EA is partnering with many of the original Westwood developers to work on the Command & Conquer Remastered Collection
20 votes -
Holy molé: A beginner's guide to Mexico’s heaven-sent sauce
10 votes -
SpeedReader: Fast and Private Reader Mode for the Web
8 votes -
Rediscovering Ancient Greek music
7 votes -
When a blogger died from silicone genital injections, his fans blamed his partner
9 votes -
Becoming Anne Frank - Why did we turn an isolated teenage girl into the world’s most famous Holocaust victim?
7 votes -
Delay, deny and deflect: How Facebook’s leaders fought through crisis
16 votes -
What have you been watching/reading this week? (Anime/Manga)
Sorry for the late post. Anyway, what have you been watching/reading this week? Feel free to talk about something you saw that was cool, something that was bad, ask for recommendations, or...
Sorry for the late post.
Anyway, what have you been watching/reading this week?
Feel free to talk about something you saw that was cool, something that was bad, ask for recommendations, or anything else you can think of.
If you want to, feel free to find the thing you're talking about and link to its Anilist, MAL, or any other anime/manga database you use!
5 votes -
Mondelez cuts ties with twelve palm oil suppliers, citing deforestation
14 votes -
What WhatsApp’s upcoming monetisation means for the company and its 1.5 billion users
16 votes -
Sony Interactive Entertainment is not attending E3 in 2019
9 votes -
Code Quality Tip: Wrapping external libraries.
Preface Occasionally I feel the need to touch on the subject of code quality, particularly because of the importance of its impact on technical debt, especially as I continue to encounter the...
Preface
Occasionally I feel the need to touch on the subject of code quality, particularly because of the importance of its impact on technical debt, especially as I continue to encounter the effects of technical debt in my own work and do my best to manage it. It's a subject that is unfortunately not emphasized nearly enough in academia.
Background
As a refresher, technical debt is the long-term cost of the design decisions in your code. These costs can manifest in different ways, such as greater difficulty in understanding what your code is doing or making non-breaking changes to it. More generally, these costs manifest as additional time and resources being spent to make some kind of change.
Sometimes these costs aren't things you think to consider. One such consideration is how difficult it might be to upgrade a specific technology in your stack. For example, what if you've built a back-end system that integrates with AWS and you suddenly need to upgrade your SDK? In a small project this might be easy, but what if you've built a system that you've been maintaining for years and it relies heavily on AWS integrations? If the method names, namespaces, argument orders, or anything else has changed between versions, then suddenly you'll need to update every single reference to an AWS-related tool in your code to reflect those changes. In larger software projects, this could be a daunting and incredibly expensive task, spanning potentially weeks or even months of work and testing.
That is, unless you keep those references to a minimum.
A Toy Example
This is where "wrapping" your external libraries comes into play. The concept of "wrapping" basically means to create some other function or object that takes care of operating the functions or object methods that you really want to target. One example might look like this:
<?php class ImportedClass { public function methodThatMightBecomeModified($arg1, $arg2) { // Do something. } } class ImportedClassWrapper { private $class_instance = null; private function getInstance() { if(is_null($this->class_instance)) { $this->class_instance = new ImportedClass(); } return $this->class_instance; } public function wrappedMethod($arg1, $arg2) { return $this->getInstance()->methodThatMightBecomeModified($arg1, $arg2); } } ?>
Updating Tools Doesn't Have to Suck
Imagine that our
ImportedClass
has some important new features that we need to make use of that are only available in the most recent version, and we're several versions behind. The problem, of course, is that there were a lot of changes that ended up being made between our current version and the new version. For example,ImportedClass
is now calledNewImportedClass
. On top of that,methodThatMightBecomeModified
is now calledmethodThatWasModified
, and the argument order ended up getting switched around!Now imagine that we were directly calling
new ImportedClass()
in many different places in our code, as well as directly invokingmethodThatMightBecomeModified
:<?php $imported_class_instance = new ImportedClass(); $imported_class_instance->methodThatMightBeModified($val1, $val2); ?>
For every single instance in our code, we need to perform a replacement. There is a linear or--in terms of Big-O notation--a complexity of
O(n)
to make these replacements. If we assume that we only ever used this one method, and we used it 100 times, then there are 100 instances ofnew ImportClass()
to update and another 100 instances of the method invocation, equaling 200 lines of code to change. Furthermore, we need to remember each of the replacements that need to be made and carefully avoid making any errors in the process. This is clearly non-ideal.Now imagine that we chose instead to use the wrapper object:
<?php $imported_class_wrapper = new ImportedClassWrapper(); $imported_class_wrapper->wrappedMethod($val1, $val2); ?>
Our updates are now limited only to the wrapper class:
<?php class ImportedClassWrapper { private $class_instance = null; private function getInstance() { if(is_null($this->class_instance)) { $this->class_instance = new NewImportedClass(); } return $this->class_instance; } public function wrappedMethod($arg1, $arg2) { return $this->getInstance()->methodThatWasModified($arg2, $arg1); } } ?>
Rather than making changes to 200 lines of code, we've now made changes to only 2. What was once an
O(n)
complexity change has now turned into anO(1)
complexity change to make this upgrade. Not bad for a few extra lines of code!
A Practical Example
Toy problems are all well and good, but how does this translate to reality?
Well, I ran into such a problem myself once. Running MongoDB with PHP requires the use of an external driver, and this driver provides an object representing a MongoDB ObjectId. I needed to perform a migration from one hosting provider over to a new cloud hosting provider, with the application and database services, which were originally hosted on the same physical machine, hosted on separate servers. For security reasons, this required an upgrade to a newer version of MongoDB, which in turn required an upgrade to a newer version of the driver.
This upgrade resulted in many of the calls to
new MongoId()
failing, because the old version of the driver would accept empty strings and other invalid ID strings and default to generating a new ObjectId, whereas the new version of the driver treated invalid ID strings as failing errors. And there were many, many cases where invalid strings were being passed into the constructor.Even after spending hours replacing the (literally) several dozen instances of the constructor calls, there were still some places in the code where invalid strings managed to get passed in. This made for a very costly upgrade.
The bugs were easy to fix after the initial replacements, though. After wrapping
new MongoId()
inside of a wrapper function, a few additional conditional statements inside of the new function resolved the bugs without having to dig around the rest of the code base.
Final Thoughts
This is one of those lessons that you don't fully appreciate until you've experienced the technical debt of an unwrapped external library first-hand. Code quality is an active effort, but a worthwhile one. It requires you to be willing to throw away potentially hours or even days of work when you realize that something needs to change, because you're thinking about how to keep yourself from banging your head against a wall later down the line instead of thinking only about how to finish up your current task.
"Work smarter, not harder" means putting in some hard work upfront to keep your technical debt under control.
That's all for now, and remember: don't be fools, wrap your external tools.
23 votes -
Why be nonbinary?
15 votes -
Japan cybersecurity minister admits he has never used a computer
25 votes -
IRA troll factory employee sent by Russia to oversee US midterms appears to violate election laws
8 votes -
Denmark withholds aid to Tanzania after anti-gay comments
6 votes -
FCPX Gets Another Big Update
3 votes -
The Magic Player's Guide to Artifact
6 votes -
Iceland supermarket chain to let loose animatronic orangutan after Christmas ad ban
9 votes