Archive for the ‘Struggling against boilerplate code’ Category

ExtJs. Struggling against boilerplate code.

March 23, 2010 Leave a comment

  I don’t like any scent of boilerplate code in any programming language, specially in dynamic languages. The dynamic languages are just not designed for it, having boilerplate code out there twice pain as in static languages.
  If you are using well-known “Preconfigured class” idiom, you will find the following idiom very useful. It delegates part of routine to just one method. I name it “config merge” idiom:

Config = {};
Config.merge = function(config, defaultConfig){
        config = config||{};
        defaultConfig =  defaultConfig||{};
        Ext.applyIf(config, defaultConfig);
        return config;

I bet the following routine of incoming config and default config is repeated in your code sometimes or more often:

MyCompany.MySuperPanel = Ext.extend(Ext.Panel, {
         config = config||{};
         var defaultConfig = {width:100, height:200, autoScroll:false};
         Ext.applyIf(config, defaultConfig);, config);

And with “merge” idiom:

 MyCompany.MySuperPanel = Ext.extend(Ext.Panel, {
          Config.merge(config, {width:100, height:200, autoScroll:false});
, config);

Applying this idiom results in twice less code or even in one line code.,
    Config.merge(config, {width:100, height:200, autoScroll:false}));