frontpage.
newsnewestaskshowjobs

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.

Show HN: Analysis of Games from Chess Olympiad with Opus 5.5 and Stockfish

https://olympiad2026.unremarkable.info/
1•myle•3m ago•0 comments

Microsoft disrupts AI-assisted platform that compromised 12,000 accounts

https://arstechnica.com/security/2026/09/microsoft-disrupts-ai-assisted-platform-that-compromised...
2•joozio•4m ago•0 comments

Why Tool AIs Want to Be Agent AIs

https://gwern.net/tool-ai
1•duck•9m ago•0 comments

Fearless SIMD v1.0 is here

https://linebender.org/blog/fearless-simd-1-0/
1•AndrewDucker•9m ago•0 comments

M6 Mac Mini uses soldered NAND storage again

https://www.macrumors.com/2026/09/22/new-mac-mini-ssd-cant-be-removed/
1•gloxkiqcza•10m ago•0 comments

Show HN: Yass – Yet Another Spec Syntax Enters Beta

https://github.com/shakefu/yass
1•bilbo-b-baggins•10m ago•0 comments

Building a docs site that screenshots itself

https://semaloop.com/blog/docs-that-screenshot-themselves
1•samstarling•14m ago•1 comments

There is no epidemic of loneliness, but there is an epidemic of scurvy

https://www.experimental-history.com/p/there-is-no-epidemic-of-loneliness
1•luu•17m ago•0 comments

Claude Code Hooks Explained: The Deterministic Layer Around Your Agent

https://blakecrosley.com/blog/claude-code-hooks-explained
1•northbridgedev•18m ago•0 comments

Jevify skill – Gets your existing agents running on Jev

2•soupz011•19m ago•0 comments

Show HN: use-citations – Claude Code skill for citation backed answers

https://github.com/sshah03/use-citations
1•sshah03•28m ago•0 comments

Show HN: Daily-updated open dataset of flight fare history by route and month

https://github.com/DeusAcc/flight-fare-history
1•deusautoai725•29m ago•0 comments

CobaltC now has a compiler (for Linux)

https://github.com/strawberry9/CobaltC
1•SilentLambda•34m ago•0 comments

First Petabit-Class Transoceanic Subsea Cable

https://engineering.fb.com/2026/09/21/connectivity/petal-petabit-transoceanic-subsea-cable/
1•tosh•37m ago•0 comments

Ask HN: Engineers on highly polished products, how are you using agentic coding?

3•PaulStatezny•40m ago•1 comments

Which AI model should your team use for Ansible

https://steampunk.si/spotter/ai-leaderboard/
2•xlab-si•40m ago•0 comments

Unreal Agent An async-first agent harness

https://github.com/unreallabsai/unreal-agent
1•Bluestein•46m ago•0 comments

Transformers now runs llama.cpp quants

https://huggingface.co/blog/transformers-llama-cpp-quants
3•mmis1000•47m ago•0 comments

Australia's giant data-centre boom

https://www.economist.com/asia/2026/09/21/australias-giant-data-centre-boom
2•burntcaramel•52m ago•1 comments

Avhash: BlurHash replacement, using a AV1 frame with container headers stripped

https://github.com/keithamus/avash
2•DamonHD•55m ago•0 comments

Show HN: Ceptile AI Search platform which combines web results with AI chat

https://www.ceptile.com/
1•ShauryaAgarwalr•56m ago•0 comments

Claude Opus 5.5 – Pelican Game

https://claude-opus-5-5.riba2534.cn/
3•denysvitali•59m ago•2 comments

I Gave My Life over to Meta's A.I. Agent and Was Blown Away

https://www.nytimes.com/2026/09/22/technology/meta-muse-ai-agent.html
3•geoffbp•59m ago•1 comments

El Niño projected to cause 450k deaths in next six months

https://www.theguardian.com/environment/2026/sep/23/el-nino-450000-extra-deaths-six-months
6•ljf•1h ago•1 comments

Mastering LLM Inference Optimization

https://machinelearningmastery.com/the-roadmap-to-mastering-llm-inference-optimization/
2•geoffbp•1h ago•0 comments

The Rio Grande's pecan problem (2025)

https://www.hcn.org/issues/57-9/the-rio-grandes-pecan-problem/
2•littlexsparkee•1h ago•0 comments

Show HN: Visually orchestrate Claude Code AI agents

https://github.com/rondoflow/rondoflow
2•bdearch•1h ago•0 comments

Benzi – Code Intelligence MCP/Harness beating Claude Code and CodeGraph

https://varianttech.net
2•tweedler290•1h ago•1 comments

A Summer of AI Optimization

https://lemire.me/blog/2026/09/22/a-summer-of-ai-optimization/
2•signa11•1h ago•0 comments

Serious editors are a commitment (at least for me)

https://utcc.utoronto.ca/~cks/space/blog/tech/EditorsAreACommitment
2•signa11•1h ago•1 comments