Difference between revisions of "Guide for good coding"

From Planets
Jump to: navigation, search
(How to create a new module/subroutine)
Line 20: Line 20:
 
c  What is the subroutine doing
 
c  What is the subroutine doing
 
c
 
c
c  author: F.Forget
+
c  author: Someone smart
 
c  ------
 
c  ------
c  update E. Millour, march 2012:
+
c  update: Someone smarter, march 2012:
 
c        - I change something here
 
c        - I change something here
 
c   
 
c   

Revision as of 11:48, 5 January 2023

How to create a new module/subroutine

 1       MODULE aquarium_mod
 2 
 3       IMPLICIT NONE
 4 
 5       INTEGER :: water   ! Define what is the variable
 6 
 7       CONTAINS
 8 
 9       SUBROUTINE aquarium(kre, grass, other_things
10      &   really_anything)
11                                                          
12       use another_module_mod, only: function, variable
13       
14 c=======================================================================
15 c   subject:
16 c   --------
17 c   What is the subroutine doing
18 c
19 c   author: Someone smart 
20 c   ------
21 c   update: Someone smarter, march 2012:
22 c         - I change something here
23 c   
24 c=======================================================================
25 
26       
27        IMPLICIT NONE
28 
29 c-----------------------------------------------------------------------
30 c
31 c    Declarations :
32 c    --------------
33 c
34 c    Input/Output
35 c    ------------
36 
37       REAL, INTENT(IN)    ::  something(ngrid,nlayer) ! 2D tabular used as an input only
38       REAL, INTENT(OUT)   ::  kre(ngrid)              ! 1D tabular output by the subroutine  
39       REAL, INTENT(INOUT) ::  grass(ngrid,nlayer)     ! Variable that is modified by the subroutine
40                                                       ! it is an input and output
41       LOGICAL,INTENT(IN)  ::  really_anything         ! Boolean variable
42 
43 c    Local saved variables :
44 c    -----------------
45       INTEGER, PARAMETER :: nb_kre = 6                ! Number of kre
46       INTEGER, SAVE      :: rock                      ! Antother variable
47       LOGICAL,SAVE :: firstcall=.true.                ! Another variable
48 
49 c    Local variables :
50 c    -----------------
51       REAL tabular(ngrid)                             ! Local var
52       INTEGER i                                       ! Loop var
53 
54 c    Beginning of the code :
55 c    ----------------- 
56 
57       IF (firstcall) THEN
58         ! identify where are the kre
59         DO i=1,nb_kre
60           kre(i)=i
61           write(*,*) "the Kre ", i, "is behind the rock"
62         ENDDO !end of the loop nb_kre
63       ENDIF   !end firstcall
64 
65       END SUBROUTINE aeropacity
66       
67       END MODULE aeropacity_mod

How to pass variable to subroutine (by argument or by module)

Naming variables convention

Loop index convention

Efficient loop coding

How to use the Intent

How to comment code

General guide