osprey
osprey

Reputation: 768

When is implicit type declaration ever good?

I have a colleague who writes Fortran, generally following the F77 standard. I have had to work with some of their source code and I find the use of implicit type declarations (not using implicit none) really confusing and frustrating. Their reason for doing so is to "eliminate all of those type declarations at the beginning of my program(s)." In my opinion, this is a poor trade off for ruining readability and comprehension of the program and its many subroutines. Are there any other, more valid, reasons for not including implicit none in Fortran programs and subroutines?

Upvotes: 2

Views: 931

Answers (3)

Natsfan
Natsfan

Reputation: 4827

Well, I used punch cards but I usually use implicit none in my Fortran codes as well. Much of my Fortran coding now, however, is just to write a pre-processor or post-processor of data. Things that people use python for now. I know python but my programming skills in python are not that good. So when you're writing 100-300 lines of code only the implicit none can be a pain. I use it so people won't make fun of me but for short utility codes, implicit none buys you little. imho!

Upvotes: 0

chw21
chw21

Reputation: 8140

When your code is a stack of punch cards that must not be dropped or brought out of order, methods to reduce line count make sense. In modern time there is no reason for implicit typing.

I had an old guard colleague who could not be dissuaded from using implicit typing. He claimed that using implicit typing for most variables and explicit typing only for those few variables that don't follow Hungarian notation makes these variables stand out more and that helps him to better understand the code.

Use comments for readability. That's what they're there for.

Upvotes: 1

francescalus
francescalus

Reputation: 32431

Just to be entirely clear, relying on implicit typing is frowned upon by many. With good cause. However, there is one case where where some may view implicit typing the lesser of two evils.

Implicit typing allows one to have an object which is of a type whose name is not accessible in a scope:

  implicit type(badexample) (d)

  type badexample
    integer :: neverever=4
  end type badexample

  call sub

contains

  subroutine reallybad(badexample)
    integer, optional :: badexample
! With the declaration above we can't declare a local variable of explicit
! type badexample:
!   type(badexample) dontdothis
    print*, dontdothis%neverever
  end subroutine reallybad

end

Really, this is a poor excuse to use implicit typing. One which can be avoided by other design choices.


Under Fortran 77, of course, one cannot use implicit none.

Upvotes: 2

Related Questions