Monday, September 2, 2013

Yes please, pass the smart data structure

“Smart data structures and dumb code works a lot better than the other way around” -- http://stackoverflow.com/questions/2953867/example-of-eric-raymonds-smart-data-structures-principle



“If the data structures are quality tools, they will make the rest of the code easier and cheaper to write. By having high quality data structures, you reduce the effort required for the rest of the code.” -- http://stackoverflow.com/questions/2953867/example-of-eric-raymonds-smart-data-structures-principle



“Show me your code and conceal your data structures, and I shall continue to be mystified. Show me your data structures, and I won’t usually need your code; it’ll be obvious.” -- http://blog.mongolab.com/2012/08/why-is-mongodb-wildly-popular/



“Bad programmers worry about the code. Good programmers worry about data structures and their relationships.” -- http://programmers.stackexchange.com/questions/163185/torvalds-quote-about-good-programmer



Don’t fell in love with code so much. Love smart and intuitive data structure instead, achieving that, the code almost writes itself. The smarter the data structure one have, the lesser the code one needs to write.



For example, if you have the following bar chart:



If you are passing the ordinal (this.x) of the clicked bar chart to the service, i.e. Goals = 0, Self-Assessment = 1, Review = 2, etc., your code need to interpret on the service layer what was clicked by the user. And what's more annoying is if there are some gaps in the chart, say the chart can be customized, the Self-Assessment is not present on the chart, the rest of the bar charts need to have their ordinal be adjusted too. Goals = 0, Review = 1, Objective = 2, etc. So on the service layer, the code should not blindly interpret that 1 is equal to Self-Assessment, as 1 is now a Review.


If the data being passed to service is not smart, the code on the service need to re-interpret the meaning of the data being passed to it, the code on the service need to be very smart, this makes the code very brittle when there are regular changes on the app.


We should make the data smarter, it would result to lesser code, small and straightforward code is easier to read and debug. One way to make the value being passed from bar chart to service smarter is to generate a corresponding array when the bar charts are generated. So instead of passing the ordinal...


events: {
    click : function() {
                  
        
        // pass the ordinal value this.x to service layer via ajax
        
    }
}


...do this instead:
var sections = ['G','S','R','O','F']; // dynamically-generated

.
.
.

events: {
    click : function() {
                  
   
        var section = sections[this.x]; // use a smarter data structure, use something that has a semantics upfront

        // pass the section value to service layer via ajax
        
    }
}


Accomplishing that, the code on the service layer won't have a need to re-interpret the meaning of the value being passed to it. Call the usage of smarter data structure a domain-driven development if you will, a dumb data structure such as ordinal doesn't carry any semantics, the quality of the code will suffer consequently.





Happy Computing! ツ

No comments:

Post a Comment