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.

Show HN: Antimemetic message that escapes screen capture

https://fx.leftium.com/static
1•Leftium•2m ago•0 comments

Eval: Quarkdown for Agentic Typesetting

https://quarkdown.com/blog/agent-skill/
1•iamgioh•3m ago•0 comments

'It's called winning': Why a tech industry super PAC is running ads about ICE

https://www.washingtonpost.com/technology/2026/05/23/ai-industry-super-pacs-are-intervening-midte...
1•1vuio0pswjnm7•3m ago•0 comments

Show HN: Cadence – Small marker tracking for source files

https://github.com/skorotkiewicz/cadence
1•modinfo•18m ago•0 comments

Toshifumi Suzuki, 7-Eleven Japan founder, has died

https://www.cnn.com/2026/05/25/asia/711-japan-founder-dies-intl-hnk
1•NaOH•20m ago•0 comments

Show HN: YieldOS-Lite – A simulator for LLM inference control-plane governance

https://github.com/nikitph/yieldos
1•loaderchips•22m ago•0 comments

QG: A speculative protocol treating presence and attention as value primitives

https://github.com/Mureskae/QG
1•Mureskae•24m ago•0 comments

My AI coding flow was burning tokens to do things code should do

https://geerttheys.substack.com/p/i-agent-deterministic-coding-flow
1•toadi•34m ago•1 comments

Hosting My Own Newsletter

https://endler.dev/2026/newsletter-setup/
1•yakkomajuri•34m ago•0 comments

Ask HN: Encouraging a child's gaming PC build despite fear of gaming addiction?

1•marttt•45m ago•1 comments

Over $5M in donations flowed in after the Lapu-Lapu Day attack. Where it went

https://www.cbc.ca/news/canada/lapu-lapu-donations-analysis-9.7207684
1•wolpoli•52m ago•0 comments

Huawei Unveils Tau (τ) Scaling Law for Transistor and System Breakthroughs

https://www.huawei.com/en/news/2026/5/ieee-iscas-tau-scaling
1•CalmStorm•53m ago•0 comments

Mecha Comet is an open-source hardware, modular Linux handheld computer

https://www.cnx-software.com/2026/01/25/mecha-comet-is-an-open-source-hardware-modular-linux-hand...
1•walterbell•55m ago•0 comments

Companies Are Just a Graph of Algorithms

https://danielmiessler.com/blog/companies-graph-of-algorithms
5•samuel246•56m ago•0 comments

CanYouCalculate

https://canyoucalculate.com
1•sauhard121•59m ago•0 comments

Porting Ytdlp to Bun (Ytdlb)

https://yamada-blog.pages.dev/blog/0007/
1•curliness•1h ago•0 comments

Software supply-chain attacks are no longer rare events

https://www.wired.com/story/teampcp-software-supply-chain-attack-spree-github/
2•latentframe•1h ago•1 comments

What is Git made of? (2022)

https://zserge.com/posts/git/
1•vinhnx•1h ago•0 comments

RLS sounds great until it isn't

https://planetscale.com/blog/rls-sounds-great-until-it-isntp
1•eigenBasis•1h ago•1 comments

Jira Is Turing-Complete

https://seriot.ch/computation/jira.html
2•vinhnx•1h ago•0 comments

Show HN: Live AI music sequencing agent

https://pretzel.shukant.com/?nickname=Anonymous&role=stage
1•shukantpal•1h ago•0 comments

A Cattle Ranch Is Doing What Ivy League Colleges Can't

https://www.nytimes.com/2026/05/20/opinion/deep-springs-college-ivy-league-education.html
1•gmays•1h ago•0 comments

The Eternal Sloptember

https://geohot.github.io//blog/jekyll/update/2026/05/24/the-eternal-sloptember.html
85•razin•1h ago•31 comments

My friend found idle NAT gateways his team said didn't exist

https://getnable.com/
1•chaandannn•1h ago•1 comments

AI Interpretability Is a Revolutionary Skill

https://www.outcryai.com/research/the-dark-between-the-stars
1•micahwhite•1h ago•1 comments

High-efficiency multi-scale holographic volumetric 3Dprinting with a phase light

https://www.nature.com/articles/s41377-026-02331-4
2•anikoghosyan•1h ago•0 comments

PaaS Platfrom to Deploy Apps

https://nept.cloud
1•nazmussamir•1h ago•0 comments

Command A+: Making sovereign agentic capabilities available to all

https://cohere.com/blog/command-a-plus
1•offbyone42•1h ago•0 comments

Splinter Cell veteran says realistic modern lighting has screwed up stealth game

https://www.rockpapershotgun.com/splinter-cell-veteran-says-realistic-modern-lighting-has-screwed...
4•Tomte•1h ago•0 comments

Weight loss drugs could save airlines money on fuel as Americans slim down

https://www.cbsnews.com/news/weight-loss-drugs-glp1s-airlines-fuel-costs/
1•mattas•1h ago•2 comments