frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Oura says it gets government demands for user data. Will it share how many?

https://this.weekinsecurity.com/oura-says-it-gets-government-demands-for-user-data-will-it-share-...
1•donohoe•4m ago•0 comments

Airbnb's Chesky Says US 'Misunderstanding' Use of Chinese Open-Source AI Models

https://www.bloomberg.com/news/articles/2026-05-20/airbnb-s-chesky-says-us-misunderstanding-use-o...
1•JSR_FDED•7m ago•1 comments

LLM's code is just untrusted text, until you validate it

https://hack8s.com/244/llms-code-is-just-untrusted-text-until-you-validate-it
1•justorius•7m ago•0 comments

Show HN: Fed.run – online collaborative Rust IDE and Markdown editor

https://fed.run
1•oinoom•8m ago•0 comments

Preventing AI agents from executing destructive terminal commands

https://github.com/7Majesty-M/terminal-guardian-mcp
1•majesty-m•8m ago•0 comments

Your Old Devices Depend on Dying Sensors. The Silicon Labs Incident Proves It

https://www.cambridge.org/engage/coe/article-details/6a054b304770e67d92e8c7a2
1•openrockets•8m ago•0 comments

We were building infra for OpenClaw, and today I just tried Hermes and holy shit

1•Stanlyya•9m ago•0 comments

Show HN: SimStream – code iOS apps from your phone by streaming the simulator

https://github.com/EliotAndres/SimStream
1•ea016•9m ago•0 comments

The Beauty of Tautologies

https://scottsumner.substack.com/p/the-beauty-of-tautologies
1•_laird•10m ago•0 comments

Hyperagents (Meta Research)

https://arxiv.org/abs/2603.19461
1•galsapir•11m ago•0 comments

How to Use a Spreadsheet for Combinatorial Creativity

https://www.nair.sh/guides-and-opinions/communicating-your-expertise/how-to-use-a-spreadsheet-for...
1•nilirl•12m ago•0 comments

Show HN: Solana Index Historical Solana Token Balance

https://solanaindex.top
1•hda_ycombinator•14m ago•0 comments

Texas sues Meta, WhatsApp over encryption privacy claims

https://www.reuters.com/legal/government/texas-sues-meta-whatsapp-over-encryption-privacy-claims-...
5•fahd777•14m ago•0 comments

The FBI Wants 'Near Real-Time' Access to US License Plate Readers

https://www.wired.com/story/security-news-this-week-fbi-license-plate-reader-real-time-access/
2•Brajeshwar•17m ago•0 comments

Show HN: I build a tool to encourage before reviewing code, review intents

https://github.com/mainline-org/mainline
1•crs0910•19m ago•0 comments

Is Logistic Regression Regression?

https://datascienceconfidential.github.io/r/predictive-models/2026/05/14/is-logistic-regression-r...
1•jprs•23m ago•0 comments

Dune Imperium Is an Everything Sandwich

https://12gramsofcarbon.com/p/boardgame-review-dune-imperium-is
1•theahura•24m ago•0 comments

Centaur – Multiplayer, self-hosted, secure agents

https://centaur.run/
1•magelinskaas•24m ago•0 comments

Emerging Patterns in Building GenAI Products

https://martinfowler.com/articles/gen-ai-patterns/
1•cebert•25m ago•0 comments

The Mysterious XF86AudioPlay Issue

https://michael-prokop.at/blog/2026/05/20/the-mysterious-xf86audioplay-issue/
2•birdculture•26m ago•0 comments

Tiny piece of math prevents perfect coding agents

https://hisohan.substack.com/p/this-tiny-piece-of-math-prevents
1•ronniebasak•26m ago•0 comments

Turn Phone to Speacker for PC

https://sonno.live
1•kinle•30m ago•0 comments

Tell HN: OpenAI Codex: Increase in users hitting Codex rate limits

https://status.openai.com/incidents/01KS88SRADTWQW27NYRAXMBAQN
3•embedding-shape•32m ago•3 comments

Sir John Soane and the red telephone box

https://www.soane.org/features/sir-john-soane-and-red-telephone-box-0
1•curio_Pol_curio•33m ago•0 comments

VPN [First VPN] used by ransomware actors dismantled in global crackdown

https://www.europol.europa.eu/media-press/newsroom/news/cybercriminal-vpn-used-ransomware-actors-...
3•sorenjan•34m ago•1 comments

Extensy

https://extensy.dev
2•amirlannk•34m ago•3 comments

How Metrics Drift: Goodhart's Law, Metric Gaming, and Reality Drift [pdf]

https://github.com/therealitydrift/reality-drift-library/blob/main/Reality%20Drift%20Project/03_R...
1•realitydrift•36m ago•0 comments

Why Did South Africa Relinquish Its Nuclear Weapons?

https://www.thecollector.com/south-africa-nuclear-weapons/
4•Tomte•36m ago•0 comments

Frontier Risk Report (February to March 2026) – METR

https://metr.org/blog/2026-05-19-frontier-risk-report/#incidents-hero
1•paraschopra•37m ago•0 comments

Maldives tragedy shines light on dangers of cave diving

https://www.theguardian.com/world/2026/may/23/maldives-diving-tragedy-cave-experts-warn-danger-sa...
1•YeGoblynQueenne•38m ago•0 comments