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.

Practical .NET Coding Guidelines We Use Internally

https://github.com/justinamiller/DotNet-Coding-Guidelines
1•maverickeye•1m ago•1 comments

Steam Machine price leak shakes the console market

https://comuniq.xyz/post?t=696
1•01-_-•1m ago•0 comments

Iranian regime tries to shut down Starlink

https://www.timesofisrael.com/iran-appears-to-jam-starlink-after-shutting-down-comms-networks/
4•ukblewis•8m ago•0 comments

Backing the Backslash

https://shadycharacters.co.uk/2025/03/backing-the-backslash/
1•everybodyknows•15m ago•0 comments

Elon Musk on Tesla's summon – LA to NY in 2 years (2016 – 10 years anniversary)

https://twitter.com/elonmusk/status/686279251293777920
2•TheAlchemist•23m ago•0 comments

Show HN: Keyboard-first diagram editor in Rust with fzf-style command palette

https://github.com/joonho3020/sansuyu
1•archipelago123•23m ago•0 comments

Biological and artificial consciousness: A case for biological computationalism

https://www.sciencedirect.com/science/article/pii/S0149763425005251
3•galaxyLogic•25m ago•0 comments

We Put Claude Code in Rollercoaster Tycoon

https://ramplabs.substack.com/p/ai-plays-rollercoaster-tycoon
2•gwintrob•27m ago•0 comments

Words

https://justinjackson.ca/words.html
1•Tomte•28m ago•0 comments

Torvalds: Another silly guitar-pedal-related repo

https://github.com/torvalds/AudioNoise/blob/71b256a7fcb0aa1250625f79838ab71b2b77b9ff/README.md
2•m-hodges•28m ago•1 comments

If I search for "opencode GitHub" in Bing, a random fork is returned

https://www.bing.com/search?q=opencode+github&PC=U316
1•theanonymousone•29m ago•0 comments

Yeast Programmed for Opioid Total Synthesis

https://cen.acs.org/articles/93/i49/Yeast-Programmed-Opioid-Total-Synthesis.html
1•slow_typist•34m ago•0 comments

Google employee made redundant after reporting sexual harassment, court hears

https://www.bbc.co.uk/news/articles/c62v51d1ry2o
3•latein•34m ago•0 comments

HeyToken – Access all LLMs for 30% less via a unified API

https://heytoken.ai
1•alhazar•38m ago•1 comments

Create Google API credentials in 50 easy steps

https://github.com/glotlabs/gdrive/blob/main/docs/create_google_api_credentials.md
1•ukuina•40m ago•0 comments

Setting Up OpenCode with Local Models

https://theaiops.substack.com/p/setting-up-opencode-with-local-models
1•ramikrispin•42m ago•0 comments

Amazon Redshift AutoWLM and SQA internals plus commentary (and a bit on CSC)

1•Max-Ganz-II•44m ago•0 comments

LitePoint clears testing milestone with Qualcomm's upcoming Wi-Fi 8 platform

https://www.rcrwireless.com/20260109/test-measurement/ces-2026-litepoint-clears-testing-milestone...
1•lordwiz•44m ago•0 comments

UI Skills

https://www.ui-skills.com/
2•handfuloflight•46m ago•0 comments

Eat More Deer

https://www.theatlantic.com/health/2026/01/deer-hunting-venison-sale/685537/
3•thunderbong•48m ago•0 comments

The Score

https://www.penguin.co.uk/books/457380/the-score-by-nguyen-c-thi/9780241653975
2•molteanu•50m ago•0 comments

Anthropic: Demystifying Evals for AI Agents

https://www.anthropic.com/engineering/demystifying-evals-for-ai-agents
3•Bayram•52m ago•1 comments

New Game Launcher for PC Games

https://www.gamekolektor.com/
1•ravioldevuy•52m ago•1 comments

Show HN: MCP for browsing, searching, exporting, backing up Cursor chat history

https://github.com/S2thend/cursor-history-mcp
2•mikasisiki•1h ago•2 comments

Signal Chat: Silicon Valley Is Plotting Against California's Billionaire Tax

https://www.wsj.com/politics/policy/save-california-billionare-tax-814a2fe9
5•1vuio0pswjnm7•1h ago•1 comments

Obscene images: X admits its mistake and stated it would comply with Indian law

https://timesofindia.indiatimes.com/india/obscene-image-row-x-admits-mistake-thousands-of-posts-o...
4•throwaway110001•1h ago•0 comments

Photos of the Forgotten

https://www.synthetrix.com/potf.html
2•exvi•1h ago•0 comments

'Fuck You, Make Me' Without Saying the Words

https://daringfireball.net/2026/01/fuck_you_make_me_without_saying_the_words
8•nopakos•1h ago•2 comments

Show HN: I built an Instagram-style productivity app with gamification

https://apps.apple.com/us/app/lockin-social-productivity/id6742454713
2•mandynoee•1h ago•1 comments

The Concise TypeScript Book

https://github.com/gibbok/typescript-book
23•javatuts•1h ago•1 comments