frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Stop using MySQL in 2026, it is not true open source

https://optimizedbyotto.com/post/reasons-to-stop-using-mysql/
1•gpi•2m ago•0 comments

Ask HN: Strongman Argument for Trump?

1•bbiab•3m ago•0 comments

Ripped off by Google? Places API

1•texuf•4m ago•0 comments

OpenAI is now selling 6x more codex for 10x the price

https://developers.openai.com/codex/pricing/
1•Szpadel•4m ago•1 comments

Eternal September

https://en.wikipedia.org/wiki/Eternal_September
1•_vaporwave_•5m ago•0 comments

New Model Fails to Explain Near-Death Experiences, Scientists Say

https://news.med.virginia.edu/research/new-model-fails-to-explain-near-death-experiences-scientis...
1•XzetaU8•6m ago•0 comments

Yori, I made a CLI tool that compiles natural language into C++ binaries

https://github.com/alonsovm44/yori
1•alonsovm•12m ago•1 comments

All horses are the same color

https://en.wikipedia.org/wiki/All_horses_are_the_same_color
1•icwtyjj•15m ago•0 comments

Curl: We stop the bug-bounty end of Jan 2026

https://github.com/curl/curl/pull/20312
2•Fiveplus•16m ago•0 comments

Growth / Decline of various mobile app SDKs

https://appgoblin.info/reports/mobile-apps-growth-sdks-2025
1•ddxv•23m ago•1 comments

Grok and the A.I. Porn Problem

https://www.newyorker.com/culture/infinite-scroll/grok-and-the-ai-porn-problem
1•fortran77•24m ago•1 comments

How to Debug Your Life

https://www.joanwestenberg.com/how-to-debug-your-life/
1•zdw•24m ago•0 comments

China's Zhipu Unveils New AI Model Trained on Huawei's Chips

https://www.bloomberg.com/news/articles/2026-01-14/china-s-zhipu-unveils-new-ai-model-trained-on-...
2•antman•26m ago•0 comments

Rare images of Europe's 'ghost cat' [wildcat]

https://www.bbc.com/future/article/20260112-rare-images-of-europes-ghost-cat
2•7402•27m ago•0 comments

Tech Writers Are About to Become Obsolete

https://kibbler.dev/blog/turn-your-codebase-into-a-knowledge-base
6•kewun•30m ago•11 comments

Two Indian students won a $200k settlement with US university

https://indianexpress.com/article/cities/delhi/a-lunch-of-palak-paneer-and-how-two-indian-student...
1•akbarnama•32m ago•0 comments

Temporal Patterns

https://martinfowler.com/eaaDev/timeNarrative.html
1•vulcan01•33m ago•0 comments

As birthrates tumble, progressives say the left needs to offer ideas, solutions

https://www.npr.org/2026/01/12/nx-s1-5637424/birthrate-population-babies
2•andsoitis•34m ago•0 comments

Tesla to stop selling FSD package, moves to subscription-only: why a big move

https://electrek.co/2026/01/14/tesla-tsla-stop-selling-full-self-driving-package-subscription-only/
1•FabHK•38m ago•0 comments

Viking Solar Navigation Modernized (DIY)

https://www.youtube.com/watch?v=4ERqAqwQ1wo
1•gsf_emergency_6•41m ago•0 comments

Show HN: Calculator to compare Startup offers vs. Big Tech comp

https://shouldijoinastartup.com
1•febtiger•42m ago•0 comments

Supernatural VR will no longer get new content or features

https://www.uploadvr.com/supernatural-coaches-face-same-fate-as-oculus-home-echo-arena/
1•guiambros•44m ago•1 comments

Handy – free open source speech-to-text app

https://github.com/cjpais/Handy
2•tin7in•45m ago•0 comments

Ask HN: How to predict nums of output tokens?

1•ssslvky1•47m ago•1 comments

Mistral Vibe – Minimal CLI Coding Agent

https://github.com/mistralai/mistral-vibe
1•tin7in•50m ago•0 comments

Show HN: Fine-tuned Qwen2.5-7B on 100 films for probabilistic story graphs

https://cinegraphs.ai/
1•graphpilled•53m ago•0 comments

Pocket TTS: A high quality TTS that gives your CPU a voice

https://kyutai.org/blog/2026-01-13-pocket-tts
3•pain_perdu•54m ago•0 comments

Conductor: Context-driven development for Gemini CLI

https://developers.googleblog.com/conductor-introducing-context-driven-development-for-gemini-cli/
1•geoffbp•57m ago•0 comments

Just the Browser

https://justthebrowser.com/
1•vinhnx•58m ago•1 comments

Claude Code Tool Search Tool

https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool
1•tin7in•59m ago•0 comments