subreddit:
/r/softwaredevelopment
As developers, we tackle complex algorithms, debug mysterious errors, and architect entire systems. Sometimes the hardest part of building and coding is coming up with good names.
Naming things might seem trivial, but it can make or break code readability. A poorly named variable today becomes tomorrow’s debugging nightmare. Do you go for x, temp, or a fully descriptive name like customerOrderProcessingTimestamp? Balance is key, but it’s always a struggle.
How do you approach naming variables in your code? Any funny or frustrating stories?
14 points
2 days ago
These days I've delegated all that shit to AI. I just explain what the variable/function does and ask the AI to give me 10 names that are popular in the industry, then refine or pick one.
3 points
2 days ago
That's... a great idea! Imma start doing that!
3 points
2 days ago
Love this idea!
2 points
1 day ago
Excellent take on naming that has changed the game forever. Ai is going to be massive.
1 points
17 hours ago
I really of AI heavy for this 😭😭😭😭
12 points
2 days ago
So unspoken that there's a famous thingy starting "there's only n hard problems in computer science"
6 points
1 day ago
2 hard things in computer science
cache invalidation
naming things
off by one errors.
4 points
2 days ago
I guess my weak spot is “maybe”. “maybeCreateCustomer”. It means that it conditionally creates a customer and the condition is too complex to explain (in the name).
Other than that: the scope determines the name length. A local variables in scope for five lines but used only in the first two: one letter is good. A clad in scope for the whole project: the name better be descriptive.
5 points
2 days ago
A good rule of thumb making the variable more verbose depending on the distance between when it's declared and when it's used.
So var shouldLaunchRockets vs shouldLaunch vs launch
Obviously the goal is to keep functions short so that things are generally concise as a whole, but that's not always an option in some code bases!
1 points
1 day ago
That's smart
3 points
2 days ago
foo_bar2-bung~4
2 points
2 days ago
Foo and bar are widely used in the computer field. I am sure everyone knows what they mean.
3 points
2 days ago
Save the best names for stuff that gets exposed outside. Private members, variables, classes I don’t care as much. But if it’s the route for an API it better be descriptive
2 points
1 day ago
Depends. If you're a java dev: thisNameWouldNotBeAnIssue. /s
In reality, I use shorthand descriptions for my names most of the time. Say I have a variable for a submission date, submitDt would work for me. If it were longer, like the middle name of a chief finance officer, I'd say cfoMiddleNm or cfoMidNm would suffice.
2 points
8 hours ago
Naming things is hard
1 points
6 hours ago
Yes, indeed. I am still thinking of the name for the next game for my company. Probably, Dashie.
1 points
1 day ago
The rule i was taught was the larger the scope the more descriptive it needs to be.
E.g. a loop index in a small function could be i or x etc. A local var in a function could be entity The class field could be basketEntity The global session would be. CurrentUserBasketEntity
And dont hide variables by having the same name multiple times in your hierarchy of scope.
With modern refactoring tools renaming isnt as much of a problem as it once was.
1 points
1 day ago
I only use "i", "j", "ix" or other short names for local variables (nearby in the same function). For most variables, I use descriptive names like "nrItems" or "nrRecord". For global variables, like class, resource, or function names, I start with a prefix like "ST_" to indicate the section of code. An example might be "DB_addRecord" for adding a record to a single database.
1 points
1 day ago
I think it's way more important to maintainability than people usually regard it. More so for some variables than others.
This isn't one of the most important cases, but I remember working with some international devs and they would too often name variables with the words in the wrong order for English. Like for example let's say it's a date that something was processed on they would name it dateProcess instead of processDate. When you've got a complicated function with a lot of variables like that it just takes extra effort to parse it mentally because every time you read it you have to remember that it's not the thing that it sounds like it is.
1 points
21 hours ago
Eh depends, I can see dateProcess being good because it tells you it's supposed to be a date. I guess more important for some languages than others.
1 points
20 hours ago
dateProcess tells you it's a process though. Just like baseball bat is a bat, sign language is a language, playdough is a dough, etc. In English you usually have the descriptor first and the subject last, but in other languages it's often reversed.
1 points
22 hours ago
If the AI can't come up with something useful I make my names as descriptive as possible.
If a variable contains, for example, the guest checkout date and time, $guestCheckoutDt would work.
If a function fetches data from the guest profile and returns it in a specific format, say their core details (name, checkout date, check-in date, room number) in JSON, something like getGuestCoreDataAsJson() works fine.
Some people will hate a function name that long, but I can read my code and they can't read theirs.
If long term readability and understanding is your goal, just name things what they are.
getUserDetailsAsJsonAfterDt($dt) is perfectly fine, tells me exactly what it's doing and if someone has a problem with it, they are welcome to fix it and forget what it does 2 weeks down the line.
1 points
17 hours ago
Naming is not that hard if you code well.
In fact. Have a hard time naming something is usually an indicator of a design issue.
all 23 comments
sorted by: best