Hosted onhyper.mediavia theHypermedia Protocol

Use Nouns for Your Fields

    I have a pet peeve about naming fields in APIs and data structures (classes, structs, maps, dicts, whatever you call them) — I hate when fields are not nouns. I especially hate it when most fields are nouns, but some others aren't.

    I probably picked it up from using Google APIs quite a bit in the past — there's this Google guideline for field naming in their Protobuf APIs, that among other things suggests to use nouns for all the fields.

      There're some things to disagree with in these guidelines, but I think this rule is actually a very good one.

    The simplest example of what annoys me is the very commonly used names for lifecycle timestamps: createdAt and updatedAt. So there would be a structure describing something like a user, and it would have fields like name, email, address, and those createdAt and updatedAt fields. Why is it name and not namedAs then? Why those fields in particular are not nouns?

      I guess it comes from Ruby on Rails and its Active Record system, which then has spread everywhere.

    But then you'd see something describing like a calendar event, and it would have fields like start and end — why not startsAt and endsAt or something like this then?

    Some other examples:

      The DID spec has a weird alsoKnownAs field among all the other fields that are nouns. Couldn't they call it aliases or alternateNames or something like that?

      The UCAN spec that has field names like with and can which you will never know the meaning of by just looking at the data. I find that resource and ability would be much better.

      GitHub Actions workflow definitions that have fields like uses and with, which I always mess up and forget what they mean. I find that action and input or something like that would be much better.

    Finding a good noun for a timestamp is probably the hardest thing, so I keep following the Google's guideline (there's a different one for timestamps) and call them as {imperative}_timecreateTime, updateTime, startTime and so on. Sometimes the resulting word won't be a proper English word, but I'm OK with that for the purposes of consistency.