This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Is it acceptable to have long names for variable?

 
Ranch Hand
Posts: 1257
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it acceptable to have long names of variables like countOfRequestIdsWithComputationComplete ?
Thanks
 
Marshal
Posts: 65782
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read them out. Are they pronounceable?
 
Monica Shiralkar
Ranch Hand
Posts: 1257
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is to denote count of request id's for which computation is complete.  I could not find a better name for it. Yes it is pronouncable but it is long.
 
Campbell Ritchie
Marshal
Posts: 65782
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't reduce it to computedRequestIDsCount? If not, then it will have to be acceptable. Put your foot down and say it is all right.
 
Monica Shiralkar
Ranch Hand
Posts: 1257
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. And what if I want count of request Id's which have been inserted into the queue named ProcessingQueue.?  The name can be countOfReqIdsInsertedIntoProcessingQueue.   It's long. Is it acceptable?
 
Campbell Ritchie
Marshal
Posts: 65782
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless you can change it to queuedRequestIDsCount, yes, tell everybody it is acceptable.
 
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Variables do not exist in a vacuum. The surrounding code should give as much context to what the variable means such that the variable itself should not have to carry all the context in its name. I would imagine ids could be a suitably expressive name if surrounding code were written like this:

One needs only look at the surrounding code to see that those are all the ids of requests that are being added to the queue.

So instead of going overboard with expressive names, refactor to make sections of code expressive. To borrow from a quote often attributed to Albert Einstein, make your code as expressive as possible, but not overly expressive.
 
Saloon Keeper
Posts: 21128
131
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The biggest problem with long variable names is when you have to type them in repeatedly. Although auto-complete in IDEs these days makes that less of a concern. In olden days, I'd simply type in a quick-and-dirty name like "qix" when coding and then do a refactor-rename or global text edit to make them meaningful.

The second-biggest problem is that really-really-long names eat of a lot of display (or printout), which can make the overall code harder to read.

On top of which, if you have multiple really-long names which are similar - say countOfRequestIdsWithComputationComplete and countOfRequestIdsWithComputationInProgress, the sheer length of the name is a mental block and does more harm than good.

Meaningful names are a laudable goal. Naming stuff like "j37" or "qix" rarely makes for a maintainable program. But likewise too much of a good thing.

I'd usually go with something like "requestsCompleted" and let the context (as Junilu said) fill in the details.
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I wrote:I would imagine ids could be a suitably expressive name if surrounding code were written like this:


Better yet, I'd refactor that even further to:

See how that works?
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:I'd simply type in a quick-and-dirty name like "qix" when coding and then do a refactor-rename or global text edit to make them meaningful.


Yes, an absolutely essential thing to do after anything quick-and-dirty. Perfectly acceptable approach if you're working in an editor with limited capabilities.
 
Monica Shiralkar
Ranch Hand
Posts: 1257
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This name queuedRequestIDsCount would  be fine only if I have only 1 queue . I am taking about the case if you have multiple queues and you only want the count of requestIds that have been inserted into ProcessingQueue.
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
¯\_(ツ)_/¯

That's why programming is hard -- you have to actually think things through and decide what makes sense.

You asked whether long names are acceptable and you have been given examples where a long name might be fine and examples where some refactoring to provide more context can help you shorten names to something more reasonable without loss to expressiveness. I think you just have to take the different advice you've been given, apply some critical thinking and decide for yourself what length of variable name is appropriate for your context.
 
Tim Holloway
Saloon Keeper
Posts: 21128
131
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:This name queuedRequestIDsCount would  be fine only if I have only 1 queue . I am taking about the case if you have multiple queues and you only want the count of requestIds that have been inserted into ProcessingQueue.



That sounds like possibly a deeper problem. In Object-Oriented programming, you tend to have shorter names because the items in question are object properties and the name of the object acts as a higher-level qualifier.

Just as a rough example, you might have a RequestProcessor with several queues as its properties: computeWaiting, computeInProgress, computeCompleted. The containing application might submit a request to be processed, the processor then works its queues and when it's done, you can pull the completed request and do whatever (if anything) you want with it.

so instead of countOfRequestIdsWithComputationComplete you would instead have something like "int completedRequestCount = requestProcessor.completedRequests.length();"
 
Campbell Ritchie
Marshal
Posts: 65782
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Variables do not exist in a vacuum. . . .

Nor does critical thinking, as Junilu mentioned earlier. If you do some critical thinking, what would the presence of such a List/Queue/OtherCollection tell you? What would happen if it is accompanied by a count variable as you had earlier? Note Tim H's comments.
 
Tim Holloway
Saloon Keeper
Posts: 21128
131
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact, countOfRequestIdsWithComputationComplete sounds like the name you would give a (shudder!) global variable.
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:In fact, countOfRequestIdsWithComputationComplete sounds like the name you would give a (shudder!) global variable.


Double-

... and shudder  
 
lowercase baba
Posts: 12766
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hopefully, wherever you work has some kind of coding standard.  Regardless of whether you think it is right or wrong, it is critical you FOLLOW it.  If there is a standard for how variables are to be named, then use it.
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

fred rosenberger wrote:Hopefully, wherever you work has some kind of coding standard.  Regardless of whether you think it is right or wrong, it is critical you FOLLOW it.  If there is a standard for how variables are to be named, then use it.


(Always the rebel, me) Certainly, conform to standards but only to the extent that it makes sense. If the "standard" falls short in some cases or there are compelling reasons to go against the grain, try to have a discussion with teammates about why and what compromises you all are willing to accept in those exceptional cases. The last thing you want to do is just blindly follow standards even when faced with an obviously exceptional case. Again, try to always apply critical thinking in what you do.
 
Monica Shiralkar
Ranch Hand
Posts: 1257
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Greenhorn
Posts: 15
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What a great thread. I love how the focus is always on making the code easy to understand. If a long variable name is necessary to understand the code, then a long variable name is best, but as mentioned, it may be possible to refactor or place the variable in such a context so that a long name is no longer necessary to understand what the variable means. And the devil is in the details; without seeing and understanding the code in depth no good specific recommendations can be made.
 
Don't MAKE me come back there with this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!