newbie structure question

Discussion in 'Perl Misc' started by ToddAndMargo@gbis.com, Oct 6, 2006.

  1. Guest

    Hi All,

    A am real new to Perl. My background is Modula2 and Bash scripts.
    Please premit a newbie question:

    1) Under Modula2, best practice (or my practice) was from top
    to bottom
    a) make my declarations (which variable did what, etc.),
    b) write all my modules and functions, and
    c) in the body, place my code.

    Is this also the proper structure under Perl as well? (Think,
    this is your chance to influence me into good habits!)

    Many thanks,
    --T
     
    , Oct 6, 2006
    #1
    1. Advertising

  2. Mirco Wahab Guest

    Thus spoke (on 2006-10-06 01:49):
    > My background is Modula2 and Bash scripts.
    > ...
    > 1) Under Modula2, best practice (or my practice) was from top
    > to bottom
    > a) make my declarations (which variable did what, etc.),
    > b) write all my modules and functions, and
    > c) in the body, place my code.
    > Is this also the proper structure under Perl as well? (Think,
    > this is your chance to influence me into good habits!)


    You could do the same here, of course -
    and express your intentions in the names
    of variables and functions, as you like.

    I don't think there can be a methodology
    of creating software that fits for every
    person and for every kind of program.

    Mostly, I try to design 'front to back',
    means, I'd try to figure out how I'd
    like to have the interfaces to the
    problem solving parts of the program
    and put most effort in designing them.

    But, it may depend ...

    At the bottom line, you won't get around using:

    - /packages/ (like 'objects') for the reusable stuff,
    - /subroutines/ for splitting up the task into independent chunks,
    - /data structures/ like arrays and hash maps for keeping the
    model you work with,
    - /variables/ of course to make things going,

    .... as in most other programming languages. You
    can also map your style to most of the different
    programming paradigms:

    http://en.wikipedia.org/wiki/Programming_paradigm

    without much problems, imho except some ;-)

    What exactly did you do before?

    Regards,

    Mirco
     
    Mirco Wahab, Oct 6, 2006
    #2
    1. Advertising

  3. Guest


    > At the bottom line, you won't get around using:
    >
    > - /packages/ (like 'objects') for the reusable stuff,
    > - /subroutines/ for splitting up the task into independent chunks,
    > - /data structures/ like arrays and hash maps for keeping the
    > model you work with,
    > - /variables/ of course to make things going,
    >
    > ... as in most other programming languages. You
    > can also map your style to most of the different
    > programming paradigms:
    >
    > http://en.wikipedia.org/wiki/Programming_paradigm



    Thank you!

    > What exactly did you do before?


    a) make my declarations (which variable did what, etc.),
    b) write all my modules and functions, and
    c) in the body, place my code.

    -T
     
    , Oct 7, 2006
    #3
  4. On 2006-10-05 23:49, <> wrote:
    > A am real new to Perl. My background is Modula2 and Bash scripts.
    > Please premit a newbie question:
    >
    > 1) Under Modula2, best practice (or my practice) was from top
    > to bottom
    > a) make my declarations (which variable did what, etc.),
    > b) write all my modules and functions, and
    > c) in the body, place my code.
    >
    > Is this also the proper structure under Perl as well?


    I don't think so. In fact I think this is a particularly bad structure
    and one of the reasons why I don't like Pascal-like languages is that
    they force this structure on the programmer.

    I believe (and I know many other perl-programmers agree) that variables
    should always be declared in the smallest possible scope. I.e., don't do
    something like this:

    my $some_variable;

    sub a_subroutine {
    ...
    }

    # many more subroutines - hundreds of lines of code

    for (...) {
    if (...) {
    # $some_variable is only used in this block:
    $some_variable = get_some_value();
    some_code();
    do_something_with($some_variable);
    more_code();
    }
    }

    There are hundreds of lines of code where $some_variable could be
    accessed and changed. When you look at the code you have no idea whether
    you are passing the same value to do_something_with() as you received
    from get_some_value(). $some_variable might have been changed by
    some_code() or any function called by it.

    Instead, write it like this:

    sub a_subroutine {
    ...
    }

    # many more subroutines - hundreds of lines of code

    for (...) {
    if (...) {
    # $some_variable is only used in this block:
    my $some_variable = get_some_value();
    some_code();
    do_something_with($some_variable);
    more_code();
    }
    }

    That way $some_variable is visible only visible inside of the if block.
    Nothing outside can change it unless $some_variable is explicitely
    passed to it (that is do_something_with() could change $some_variable).


    Browsing through Conway's "Perl Best Practice", I was surprised that I
    couldn't find that advice, although he does follow that style. Maybe he
    thought that's so self-evident that he doesn't have to spell it out.

    hp

    --
    _ | Peter J. Holzer | > Wieso sollte man etwas erfinden was nicht
    |_|_) | Sysadmin WSR | > ist?
    | | | | Was sonst wäre der Sinn des Erfindens?
    __/ | http://www.hjp.at/ | -- P. Einstein u. V. Gringmuth in desd
     
    Peter J. Holzer, Oct 7, 2006
    #4
  5. Re: Merits vs costs of Structured Programming Dogma

    On 2006-10-07 12:53, <> wrote:
    > "Peter J. Holzer" <> writes:
    >> On 2006-10-05 23:49, <> wrote:
    >> > 1) Under Modula2, best practice (or my practice) was from top
    >> > to bottom
    >> > a) make my declarations (which variable did what, etc.),
    >> > b) write all my modules and functions, and
    >> > c) in the body, place my code.
    >> >
    >> > Is this also the proper structure under Perl as well?

    >>
    >> I don't think so. In fact I think this is a particularly bad structure
    >> and one of the reasons why I don't like Pascal-like languages is that
    >> they force this structure on the programmer.
    >>
    >> [...snippage...]
    >>
    >> There are hundreds of lines of code where $some_variable could be
    >> accessed and changed. When you look at the code you have no idea whether
    >> you are passing the same value to do_something_with() as you received
    >> from get_some_value(). $some_variable might have been changed by
    >> some_code() or any function called by it.

    >
    > I agree wholeheartedly with Peter's analysis. I've recently had to do
    > a major bit of coding in C after many years of full time Perl work and
    > the "forced structure" grates against me - I never wrote Pascal code
    > because it grated on me even when it was novel and exciting :).


    C is actually quite similar to Perl in that respect: You could always
    declare variables at the start of each block, and you can declare them
    in the middle of a block since C99.


    > However, to play Devil's Advocate for a moment: One of the OTHER
    > dogmas of the Structured Programming Camp is: You shouldn't have any
    > block that is "hundreds of lines of code". If you violate rule (n) of
    > any set of best practices, then rule (n+1) may suddenly not make as
    > much sense as it used to.


    I wasn't thinking of a single block of hundreds of lines, but of many
    small subroutines.

    hp


    --
    _ | Peter J. Holzer | > Wieso sollte man etwas erfinden was nicht
    |_|_) | Sysadmin WSR | > ist?
    | | | | Was sonst wäre der Sinn des Erfindens?
    __/ | http://www.hjp.at/ | -- P. Einstein u. V. Gringmuth in desd
     
    Peter J. Holzer, Oct 7, 2006
    #5
  6. Peter J. Holzer <> wrote:

    > I believe (and I know many other perl-programmers agree) that variables

    ^^^^
    > should always be declared in the smallest possible scope.



    I am quite sure that we needn't include that qualifier.

    Many programmers agree that variables should always be
    declared in the smallest possible scope.

    That is just basic good Software Engineering, nothing specific
    to Perl in it.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 7, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Excluded_Middle

    Pointers to structure and array of structure.

    Excluded_Middle, Oct 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    791
    Martin Ambuhl
    Oct 26, 2004
  2. Leo Nunez
    Replies:
    3
    Views:
    1,292
    Neil Kurzman
    Feb 9, 2005
  3. Replies:
    2
    Views:
    641
  4. Replies:
    9
    Views:
    25,536
    Lal Bahadur Singh
    Nov 11, 2011
  5. meisterbartsch
    Replies:
    2
    Views:
    807
    meisterbartsch
    Jun 12, 2007
Loading...

Share This Page