frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

1•ramsicandra•6mo ago
I'm relatively new to Java. I often notice a pattern where there is a list of constant which value are equal to the name.

  class Constant {
    public static final String ALBUM = "album";
    public static final String NAME = "_name";
    public static final String DISPLAY_NAME = "display-name";
    public static final String SERVICE_NAME_METRIC_NAME_PREFIX = "service_name.metric_name";
  }

Here is a public example of this practice I could find: https://developer.android.com/reference/android/provider/MediaStore.MediaColumns

I could understand that this might help in 2 ways refactoring and typo. This reduces chance of typo because you'll get compile error instead of run-time error if you typo a constant. This might also help in refactoring if you ever wants to change the value. but if may use this android public API example, I don't think it's wise to change a field name ever. If it's decommissioned, it's good to keep it so we don't re-use the field. If it's a new better field available, I think it should have a different name. I maybe making a straw man argument here. Let me know. If it's an internal API where such refactoring might make sense -- I still kind of think internal API should also be backward compatible, replacing a string are not a complicated operation in my opinion.

I see that this practice has a cost. One being that in every class that use this API. You need to add an import. It's also often the const is only used once from my experience.

  import static com.example.MediaFields.NAME;
  import static com.example.MediaFields.DISPLAY_NAME;

  String value = json.getString(NAME);
  String value2 = json.getString(DISPLAY_NAME);
vs

  String value = json.getString("name");
  String value2 = json.getString("display_name");
You write 1 line for declaration plus 2 lines for each class using this API. This is not a big deal in terms of LoC and I'm not an LoC police. However, my sense is the cost outweigh the benefit.

What do you think?

Comments

lanna•6mo ago
You just made TWO typos: "display-name" vs "display_name" and "_name" vs "name", automatically counter-argumenting your point.

It is also for documentation. With the declared constants, we know all possible values. With plain strings, how am I supposed to know which values to use?

The benefits far outweigh the marginal cost.

ramsicandra•6mo ago
The -, _, and leading _ are just variations of white space / separator I have encountered. I think it's possible to document all the allowable values in the Javadoc section of the function that takes in string as their argument.

In the specific android example, I would put it here. Under projection params where it takes in all the Images.Media.* string consts.

https://developer.android.com/reference/android/content/Cont...

Though, if it's a practice of Java Engineer to document allowable enum like string as a constant, then I can say that's a valid argument.

Show HN: A non-encrypted password manager

https://www.bloodless.ai/
1•shayarma•4m ago•0 comments

AI-Generated Misstatement Risk:A Framework for Enterprise Organisations

https://zenodo.org/records/17885472
1•businessmate•5m ago•1 comments

Pg_ClickHouse: ClickHouse-Speed Analytics from Postgres

https://github.com/ClickHouse/pg_clickhouse
1•saisrirampur•6m ago•0 comments

DuckGPT – agent for solving complex coding problems

https://duckgpt.vercel.app
2•mrdw•7m ago•0 comments

AI will probably force you to gate your content

https://www.niemanlab.org/2025/12/ai-will-probably-force-you-to-gate-your-content/
1•coloneltcb•9m ago•0 comments

Trump's SAVE tool looks for noncitizen voters. It's flagging U.S. citizens too

https://www.npr.org/2025/12/10/nx-s1-5588384/save-voting-data-us-citizens
3•starkparker•9m ago•0 comments

Maybe we don't need a server

https://lecaro.me/20251203-maybe-we-do-not-need-a-server.html
1•todsacerdoti•10m ago•0 comments

Google adding second AI to Chrome

https://www.msn.com/en-us/news/technology/google-to-deploy-a-second-ai-in-chrome-to-make-sure-the...
1•drivingmenuts•11m ago•1 comments

Convenience store socks became Japan's coolest souvenir

https://www.bbc.com/travel/article/20251209-how-convenience-store-socks-became-japans-coolest-sou...
3•billybuckwheat•12m ago•0 comments

Show HN: QueueWatch – Laravel queue monitoring with real-time alerts

https://queuewatch.io
1•mvpopuk•14m ago•0 comments

Coatings: The surface behind space missions

https://blog.satsearch.co/2025-12-10-coatings-the-hidden-surface-behind-space-missions-with-acktar
3•kartikkumar•14m ago•0 comments

Earliest Evidence of Making Fire

https://www.nature.com/articles/s41586-025-09855-6
3•cf100clunk•15m ago•1 comments

Edelweiss triples prices charged to publishers after acquisition

https://www.publishersweekly.com/pw/by-topic/industry-news/bookselling/article/99205-edelweiss-pr...
1•ilamont•15m ago•0 comments

ICE is using smartwatches to track pregnant women, even during labor

https://www.theguardian.com/technology/2025/dec/10/ice-tracking-pregnant-women
2•mdhb•16m ago•0 comments

Pay less taxes using web agents, a directed graph, and Dijkstra's algorithm

https://berke.bearblog.dev/pay-less-taxes-using-web-agents-a-directed-graph-and-dijkstras-algorithm/
1•realberkeaslan•17m ago•0 comments

Show HN: Metaskills: AI agents that autonomously create their own capabilities

https://earthpilot.ai/metaskills/
1•ada1981•17m ago•0 comments

The real AI Bubble

https://www.vipshek.com/blog/ai-bubble
1•vipshek•19m ago•0 comments

Why Write Engineering Blogs?

https://writethatblog.substack.com/p/why-write-engineering-blogs
1•cyndunlop•19m ago•0 comments

Cdktf Has Been Deprecated

https://github.com/hashicorp/terraform-cdk/commit/a0c56138e2b4c7b6c128bd7ba08a8e317df852af
2•todsacerdoti•20m ago•0 comments

Domains as "Internet Handles"

https://xn--gckvb8fzb.com/domains-as-internet-handles/
2•rozhok•20m ago•1 comments

I do not regret spending 8 years of my life in crypto

https://twitter.com/nic_carter/status/1998813611997745386
1•wmf•21m ago•2 comments

A video on the details of how Trunk-Based Development worked at MFT Energy

https://www.youtube.com/watch?v=CR3LP2n2dWw
1•todsacerdoti•22m ago•0 comments

News.Y Combinator.com

1•molchanovartem•23m ago•0 comments

New eBook Download Options for Readers Coming in 2026

https://www.kdpcommunity.com/s/article/New-eBook-Download-Options-for-Readers-Coming-in-2026?lang...
1•resfirestar•23m ago•0 comments

Harper Can Apply Title Case

https://elijahpotter.dev/articles/harper-can-apply-titlecase
1•chilipepperhott•24m ago•0 comments

Pentagon ordered to form AI steering committee on AGI

https://www.perplexity.ai/page/pentagon-ordered-to-form-ai-st-3qDBlb0uS0SHVH5mHEjxJw
2•chickensong•25m ago•0 comments

Flexible display cools surfaces for energy-efficient signs

https://techxplore.com/news/2025-11-flexible-display-cools-surfaces-energy.html
1•PaulHoule•27m ago•0 comments

Context management and project planning tool

https://vibecoco.app/
1•rjboadi•28m ago•2 comments

I bought a Grace-Hopper server for €7.5k on Reddit and converted it to a desktop

https://dnhkng.github.io/posts/hopper/
8•dnhkng•28m ago•1 comments

It's cheaper to buy a new printer every month

https://idiallo.com/byte-size/cheaper-to-buy-a-new-printer-every-month
6•foxfired•30m ago•3 comments