Scientist Programmer



  • I work with scientists. They are usually very smart, unfortunately they are not necessarily very good programmers. But the snippet below made me post this here.

    It is FORTRAN (scientists, you see?), and what it does is... well, it is part of the routine to read in a NetCDF file. 

    if (nvar.eq.5) then
      do i=1,nvar
        call nf_inq_varname(ncid,i,name)
        var(i) = name
        if (i.eq.1) call nf_inq_varid(ncid,var(i),lon_varid)
        if (i.eq.2) call nf_inq_varid(ncid,var(i),lat_varid)
        if (i.eq.3) call nf_inq_varid(ncid,var(i),lvl_varid)
        if (i.eq.4) call nf_inq_varid(ncid,var(i),rec_varid)
        if (i.eq.5) call nf_inq_varid(ncid,var(i),dat_varid)
      enddo
    endif

    For those unfamiliar with NetCDF files: Data is stored in named variables. Internally, these variables have an integer varid, by which they can be called.

    Inside the loop, it first asks the netCDF library to return the name of the variable with the varid i. And then, it asks for the varid of the variable with that name.

    The following would have done the same, but less complex:

    DO i = 1, 5
      call nf_inq_varname(ncid,i,var(i))
    ENDDO
    lon_varid=1
    lat_varid=2
    lvl_varid=3
    rec_varid=4
    dat_varid=5

    That would have also made me realise far quicker that the netCDF file needs to have the varids in a specific order, which it shouldn't have to.



  •  @Skywolf said:

    They are usually very smart, unfortunately they are not necessarily very good programmers

    The code snippet you present is very common with "professional" programmers. The pattern has a name even "for-if antipattern", and featured on the frontpage http://thedailywtf.com/Articles/Switched_on_Loops.aspx

    Consider yourself lucky! At least they are smart ;)



  • What makes this for-if instance really special, though, is the wonderful way it makes no use of the names defined NetCDF file's names except for the specific purpose of looking up IDs it already has. It's the Sirius Cybernetics Corporation anti-pattern.


Log in to reply