A better and *shorter* method name using ChatGPT

A better and *shorter* method name using ChatGPT
Photo by Rolf van Root / Unsplash

When I write code, I don't care how the function is named. But when it's done, I want a good and short name.

Often my methods are just called "magic" since I don't care yet. After it's complete, I update the name that explains what the code does. This results in a very long and verbose name. A trick I like to employ is using ChatGPT to generate a shorter name.

The following is a discussion I had with ChatGPT. Since ChatGPT has a good interface for improving your point (by going back and forth) it's easy to slowly get to a good result. Here is an example of the discussion.

DevWouter: I'm looking for a shorter name than FindOrganisationsAndIncludeFirstAttachedOrgAndAllInBetween


ChatGPT: How about "OrgFinder"?


DevWouter: It's a function (C# language), so not an object or a value. And we have more than one "Find" function. Here is the full code, may that helps?

private IEnumerable<Guid> FindOrganisationsAndIncludeFirstAttachedOrgAndAllInBetween(Guid userId, Guid organizationId)
{
    List<Guid> orgIds = new List<Guid>();
    orgIds.Add(organizationId);

    var matched = _dbConnection.QuerySingle<int>(
        "SELECT COUNT(1) FROM Organization_Attached_UserConnections WHERE Organization_id = @OrganizationId AND User_id = @UserId",
        new { OrganizationId = organizationId, UserId = userId }
    );

    if (matched > 0)
    {
        return orgIds;
    }

    var parentOrg = _organizationService.FindParentOrganizationId(organizationId);
    if (parentOrg != null)
    {
        orgIds.AddRange(FindOrganisationsAndIncludeFirstAttachedOrgAndAllInBetween(userId, parentOrg.Value));
        return orgIds;
    }

    return new Guid[0]; // Empty array
}

(Note: This was after refactoring some old code, so it's not the best code, but it's what I have to work with)


ChatGPT: Thanks for providing more context. Here are a few suggestions for a shorter name that still conveys the purpose of the method:

  1. FindAttachedOrgs: This name conveys that the method finds organizations attached to the specified user and includes the first attached org and all in between.
  2. GetOrgsInRange: This name conveys that the method gets organizations within a range that includes the first attached org and all in between.
  3. FindLinkedOrgs: This name conveys that the method finds organizations linked to the specified user and includes the first linked org and all in between.

DevWouter: It's not a range, that we search in. And it's not only attached that it's searching for. Basically it gets the current orgGuid and then calls it self unless it finds that it was attached.


ChatGPT: Thank you for the clarification. Based on that, here are a few more suggestions for a shorter name:

  1. FindOrgsWithAncestors: This name conveys that the method finds organizations along with their ancestors until it finds an organization that is attached to the specified user.
  2. FindLinkedOrgsWithAncestors: This name conveys that the method finds organizations linked to the specified user and includes their ancestors until it finds an attached organization.
  3. FindUserOrgAncestry: This name conveys that the method finds the ancestry of an organization relative to the specified user.

DevWouter: What about FindOrgsUntilFirstAttached?
(Note: At this point I had already made the decision, but I find it fun to ask a large language model for an opinion)


ChatGPT: "FindOrgsUntilFirstAttached" is a good name that conveys the purpose of the method accurately. It is clear and concise, which makes it easy to understand the method's functionality at a glance. This name also follows the convention of using verb-noun pairs for method names, which is commonly used in C# programming. Overall, I think "FindOrgsUntilFirstAttached" would be a good choice for the method name.

Note: This is not a opinion. It's just content generated that is common in this context. Quite a length answer and I wonder if this is to rank up the bill.


DevWouter: Thanks!


ChatGPT: You're welcome! If you have any more questions or need further assistance, feel free to ask.