newbie structure question

T

ToddAndMargo

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
 
M

Mirco Wahab

Thus spoke (e-mail address removed) (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
 
T

ToddAndMargo

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
 
P

Peter J. Holzer

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
 
P

Peter J. Holzer

Peter J. Holzer said:
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
 
T

Tad McClellan

Peter J. Holzer said:
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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,264
Messages
2,571,065
Members
48,770
Latest member
ElysaD

Latest Threads

Top