Hi Pierre-Yves,
Sorry for late replay, was unavailable till 1st of March.
To get a better view on this topic, please have a look to following picture :
When a variable is declared with EXTERNAL clause, system first looks in this External table if same variable name has already been defined.
Here, as it’s the first time this external name is encountered at Run Time, it will add its name, length and address in the External Table and retains the program ID that first declared the External. (PROGRAM1)
Once the program (PROGRAM1) that first declared these External exits, the system marks all these External (E1 E2 E3) that have been declared in that program as free. (Logical delete, contains stay in place as long as not reused by another external)
PROGRAM1 is declaring all externals that will be used in current application during one LUW. (job)
It doesn’t use them, just reserving all of them. The first time a variable is declared with EXTERNAL clause, system first looks in this External table if same variable name has already been defined : when new one, it will add its name, length and address in the External Table and retains the program ID that first declared this External.
PROGRAM2 is also declaring E1 external, System will check the External table and found it as it was previously reserved by PROGRAM1, System checks so it matches the length of the declared variable in its table, and as it matches, it will reuse same address & by so, same content.
When leaving the PROGRAM2, E1 external is not marked as FREE as the program hat initially declared E1 external is PROGRAM1.
PROGRAM6 is declaring E1 external, System will check the External table and found it as it was previously reserved by PROGRAM1, System checks so it matches the length of the declared variable in its table, and as it matches, it will reuse same address & by so, content that also have been modified by PROGRAM2.
Unused/unreferenced external clean-up can be done at run time only :
- If we remove unreferenced declarations of E2 & E3 from PROGRAM4, you’re right : it won’t change anything in this case – all external have been defined in PROGRAM1
- If we remove all unreferenced declarations of E1 E2 E3 external from PROGRAM1, it WILL cause DATA issue : Data consistency won’t be ensured between PROGRAM2 and PROGRAM6 as E1 external will be marked as free (logical delete) when ending the PROGRAM2 (first program to declare it at RUN TIME). So, you have no idea of what will be the content of external E1 when executing PROGRAM6. Content could be kept or it could be reused by IMS system when memory space is needed – so we can use wrong data (worse case) or we can get an abend (best case).
As SonarQube analysis is done program per program, and can’t be done at RUN TIME, you can just detect an EXTERNAL field is not referenced in that program, but it’s not sufficient to recommend a deletion, as this recommendation could only be valid for intermediate programs.
Hope this is a bit clearer, don’t hesitate to ask for more in case
Cheers
Stef