4

I'd like to optimize the readability of my codes in Fortran by using OOP. I thus use derived types. what is the best practice to name the types and derived types?

For example, is it better to:

type a
    real :: var
end type
type(a) :: mya

or always begin type names by type_ like in type_a? I like this one but maybe better ideas can be foud.

Also, is it better (then why) to use short names that are less readable or longer names that end up quite difficult to read if the type has too many "levels". For example, in a%b%c%d%e, if a, b, c, d and e are 8 or more letters long as in country%hospital%service%patient%name, then once again readability seems to be a concern.

Advices from experts are really welcome.

4

2 回答 2

4

This not anything special to Fortran. You can use coding recommendation for other languages. Usually, type names are not designated by any prefix or suffix. In many languages class names start with a capital letter. You can use this in Fortran also, even if it is not case sensitive. Just be sure not to reuse the name with a small letter as a variable name.

One example of a good coding guideline is this and you can adapt it for Fortran very easily. Also, have a look on some Fortran examples in books like MRC or RXX. OS can be also useful.

I would recommend not to use too short component names, if the letter is not the same as used in the written equation. In that case it can be used.

Use the associate construct or pointers to make aliases to nested names like country%hospital%service%patient%name.

于 2013-04-12T10:27:22.090 回答
3

根据我的经验,由于 Fortran 的命名模块、缺少命名空间和不区分大小写等原因,OO Fortran 中的命名问题比其他语言(例如 C++)更多。不区分大小写会造成伤害,因为如果您命名 type Foo,则不能命名变量foo,否则会出现编译器错误(使用 gfortran 4.9 测试)。

我确定的规则是:

  • 每个 Fortran 模块都提供一个名为Namespace_Foo.
  • 该类Namespace_Foo可以作为Namespace/Foo_M.f90.
  • 类变量是具有描述性的小写名称的名词,例如baror bar_baz
  • 类方法是具有描述性(但尽可能简短)名称的动词,并使用 rename search => Namespace_Foo_search
  • 当没有简单的替代方案时,Namespace_Foo可以命名类的实例(没有命名空间)。foo

Namespace::Foo这些规则使得在 Fortran 中镜像 C/C++ 类或将 C++ 类绑定(使用)到 Fortran变得特别容易BIND(C)。它们还避免了我遇到的所有常见名称冲突。

这是一个工作示例(使用 gfortran 4.9 测试)。

module Namespace_Foo_M
implicit none

type :: Namespace_Foo
  integer :: bar
  real    :: bar_baz
  contains
  procedure, pass(this) :: search => Namespace_Foo_search
end type

contains

function Namespace_Foo_search(this, offset) result(index)
  class(Namespace_Foo) :: this 
  integer,intent(in)   :: offset !input
  integer              :: index  !return value
  index = this%bar + int(this%bar_baz) + offset
end function

end module

program main
use Namespace_Foo_M !src/Namespace/Foo_M.f90

type(Namespace_Foo) :: foo

foo % bar = 1
foo % bar_baz = 7.3

print *, foo % search(3) !should print 11

end program

请注意,为了运行示例,您可以将上面的所有内容复制/粘贴到一个文件中。

最后的想法

我发现 Fortran 中缺少名称空间非常令人沮丧,破解它的唯一方法就是将其包含在名称本身中。我们有一些嵌套的“命名空间”,例如在 C++Utils::IO::PrettyPrinter和 Fortran 中Utils_IO_PrettyPrinter。我将 CamelCase 用于类的一个原因,例如,PrettyPrinter而不是Pretty_Printer,是为了消除命名空间的歧义。命名空间是大写还是小写对我来说并不重要,但是在名称和文件路径中应该使用相同的大小写,例如类utils_io_PrettyPrinter应该位于utils/io/PrettyPrinter_M.f90. 在大型/不熟悉的项目中,您将花费大量时间搜索源代码树以查找特定模块所在的位置,并且在模块名称和文件路径之间制定约定可以节省大量时间。

于 2016-05-24T20:53:11.640 回答