frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Dominoes Agent Tracker: pizza tracker for your agent work

https://github.com/robertkarl/DominoesAgentTracker
1•robertkarl•1m ago•0 comments

My journey in optimising Elixir codebase

https://hauleth.dev/post/things-about-elixir-you-probably-will-never-need/
1•hauleth•1m ago•0 comments

Viral Ink – AI agent that writes LinkedIn posts in your voice

https://github.com/Sohamp2809/viral-ink
1•Soham2809•1m ago•0 comments

Agent Skills Latent Space

https://old.reddit.com/r/dataisbeautiful/comments/1sqzctu/i_mapped_907_agent_skills_into_a_3d_lat...
1•shmulc•1m ago•0 comments

Punkt MC03 Smartphone

https://www.punkt.ch/products/mc03-premium-secure-smartphone
1•pentagrama•3m ago•0 comments

Regenerative Organic Agriculture and Reimagining the Food System

https://www.hbs.edu/environment/podcast/Pages/podcast-details.aspx?episode=8426237464
1•doener•3m ago•0 comments

Show HN: Resume Script for VLC

https://gist.github.com/hq6/3d1f291de5b808da7d545ed44d6fc82b
1•hq6•3m ago•0 comments

AutomationBench by Zapier

https://zapier.com/benchmarks
1•ofou•4m ago•0 comments

How to Use Git Worktrees with Claude Code

https://old.reddit.com/r/ClaudeAI/comments/1sqxpkv/how_to_use_git_worktrees_with_claude_code/
1•bgnm2000•5m ago•1 comments

Mobile phones to be banned in schools in England under new plans

https://www.theguardian.com/politics/2026/apr/20/mobile-phones-statutory-ban-schools-england-bill...
2•chrisjj•5m ago•0 comments

Show HN: Dunetrace – Runtime failure detection for AI agents

https://github.com/dunetrace/dunetrace
1•dunetrace•6m ago•1 comments

AI Alignment Is Impossible

https://www.persuasion.community/p/ai-alignment-is-impossible
1•cdrnsf•6m ago•0 comments

Π0.7: A Steerable Model with Emergent Capabilities

https://www.pi.website/blog/pi07
1•aanet•7m ago•1 comments

Kimi 2.6 vs. Opus 4.7 and Cabbages

https://techstackups.com/comparisons/kimi-2.6-vs-opus-4.7-and-cabbages/
1•ritzaco•7m ago•0 comments

Muon G-2 Experiment Pioneers Win Breakthrough Prize in Fundamental Physics

https://home.cern/news/press-release/cern/muon-g-2-experiment-pioneers-win-breakthrough-prize-fun...
2•nhatcher•11m ago•0 comments

What Are We Doing?

2•chcardoz•11m ago•0 comments

Fail2Drive: Benchmarking Closed-Loop Driving Generalization

https://simonger.github.io/fail2drive/
2•mxfh•13m ago•0 comments

Can you make a picture of a dog wearing a hat?

https://dispatchesfromthefuture.substack.com/p/can-you-make-a-picture-of-a-dog-wearing
2•skandergarroum•14m ago•1 comments

The world wants Chinese tech. China is determined to keep it

https://www.economist.com/china/2026/04/20/the-world-wants-chinese-tech-china-is-determined-to-ke...
2•andsoitis•16m ago•0 comments

Anduril, Palantir and SpaceX are changing how America wages war

https://www.economist.com/business/2026/04/20/anduril-palantir-and-spacex-are-changing-how-americ...
15•andsoitis•16m ago•1 comments

The Code-Adjacent Power of AI

https://danielabaron.me/blog/the-code-adjacent-power-of-ai/
1•speckx•17m ago•0 comments

Teaching Claude CAD skills. Onshape MCP and visual reasoning tools

https://reshef.io/a/20260420_onshape_mcp/
1•ReshefElisha•17m ago•1 comments

No more Opus for Copilot Pro plan users

https://github.blog/changelog/2026-04-20-changes-to-github-copilot-plans-for-individuals/
6•guilamu•19m ago•1 comments

Show HN: A document snapshot solution to counter backups with name "Final_final"

https://apps.microsoft.com/detail/9nsd7jd58g17?hl=en-US&gl=US
1•hideload•23m ago•0 comments

Humanoid Robot Shatters Human Half-Marathon Record to Make History

https://hothardware.com/news/humanoid-robot-shatters-human-half-marathon-record
1•axelfontaine•23m ago•0 comments

American Library Association releases list of the most challenged books of 2025

https://text.npr.org/nx-s1-5791617
2•everdrive•24m ago•0 comments

The glance, and song, that could define the Premier League title race

https://www.cnn.com/2026/04/20/sport/premier-league-manchester-city-arsenal-erling-haaland
1•mooreds•24m ago•0 comments

US Class Action Suit over BrowserGate

https://browsergate.eu/updates/us-class-action-suit-over-browsergate-filed/
3•speckx•25m ago•0 comments

Show HN: Clock-MCP, stop guessing the time in AI

https://crates.io/crates/clock-mcp
1•DavidCanHelp•26m ago•1 comments

We got 207 tok/s with Qwen3.5-27B on an RTX 3090

https://github.com/Luce-Org/lucebox-hub
3•GreenGames•26m ago•1 comments