Re: Fortran refactoring



phillip.news@xxxxxxxxx <phillip.news@xxxxxxxxx> wrote:
[much elided]

One of the key benefits of contained subroutines is that they're scoped
so that they can see all of the variables available in the containing
program. If you keep all of the variable declarations at the
containing routine level, the variables are automatically shared
between all contained subroutines.

One caveat. This works fine *IF* all the variables are explcitly
declared. Of course, that is a good idea (and the more so for larger
codes), but you didsay you were talking about old and messy (not your
words) code, where I can imagine that this won't necesssarily be the
case.

If a variables is not explicitly declared and you move all of the code
that refers to that variable out of the host, then the scope of the
variable will also move out of the host. If two of your contained
subroutines refer to it, they will now be referring to two separate
variables, which will likely be a bad thing. Even if all the code
referring to the variable moves into a single contained procedure, the
new scope might cause the variable to loose its value between calls to
the contained procedure.

All of which suggests that possibly a prior step should be to add
"implicit none" and fix up any problems that shows... which isn't a bad
idea anyway.

--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
.



Relevant Pages

  • Re: c99/c++ localised variable definition
    ... When variables are used only in a limited scope I find it better to ... Forcing variable declarations to the top of the function means extra ... > reason why things like ancillary counter variables seem reasonable, ... you can combine the decl and the initialization. ...
    (freebsd-arch)
  • Re: Making C better (by borrowing from C++)
    ... there's no way to be certain unless declarations are restricted not ... merely to the top of the scope, but to the top of the function. ... line by line for the matching declaration; the context switch between ... the two search modes costs at least as much as the nominally more ...
    (comp.lang.c)
  • Re: Identify new pairs in an array
    ... Leaving us to figure it out for ourselves from the code? ... [declarations below rearranged into groups I can comment on] ... One-character variable names suck even more when they ... You should limit the scope of variables to the smallest possible scope. ...
    (comp.lang.perl.misc)
  • Re: About String
    ... My idea is that every statement list is a scope. ... What is so special in statement lists? ... and to me declarations intermixed with statements look similarly ... You declare something inside a loop body, ...
    (comp.lang.ada)
  • Re: Java needs "goto" (was Re: hi)
    ... declarations are collected at the start of the method, ... the whole thing and starting from scratch. ... Too much work to be done and too lil time to do it. ... Played with this scope thing in just about ...
    (comp.lang.java.programmer)