vscode-rpgle icon indicating copy to clipboard operation
vscode-rpgle copied to clipboard

Lint option: Many defined variables with the same type should become LIKE(x)

Open worksofliam opened this issue 3 years ago • 2 comments

E.g.

Dcl-S name char(25);
Dcl-Ds stuff qualified;
  name char(25);
  initials char(3);
End-Ds;

Should be

Dcl-C NAME_LENGTH 25;

Dcl-S name char(NAME_LENGTH);
Dcl-Ds stuff qualified;
  name char(NAME_LENGTH);
  initials char(3);
End-Ds;

Though, not sure how to force this. We'd need to quick fix to handle this

worksofliam avatar Nov 18 '21 04:11 worksofliam

I think this is tricky to determine. Not all CHAR(25) are names. I guess if the name of the field is always "name" it -might- be ok, but it could also be annoying to get lint errors that were irrelevant. A "name" subfield in Product_Info and a "name" subfield in Pet_Info might only be both 25 by coincidence, and shouldn't necessarily be tied together with a named constant or LIKE definition.

bmorris1 avatar Nov 18 '21 19:11 bmorris1

Actually doing some RPG coding, and come here to raise this as a linter idea! I'd noticed I was using a CHAR(7) everywhere to represent the same entitiy, so went back to change it to use LIKE.

As far as implementation, you should definitley exclude anything with the TEMPLATE keyword from being picked up as an error by this.

However, I would also say the specification of a template should probbaly be factored in to the count. That is, I have defined a TEMPLATE with type CHAR(25), so thats 1 with type char(25)... now do I use it anywhere else. (although I agree with Barbara this would be difficult to distinguish from a genuinely different field which has the same specification... although it could just prompt people to template that one as well!)

Also a point to note, the heading mentions using LIKE(x) but your example does not actually use LIKE, it uses a named constant to define the length of a char;

I would say the following is a "best-practice" example of using a LIKE:

Dcl-S name_t char(25) TEMPLATE;

Dcl-S name like(name_t);

Dcl-Ds stuff qualified;
  name like(name_t);
  initials char(3);
End-Ds;

or how its more likley to end up if the initial variable is globally defined and used within the main program:

Dcl-S name char(25);

Dcl-Ds stuff qualified;
  name like(name);
  initials char(3);
End-Ds;

I do use TEMPLATE more regularly if I am using a field from a file as the template:

DCL-DS file_t EXTNAME('filename') QUALIFIED TEMPLATE;

DCL-S name like(file_t.name)

priceaj avatar Dec 01 '21 16:12 priceaj