Best_Practices icon indicating copy to clipboard operation
Best_Practices copied to clipboard

Use keyword argument calling-style for all optional arguments in procedures callings

Open szaghi opened this issue 9 years ago • 0 comments

For readability/debugging/robustness/safeness (what else?) purposes it could be interesting to add a guideline about

use keyword argument calling-style for all optional arguments in procedures callings

e.g.

subroutine foo(a, b, c)
  integer, intent(in) :: a
  real, intent(in) :: b
  logical, optional, intent(in) :: c
end subroutine foo
...
call foo(x, y, z) ! bad
call foo(a=x, b=y, c=z) ! good 

The discussion is open :smile:

From @tclune suggestion

module Foo_mod
   implicit none
   private
   public :: Foo

   type :: Foo
      integer :: i
   contains
      procedure :: some_method
   end type

   ! This type is PRIVATE
   type :: Unusable
   end type

contains
   subroutine some_method(this, unused, opt1, opt2)
      class (Foo), intent(inout) :: this
      type (Unusable), optional, intent(in) :: unused
      real, optional, intent(in) :: opt1
      integer, optional, intent(in) :: opt2
    end subroutine some_method
end module Foo_mod

This enforce to call myfoo%some_method(opt1=x, opt2=y)

szaghi avatar Jan 21 '16 17:01 szaghi