frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

1•ramsicandra•12mo 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•12mo 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•12mo 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.

Google Search Is Becoming an AI Agent

https://surfacedby.com/blog/google-search-is-becoming-an-ai-agent
1•startages•2m ago•0 comments

EU Cloud Provider News Roundup (Feb–May 2026)

https://www.eucloudcost.com/blog/eu-cloud-news-feb-may-2026/
1•mixxor•2m ago•0 comments

We replaced Redis with MySQL for inventory reservations – and it scaled

https://shopify.engineering/scaling-inventory-reservations
1•mlenol•5m ago•0 comments

Pythagorean Addition in the Head

https://entropicthoughts.com/pythagorean-addition
1•crescit_eundo•8m ago•0 comments

Rust Database from Scratch

https://github.com/ayoubnabil/aiondb
1•ayoubnabil•10m ago•0 comments

Show HN: Proposalkit.io – you write the content, we control the design

https://proposalkit.io/
1•silvertaza•12m ago•0 comments

Scented Entanglements (2023)

https://boasblogs.org/undoingraceandracism/scented-entanglements/
1•robtherobber•17m ago•0 comments

The slow creep of epistemic apocalypse

https://haversine.substack.com/p/the-slow-creep-of-epistemic-apocalypse
2•gpvos•17m ago•0 comments

Google's Next AI Push Is About Agents, Not Chatbots

https://firethering.com/gemini-3-5-flash-ai-agents-google-io/
1•steveharing1•18m ago•0 comments

Australia installs 400k home batteries in 10 months for 11.2 GWh

https://www.pv-magazine.com/2026/05/19/australia-installs-400000-home-batteries-in-10-months-for-...
1•xbmcuser•18m ago•0 comments

Git Tools – Rerere

https://git-scm.com/book/en/v2/Git-Tools-Rerere
1•trymas•18m ago•0 comments

Helsing and OHB form KIRK joint venture for European space reconnaissance system

https://defence-industry.eu/helsing-and-ohb-form-kirk-joint-venture-for-european-tactical-space-b...
1•vrganj•19m ago•0 comments

Ask HN: Why am I not able to add a comment on the "Who is Hiring" thread?

1•arnavpraneet•19m ago•2 comments

SAP Invests in AI Automation Startup N8n at $5.2B Value

https://www.bloomberg.com/news/articles/2026-05-12/sap-invests-in-ai-automation-startup-n8n-at-5-...
1•doppp•21m ago•0 comments

Why multi-agent AI breaks in prod and how Yugabyte's Meko is trying to fix it

https://tessl.io/blog/why-multi-agent-ai-breaks-in-production-and-how-yugabytes-meko-is-trying-to...
1•3littlefish•22m ago•0 comments

AI Assistance Reduces Persistence and Hurts Independent Performance

https://www.researchgate.net/publication/403562106_AI_Assistance_Reduces_Persistence_and_Hurts_In...
1•ColinWright•23m ago•0 comments

AI companies' DNS: 17 Anthropic-verified, 23% have spoofable email

https://domainintel.vercel.app/research
1•johnleslie_pm•24m ago•0 comments

C++26: More Function Wrappers

https://www.sandordargo.com/blog/2026/05/20/cpp26-copyable-function
1•ibobev•26m ago•0 comments

Navigation Systems in Video Games

https://pikuma.com/blog/navigation-systems-in-video-games
1•ibobev•27m ago•0 comments

Google Cloud suspended major customer Railway.com without cause, causing outage

https://www.theregister.com/off-prem/2026/05/20/google-cloud-suspended-major-customer-railwaycom-...
2•igortru•28m ago•0 comments

How to style a Hugo Atom feed with XSL

https://afranca.com.br/how-to-style-a-hugo-atom-feed-with-xsl/
1•blenderob•31m ago•0 comments

GDS weighs in on the NHS's decision to retreat from Open Source

https://shkspr.mobi/blog/2026/05/gds-weighs-in-on-the-nhss-decision-to-retreat-from-open-source/
2•blenderob•31m ago•0 comments

Show HN: Open-Source Agentic QA Harness with Memory

https://vostride.com/agent-qa
4•pranshuchittora•32m ago•0 comments

Prompts are technical debt too

https://www.seangoedecke.com/prompts-are-technical-debt-too/
2•blenderob•32m ago•0 comments

A 4K-year-old city became more equal as it became more successful

https://phys.org/news/2026-05-year-city-defied-history-equal.html
1•pseudolus•32m ago•0 comments

Show HN: I built a dashboard to combine my WHOOP, Oura and Apple Health data

https://vitaltrends.net
1•dansku•33m ago•1 comments

Show HN: I put Codex and Claude in a tank arena; Codex is winning 55% so far

https://old.reddit.com/r/codex/comments/1tgbb28/i_put_codex_and_claude_into_a_tank_arena_codex_is/
1•mazzystar•35m ago•0 comments

The missing men of the American marriage market

https://www.npr.org/sections/planet-money/2026/05/19/g-s1-122695/the-missing-men-of-the-american-...
1•pseudolus•35m ago•0 comments

Launched Chronl – a daily history puzzle (Garfield, Berlin Wall, etc.)

https://chronl.com/
1•fastpizzaguy•36m ago•0 comments

NTSB Analysis of MD-11F Engine Pylon Failure

https://www.youtube.com/watch?v=iWkLZjJXaos
1•connoronthejob•37m ago•0 comments