SAP / styleguides

This repository provides SAP style guides for coding and coding-related topics.
Other
1.68k stars 445 forks source link

Inline TYPES and name shadowing #208

Open Jonasdoubleyou opened 3 years ago

Jonasdoubleyou commented 3 years ago

Inside reports or methods, sometimes an intermediary datastructure is needed. In these cases I usually start to shadow² the name, that is I choose the same name for the type and the variable:

TYPES:
  BEGIN OF employee,
    empid    TYPE i,
    name     TYPE char5,
    location TYPE char6,
  END OF employee,
  employees TYPE STANDARD TABLE OF employee WITH EMPTY KEY.

DATA(employees) = VALUE employees(
  ( empid = 1 name = 'A' location = 'Pune'   )
  " ...
).

context

Now I think this is quite clean and pretty much follows this guide, especially "pick one word per concept", as conceptually, the type is just used to describe that one usage (yes, the example above is not quite optimal as "employee" is rather generic, and the internal table should use a more usecase specific name, however I had this case a few times already).

However this might be slightly confusing to readers, especially as using the same identifier for different entities only works in rare cases in ABAP, and a lot of other programming languages only have one namespace (the only exception I know is Typescript, which also separates type and variable identifiers).

As I'm quite unsure on how to deal with this pattern in the future, I'd like to raise the question here: Is name shadowing okay in this case (or even generally) when variable and type are the conceptually the same? And what should be done if the type and the name cannot share the same name (e.g. inside a class)?

Relevant section in the ABAP docs

²Shadowing is not completely fitting here, but I couldn't find a better term...