1. #1
    Ext JS Premium Member
    Join Date
    May 2010
    Posts
    39
    Vote Rating
    0
    caseyb is on a distinguished road

      0  

    Default Best Practice Question - When to create a new class vs an instance?

    Best Practice Question - When to create a new class vs an instance?


    This is as much an object-oriented programming question as it is an extJS question:

    We are re-writing a LARGE application here at work. It was originally written in a tag-based scripting language and we are re-writing it with an object-oriented/ext approach. It's a huge project and I'm pretty new to true object-oriented programming. Our question, to be very general, is this: When is it a good idea to extend an ext class into a new class as opposed to just creating an instance with the provided overrides? Or, to phrase it another way, when is an instance of an object too specific to be extended into a subclass?

    I'm not sure I'm phrasing the general question clearly, so I'll ask in a more specific way too:

    I am writing a very small module that uses a tree to display a parts breakdown report. For this module, I am using an EXT TreeLoader object. I am overriding the createNode() method to display special icons for each tree node. My plan was to create a class file with and to extend TreeNode into a special subclass used specifically for this module. It would look something like this:

    Code:
    Ext.ns('Ext.ux.ConfigCompView');
    
    Ext.ux.ConfigCompView.ConfigCompTreeLoader = Ext.extend(Ext.tree.TreeLoader, {
        dataUrl: 'config_data.js',
        requestMethod: 'get',
        
        //Override createNode to set asm and comp icons
        createNode : function(attr){
            
            if (attr.component) {
                attr.iconCls = 'component';
            }
            else if (attr.assembly) {
                attr.iconCls = 'assembly'
            }
            
            return Ext.tree.TreeLoader.prototype.createNode.call(this, attr);
        }
        
    });
    I will also create separate class files for the layout I end up using and for any other components I use.

    Other programmers in the group contend that this class is far too specific and will likely never be re-used anywhere else in our application. Therefore, I should have just created an instance of the TreeLoader object with an override for createNode provided in the constructor call. Their reasoning is that creating a new class file and including it in the module's main page adds a level of complexity and ambiguity to the code for no added benefit. It's true that this class is very specific since the iconCls property is being defined and the dataUrl is hard-coded.

    So when is it a good idea to break the code up into class files and when is it a bad idea? I was under the impression that it is ALWAYS better to break your code up in this way, but I understand the point of my fellow programmers and I want to make sure we are all on the same page before we continue programming.

    Thanks in advance for any help you can provide. As I said, we're all fairly new to object-oriented programming (in the sense that we have never written a large application in this way) and we would love any best practice advice the community can provide.

  2. #2
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,505
    Vote Rating
    53
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    It's a good question.

    I agree with your colleague that the test should be re-useability. That is re-useability multiple times within the lifetime of the HTML document.

    If there is only one "new" call, then there is no point creating a new class containing the specialized methods, and then instantiating that class.

    You might as well just inject the override implementations in through configuration.

  3. #3
    Sencha User laurentParis's Avatar
    Join Date
    Aug 2010
    Location
    paris
    Posts
    246
    Vote Rating
    2
    laurentParis is on a distinguished road

      0  

    Default


    I suggest to never modify Extjs comportment with override methode. I use override method only for patch ExtJS.

    when you need extend ExtJs class, and you are sure that this one will be re-used, extend One, on other case, instantiate it and refined method that you need

    in your case, with your class too specific, I propose to transform it like this
    PHP Code:
    var ConfigCompTreeLoader = new Ext.tree.TreeLoader({
        
    dataUrl'config_data.js',
        
    requestMethod'get',
        
        
    createNode : function(attr){
            if (
    attr.component) {
                
    attr.iconCls 'component';
            }
            else if (
    attr.assembly) {
                
    attr.iconCls 'assembly'
            
    }
            
            return 
    this.constructor.prototype.createNode.call(thisattr);
        }
        
    }); 

  4. #4
    Sencha User darthwes's Avatar
    Join Date
    Mar 2010
    Posts
    633
    Vote Rating
    -11
    darthwes can only hope to improve

      0  

    Default


    I'll argue that extending a class is preferable for more than just reuse. It helps to have the scope simplified and helps developers keep their thinking abstract. Furthermore, I find it hard to believe that an additional include would be too costly: we minify all our class definitions into one js file anyway, so you're really not saving yourself anything by putting the overrides in the new config option.

    You will end up with an include that has a nice scope that the devlopers can understand across all ext extend calls and even if you're not going to re-use the component your not incurring overhead that you can escape, you have to declare the overrides somewhere.

    Furthermore, you might find that certain function overrides DO overlap, and then having simple extensions that you can chain together to create new components is definately valuable.

    Bottom line is that you're not saving more than a few (50?) ms and understanding an ext extend call is all that is required to read the code.
    Wes

  5. #5
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,505
    Vote Rating
    53
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    Scope is imposed by the caller, so any injected method will be called with the same scope as the original method would have been called with.

    I just keep seeing apps where the developers are just all too keen to create webs of classes all coupled way more than classes should be, when at base, all they want is one specialized Component.

  6. #6
    Ext JS Premium Member
    Join Date
    May 2010
    Posts
    39
    Vote Rating
    0
    caseyb is on a distinguished road

      0  

    Default


    Hey guys -

    Thanks for everyone's input.
    Animal - Using one "new" call seems like a good general rule. However, what worries me is that I don't really know what lies ahead in the future. I'm having trouble deciding at what point I should be comfortable saying, "Ok, there's no way I'm ever going to use this object again because it's just TOO specific."

    darthwes - I think you are making the same points that I've been making here at work. I actually think breaking it up into class files can make the code EASIER to read and debug. But I don't want to ignore the points of my fellow programmers because I think it is valid for them to argue that it adds some complexity.

    I was expecting varied opinions on this and I appreciate everyone's response. Thanks guys! Hopefully some more will chime in and my group can make a decision going forward.

Similar Threads

  1. Create a singleton, or retrieve an instance ?
    By gu1gui in forum Ext GWT: Discussion
    Replies: 1
    Last Post: 8 Mar 2010, 1:10 AM
  2. Replies: 6
    Last Post: 11 Jun 2009, 9:28 PM
  3. Window Class doesn't provide access to draggable instance
    By dardison in forum Ext GWT: Help & Discussion (1.x)
    Replies: 1
    Last Post: 22 Apr 2008, 10:31 PM
  4. possibly a stupid question: create an "instance" of a tab/panel/window ?
    By PromaneX in forum Ext 2.x: Help & Discussion
    Replies: 3
    Last Post: 28 Mar 2008, 9:37 AM

Thread Participants: 3