The Passionate Programmer

AI

A Conversation on Using AI to Generate Better Names in Code

Note: The following is a conversation between me and ChatGPT 4o. 

David: Hey ChatGPT, I’ve been thinking about how crucial naming is in programming. Good names for methods, types, and namespaces can make a huge difference in code readability and maintainability. But coming up with the perfect name can be challenging. Can you help with that?

ChatGPT: Absolutely, David. Naming things is indeed one of the hardest problems in computer science. Good names are clear, concise, and descriptive. They should convey the purpose and usage of the code elements they represent. How can I assist you today?

David: Let’s start with what makes a good name. Can you outline the key characteristics?

ChatGPT: Sure. A good name should be: 1. Descriptive: It clearly conveys what the method, type, or namespace does or represents. 2. Concise: It’s as short as possible while still being descriptive. 3. Consistent: It follows a naming convention that is consistent throughout the codebase. 4. Contextual: It fits within the context of the application and the specific functionality it’s related to. 5. Unambiguous: It should avoid any potential confusion or misinterpretation.

David: That makes sense. So, how does AI, like you, choose a name? What’s the process behind it?

ChatGPT: When I generate names, I consider several factors: 1. Context Analysis: I look at the surrounding code and comments to understand the purpose of the method, type, or namespace. 2. Keyword Identification: I identify key actions, objects, or roles described in the code. 3. Pattern Recognition: I recognize common naming patterns and conventions used in the codebase. 4. Language and Syntax Rules: I apply programming language-specific rules to ensure the names are syntactically correct. 5. Descriptive Clarity: I aim to make the name as descriptive as possible without being overly verbose.

For example, if you have a method that calculates the interest on a loan, I would analyze the code and context to suggest names like CalculateInterest or ComputeLoanInterest.

David: Interesting. Can you give me a practical example? Let’s say I have a method that retrieves a list of active users. How would you help me come up with a better name for it?

ChatGPT: Of course. Let’s start by understanding the purpose of the method. It retrieves a list of users who are currently active. Based on this description, a good name should include keywords like “Get,” “Active,” and “Users.” Here are a few suggestions: – GetActiveUsers – FetchActiveUsers – RetrieveActiveUsers – LoadActiveUsers

Each of these names clearly describes what the method does. The choice between them can depend on your preferred naming conventions and the style used in your codebase.

David: Those are great suggestions. What if I want to refine these names further? How can I collaborate with AI to iterate on and improve the names?

ChatGPT: Collaboration between developers and AI for refining names involves a few steps: 1. Initial Suggestions: Start with the AI’s initial suggestions, like the ones I provided. 2. Contextual Feedback: Provide feedback on how these names fit within the specific context of your application. For example, if “Fetch” is more commonly used in your codebase than “Get,” you might prefer FetchActiveUsers. 3. Refinement: Ask the AI to refine the suggestions based on this feedback. For instance, “Can you suggest names using ‘Fetch’ instead of ‘Get’?” 4. Iteration: Repeat the process, incorporating more context or specific requirements. For example, if you need the name to reflect that the method retrieves users from a database, you might ask for names like FetchActiveUsersFromDb. 5. Final Selection: Choose the name that best fits your conventions and is most descriptive.

David: Got it. This iterative approach seems useful. Now, let’s discuss types and namespaces. How do you approach naming these?

ChatGPT: Naming types and namespaces follows similar principles but with some added considerations: – Types (Classes, Interfaces, Enums): The name should reflect the entity’s role or purpose. For example, a class representing a user should be named User or Customer if it’s specific to customers. If it’s an interface, prefixing with “I” is common, like IUserService. – Namespaces: These should group related types and functionality. Namespaces should reflect the broader context or module they belong to. For instance, all user-related types might go under ProjectName.Users.

For a type that manages user sessions, suggestions might include: – UserSessionManager – SessionHandler – UserSessionController

For a namespace containing all user-related functionality: – ProjectName.Users – ProjectName.Authentication.Users – ProjectName.Services.Users

David: That’s helpful. How can developers ensure that the names are consistent and follow conventions throughout the codebase when using AI?

ChatGPT: Ensuring consistency can be achieved through the following steps: 1. Establish Naming Conventions: Define clear naming conventions and document them. These should cover method names, types, and namespaces. 2. Training the AI: Use examples from your codebase to train the AI on your specific conventions and preferences. The more context the AI has, the better its suggestions will align with your standards. 3. Regular Review: Periodically review the names suggested by AI to ensure they adhere to conventions. Provide feedback to improve the AI’s performance. 4. Automated Checks: Implement automated code review tools that check for naming consistency and provide feedback based on your conventions.

David: That makes sense. Do you have any final thoughts on how AI can help developers improve code quality through better naming?

ChatGPT: Certainly. Better names lead to clearer, more maintainable code. By using AI to suggest and refine names, developers can ensure that their code is self-explanatory and easier to understand. This reduces the cognitive load on developers and helps new team members get up to speed faster. Additionally, by offloading the task of naming to AI, developers can focus more on solving complex problems and writing high-quality code.

AI can also continuously learn and adapt to the evolving codebase, providing increasingly accurate and relevant suggestions over time. This dynamic collaboration between developers and AI fosters a more efficient and productive development environment.

David: Thanks, ChatGPT. This has been really insightful. I’m looking forward to using AI to improve our naming conventions and overall code quality.

ChatGPT: You’re welcome, David. I’m here to help. Let’s make your code clearer and more maintainable together!

What do you think? Please join the conversation with your comments below.