I have no idea who came with this statistic, but I can confirm that it is true !
The example below doesn’t want to explain how to search for strings nor how to use TDD, but to demonstrate that a bit of planning in advance can really speed things up!
I was in need of a way to search in a pretty complex data structure for a complicated string (letters, numbers, symbols), but not by doing an exact search. Instead I was looking like a search similar to how humans apply a “looks like” algorithm.
So, I was trying to implement some heuristics.
Having TDD (Test Driven Development) in mind, I created a test program and I was planning to enhance the algorithm while I was feeding it with values.
So, I immediately identified a few patterns that were rather trivial to implement and without thinking too much, I actually started to implement them.
How useless !
After more than 30 min of implementing, I started to scratch my head because I was identifying more and more cases that didn’t match those patterns. Needless to say, my TDD-like attempt failed miserably! (Yes, I know, TDD, is more complex than this)
Pretty nervous, I closed the computer and took a piece of paper.
I started to write down what I want to achieve in the first place:
Search for a string in the data structure by identifying strings that “look like” the string I need.
Then I identified the actors:
STRING DATA STRUCTURE(DS)
Below each of them I wrote examples of possible values.
The problem was that in some cases, the given STRING was much bigger than the strings in the DS, even if they were looking alike. They contained too much additional data (context) that I didn’t need. My first approach (when doing TDD) was to remove the extra data, but this proved to be too complex.
After less than 10 minutes of analyzing the possible input data, it struck me:
Instead of trying to reduce the long STRING so that I always can compare it with the values stored in the DS, why not comparing the values in the DS with the STRING?
Well, this covers… about 50% of my values. Fortunately, the first method, compare STRING with each element in DS, covers the rest 50%.
So, I wrote 1/2 line of code and I solved the problem in 30 seconds.
Compare this with more than 30 min of writing code.
Plan in advance, do not assume, even if you are sure about something.
Even the simplest things can be solved sometimes easier if you just looks at them from another perspective.
© Copyright 2015 Sorin Mustaca, All rights Reserved. Written For: Sorin Mustaca on Cybersecurity
Check www.mustaca.com for the IT Consulting services I offer.
Visit www.itsecuritynews.info for latest security news in English
Besuchen Sie http://de.itsecuritynews.info für IT Sicherheits News auf Deutsch