How do you pick good meaningful names?

JasinCole

Well-known member
Joined
Feb 16, 2023
Messages
66
Programming Experience
1-3
I'm just curious how others choose good meaningful names for there data structures?

One thing that always causes me to waste more time is choosing a name for things that make sense in the context of my code. Is there some special trick or resources you use that helps you decide or is this just something you develope overtime. What method do you use if any that helps you narrow down a proper name?
 
For C++, I use Hungarian naming conventions, so the only decision point becomes what suffix to use. Often the problem domain dictates what that suffix is.

Fortunately or unfortunately depending on your point of view, C# naming conventions explicitly says not to use Hungarian naming conventions, so that means leaning on the problem domain to provide the names. It tend to use plural nouns for collections, singular nouns for instances/classes, and subject-verb, or simply verb for method names. I try to follow the naming conventions that also dictates not to use the underlying type (which is what Systems Hungarian does) as part of the name. I do tend to break the no-Hungarian rule when it comes to variables referencing UI controls, as well as, using leading underscore prefixes for class variables. Hard habits to break from many years of C/C++ Win32 API coding.

Having a thesaurus readily available also helps when choosing names of things.
 
Is there some special trick

Pick the first thing that comes to mind. Refactor using the rename option later in VS if it turns out to be a bad choice

You're perhaps not alone; some of the junior devs I mentor turn in code like

C#:
    var dog = cars.MaxBy(c => c.Mileage);
    var cat = cars.MinBy(c => c.Mileage);

And when I say "I can see why one might refer to a high mileage car as a dog, but why pick these names?" and they say "I couldn't think of anything then my dog came in the room". When I ask "what about 'highestMileageCar'?" I get "oh, is that not a bit long?"

I've seen your code; I'm sure you don't struggle to this extent 😂

in the context of my code

It's more like on the context of the problem. What does this class you're trying to name represent in terms of the problem you're trying to solve, not the code you've already written.
 
Last edited:
So in this particular instance I am trying to represent income and expense which are opposite but the exact same thing as far as coding them goes.

In business these are considered cash flow. In an accounting context they are considered generically as accounts or a more refined term would be transactions.

My class is a model that I want to use to display the monthly quarterly and yearly values of those account types as a sum.

I think Cashflow probably works best in this instance. With properties for monthly quarterly and yearly values.

But to come to this conclusion took me days of back and forth. Free time only of course, but it feels like it’s a bunch of wasted time. I’m sure inexperience is part of this issue as sometimes I struggle with implementation details.

Now I’m prepared to be amazed that someone will post a reply and say, why not … and it will be so much more intuitive solution.
 
But to come to this conclusion took me days of back and forth. Free time only of course, but it feels like it’s a bunch of wasted time. I’m sure inexperience is part of this issue as sometim

For bigger projects (or projects that I think will be long lived and will likely be taken over by someone else), I usually draw use case and data flow diagrams. In the process of drawing those diagrams, nouns and verbs usually pop-up for the sake of labelling things while I refine my ideas/thoughts, or while discussing with the "client" to make sure that I understand the problem correctly, and that I'm solving the problem the way they want to. Those words often end up in the code.
 
So in this particular instance I am trying to represent income and expense which are opposite but the exact same thing as far as coding them goes.
Have a conversation with your accountant. I did with mine, asking them if they could think of a scenario where income and expense might be considered different concepts beyond a minus or not in front of a number. "Expenses may or may not be allowable expenditure for tax purposes, for example. Income doesn't have this concept" came back ..

..which set me thinking about an inheritance hierarchy where Income and Expense derive from a common, but eg an expense has an extra bool (say). In cases where the code can process them as the same, make the method accept the common. In the "enter your exposes" screen, the code works with Expense objects
 
Back
Top Bottom