Home > ExtJs, Struggling against boilerplate code > ExtJs. Struggling against boilerplate code.

ExtJs. Struggling against boilerplate code.

  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, {
   constructor:function(config){
         config = config||{};
         var defaultConfig = {width:100, height:200, autoScroll:false};
         Ext.applyIf(config, defaultConfig);
         MyCompany.MySuperPanel.superclass.constructor.call(this, config);
}});
  

And with “merge” idiom:

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

Applying this idiom results in twice less code or even in one line code.

 MyCompany.MySuperPanel.superclass.constructor.call(this,
    Config.merge(config, {width:100, height:200, autoScroll:false}));
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: