#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2014
    Posts
    5
    Rep Power
    0

    Question Which methods to override? (Perl with Moose)


    From Object moved

    Let the following diagram describes which methods (say of a database object-relational mapping class or something similar) call which:



    Update() calls Save('update') and Insert() calls Save('insert'). Save() calls either _Update() or _Insert() dependently on the argument.

    _Update() and _Insert() do the actual work to modify data in the database.

    This structure is made in order that I could easily override methods with Moose method modifiers.

    For example, I can override (e.g. with Moose around method modifier) the method Save() to add features for both Update() and Insert(), without repeating code in both Update() and Insert().

    Now how to add code to both Update() and Insert() is clear (see the previous paragraph).

    But what if I want to modify (in a subclass) the behavior of just one of Update() (or respectively Insert()), should I override Update() or _Update()? (or respectively Insert() or _Insert()?)
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2014
    Posts
    5
    Rep Power
    0
    I think I should override _Update() and/or _Insert(), because this way I get correct Save() "for free"
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    168
    Rep Power
    22
    It's not just about which method you can modify ( update() or _update() ) (or respectively insert() or _insert()); it's more about good practice. While it's technically valid to modify _update(), it's not a good practice. Conventionally, the attributes that start with a single underscore are an indication to other programmers that the attribute is intended for private use. In this case, it's for use by the save() method.

    Just to show an example, if you look at the python language, you would not be able to import an attribute that starts with _
    Code:
    from module import *
    Here, all attributes are imported except the ones that start with _ ; since it's intended for internal private use inside the module.

IMN logo majestic logo threadwatch logo seochat tools logo