2

I have a significant amount of legacy code to deal with (Fortran F90). One of the most difficult things to deal with is that each file contains a small number of massive functions, and each function has a long list of

use <module name>

The code then uses variables and data structures defined in these modules. The issue is its incredibly difficult to determine where these variables are initially defined/initialized because they're defined in one of the use modules and initialized somewhere else in the code (which is about 100 000 LOC).

Is there any way to provide namespace resolution in Fotran? From reading around, I'd guess not, so more generally, is there a good Fortran-Y way to get around this kind of an issue?

Alex
  • 2,000
  • 4
  • 23
  • 41

2 Answers2

6

You can pick and choose what is imported from a use statement, like this:

use foo, only : bar, baz

In this case, only the bar and baz variables would be imported. This also provides a way to self-document your code, which seems like what you are looking for.

SethMMorton
  • 45,752
  • 12
  • 65
  • 86
5

To extend, slightly, what @SethMMorton has already told you, you can also rename imported items

use foo, only : local_name=>bar, baz=>foo_baz

Unfortunately Fortran doesn't provide much in the way of language-directed discipline for doing what other languages do with namespaces. The renaming of variables by prefixing the name of the module in which they are declared would be entirely a programmer's decision and responsibility.

One can, of course, use standards- or management-enforced discipline but compilers are rarely able to help much with either of those.

Or one can console oneself with the thought that exporting many names from modules is probably a breach of good software engineering practices (encapsulation, information-hiding, what-have-you) and that the problems you are dealing with are those made by an earlier, less-disciplined generation of developers.

High Performance Mark
  • 77,191
  • 7
  • 105
  • 161