Improving bad practice

Discussion in 'Perl Misc' started by Justin C, Feb 20, 2014.

  1. Justin C

    Justin C Guest

    I stumbled across this web-page
    <URL:http://http://perl-begin.org/tutorials/bad-elements>
    and read the following:

    Passing parameters in delegation
    Sometimes we encounter a case where subroutines each pass the
    same parameter to one another in delegation, just because the
    innermost subroutines in the callstack need it.

    To avoid it, create a class, and declare methods that operate
    on the fields of the class, where you can assign the delegated
    arguments.


    I am definitely guilty of this. What I don't understand is the
    explanation of how to avoid it. Can someone point me at some
    simple examples or easy to comprehend documentation?

    Justin.
     
    Justin C, Feb 20, 2014
    #1
    1. Advertisements

  2. Oh, that's easy: To avoid passing arguments, assign them to global
    variables! To avoid getting flak for that, put all variables shared by a
    set of subroutines into a hash (aka 'object instance') and pass that
    around "by implicit delegation" using $self->other_method() instead.

    NB: This piece of ill thought out advice is rather symptomatic for the
    web page in question.
     
    Rainer Weikusat, Feb 20, 2014
    #2
    1. Advertisements

  3. Justin C

    Justin C Guest

    If it's ill thought out then I'll just ignore it!

    Thank you for the reply.


    Justin.
     
    Justin C, Feb 23, 2014
    #3
  4. Justin C

    Justin C Guest

    No problem, there are no deadlines for replies. :)

    I understood it differently. I didn't think it suggested number of
    parameters, but how many subs they/it are/is handed down through.
    And possibly about calling subs from subs within subs within subs...

    I'm still struggling with terminology in OO. I use OO perl all the
    time (at least, I *think* that's what I'm doing! - mostly with CPAN
    modules), but I'm finding it difficult to understand "turning the
    functions into methods on some objects and parameters into
    attributes".

    Working in a more OO way will probably be beneficial to my under-
    standing, I just don't see OO solutions to my everyday problems or
    tasks. So while I might use OO with the modules I'm using, I tend
    to write in a procedural way.

    I'm sure there are instances in my code where I should do this, but
    I don't always see when it would be useful. Right now I'm thinking
    of some of my programs that create/write MS Excel. Module
    SpreadSheet::WriteExcel uses OO methods, but I just iterate over
    my data and pass it along all serialised. I should probably create
    row objects, and cell objects which have content and formatting....
    but there are many other situations, now I think about it, where
    I should create objects out of my data.

    Yup, I definitely need to think this way. Maybe if I finish the
    Alpaca and/or Modern Perl I'll be better placed to work this way.

    [snip]
    I've been reading about MVC in reference to Dancer, Mojolicious, and
    Catalyst. It's one of the concepts I'm trying to get my head around.

    I think I should stop jumping in and writing code, and consider what
    the problem is I'm trying to solve. Then write tools for a generic
    situation rather than just the specific. I think this will get me code
    that is more OO.

    I've got some hideous DB stuff here. The first stuff I wrote, a cron
    job on the DB server extracted data using SQL and emailed it to a
    dummy user where procmail called perl to process the data. The next
    gen of DB code used SSH, and now I've got an RPC server sitting on
    the DB server and handling whatever my main working machine asks for

    Yes, I got to the end. And I've been mulling it over all yesterday,
    and a lot of today! Thank you for the comprehensive reply. It's
    given me plenty to think about, and I need to think about such
    things before I actually start writing code. I think I'm going to
    be thinking about this for most of the rest of today too.


    Justin.
     
    Justin C, Feb 25, 2014
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.