Search code examples
namingconventions

Classes with reserved names (keywords). How do you deal with this?


it just so happens that my class is a (Medical)Service, do you have any suggestions on what I would name a Service in angular that retrieves medical services?

So far I have thought of two things:

  • MedicalServicesService sounds a bit weird or not?
  • I was thinking of maybe replacing "(medical) service" by a synonym, maybe "medical assistance". Then I would have MedicalAssistanceService as the (programming) service name. Still, surgery hardly passes for a medical assistance. It really is a medical service.

Was curious what people do when one of their classes names just happens to use a programming keyword. A question open for debate.

Not sure if naming conventions questions are allowed here? I will gladly delete my question if they aren't. Thank you.


Solution

  • First off, this depends a lot of the language.

    TALK TO YOUR TEAM

    Secondly, and most important! Conventions may change a lot depending on your team. Talk to your team, and agree on one convention to use. Never forget this.

    Conventions

    There are universal conventions/standards to follow, and like you mentioned, using keywords is usually a bad-ish idea. I for one try to avoid them, just as I avoid using digits in my variable-names, even if the specific language I'm working with allows it. Reason? It's easier to stick with "let's avoid problems by mixing rules between languages" than having to check the rules each time.

    I often spend 30 minutes thinking of the perfect variable name, so I am quite used to this kind of pondering.

    Length

    Excessively long variable names is bad, because it hinders flow reading, while excessively short variable names are also bad because it is hard to guess what word you want. You could call it srvc, sure, but who will know what that means in a month (unless you comment it, sure). Dropping the vowels in user-variables is quite common, actually, especially in low-level/old languages.

    Specific case

    As for this specific example, I wouldn't think of MedicalService as a keyword. First off, it's part of a longer name, like MedicalFile doesn't look like a file from the system at all, but rather a form with medical data on it.

    I don't exactly know what this MedicalService does, but it seems like a generic (abstract, probably) class name for services that you can ask for at the counter of a hospital, so I'm assuming that.

    GenericMedicalThingToDo is a funny way to avoid the keyword, but I wouldn't call it that. MedicalUseCase seems quite better, and gets to the point.

    On the other hand, if this is just a string stating the use case for whatever it is the user has chosen (considering you mentioned Angular), I would just stick with userMedicalChoice (drop the PascalCase to camelCase).

    If you need to use a word that is actually a keyword, which often happens, you might want to add a _ on the end or the beginning of it. This is not usually good for interfaces, as it's conventional to only use those internally/privately. Some conventions use double _ for private, and single _ to avoid dupes.

    Last point:

    Having keywords as part of a longer variable name is not a problem in any of the many programming languages I have sailed in, so just call it MedicalService, or GenericMedicalService if you're going to subclass it.

    PS: Read up on some conventions of different languages, like PEP-8, and PEP-256 from Python, or Google's C++ conventions. While not specifically being valid for all languages, they do give you something pin-pointers to what is important.