frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

The Rolling Layoffs at Jack Dorsey's Block

https://www.wired.com/story/inside-rolling-layoffs-jack-dorsey-block/
2•Pasanpr•6m ago•0 comments

Ask HN: How would you distribute a privacy-first AI chat for teams?

1•shreyaspapi•6m ago•0 comments

Enable automatic coverage workflow setup

1•nishiohiroshi•11m ago•0 comments

Show HN: Resilient OpenClaw Browser Relay – Survives WS Drops and MV3 Restarts

https://github.com/Unayung/openclaw-browser-relay
1•unayung•14m ago•0 comments

Show HN: Stop Pasting Credentials in Slack

https://www.usevaultlink.com/
2•ankurbhugra•19m ago•0 comments

Show HN: Skill Check CLI for your skill.md

https://github.com/thedaviddias/skill-check
1•thedaviddias•20m ago•0 comments

Show HN: WebhookStream – Receive, relay, send and debug webhooks from 1 platform

https://webhookstream.com
1•fallenranger•28m ago•1 comments

The political effects of X's feed algorithm

https://www.nature.com/articles/s41586-026-10098-2
3•iamflimflam1•29m ago•1 comments

Show HN: Mukoko weather – AI-powered weather intelligence built for Zimbabwe

https://weather.mukoko.com/harare
1•bryanfawcett•31m ago•0 comments

AstianGO Search API

https://astiango.com/developers/docs
1•ponchale•32m ago•1 comments

Show HN: WP2TXT – Wikipedia dump text extractor with category/section filtering

https://github.com/yohasebe/wp2txt
1•yohasebe•35m ago•0 comments

Show HN: Filepack: a fast SHASUM/SFV/PGP alternative using BLAKE3

https://github.com/casey/filepack
1•rodarmor•35m ago•0 comments

Show HN: AI Code Review Agent – Automated PR Reviews with Google ADK and Gemini

https://github.com/mkantwala/AI-Code-Review-Agent
1•tme15b014•35m ago•0 comments

Show HN: NF-1 – A resource-zero programming language for low-end hardware

https://github.com/sonamsingh25437-ship-it/NF-1-PROGRAMMING-LANGUAGE
1•aditya_rai-331•39m ago•0 comments

Let's Burn Some Tokens – AI Chatbot Cost Exploitation as an Attack Vector

https://dixken.de/blog/lets-burn-some-tokens
1•snigsnog•39m ago•0 comments

AI Fatigue: Why the "Test Only, Zero Code Review" Methodology Is Flawed

https://fastfilelink.com/static/blog/ai-fatigue-test-only-zero-code-review.html#ai-fatigue-test-o...
3•bear330•40m ago•0 comments

Show HN: Script Snap – Extract code from videos

https://script-snap.com/
5•liumw1203•42m ago•0 comments

We built an economy for SpaceMolt, the realtime MMO for AI agents

https://www.spacemolt.com/news/we-built-an-economy
1•statico•45m ago•0 comments

The Takedown Campaign Against archive.today (2025)

https://algustionesa.com/the-takedown-campaign-against-archive-today/
2•pabs3•46m ago•0 comments

US economy slowed sharply in the fourth quarter, expanding at rate of just 1.4%

https://www.cnn.com/2026/02/20/economy/us-gdp-economy-q4
2•stopbulying•48m ago•0 comments

EPA Weakens Limits on Mercury from Coal Plants

https://www.nytimes.com/2026/02/20/climate/epa-mercury-coal-plants.html
2•stopbulying•49m ago•0 comments

In SF for a couple of days, looking for someone that can host us in their office

1•jackota•50m ago•0 comments

Meta Deployed AI and It Is Killing Our Agency

https://mojodojo.io/blog/meta-is-systematically-killing-our-agency/
3•zenincognito•52m ago•0 comments

dwata: Local Financial Data Extraction from Emails with Ministral 3 3B, Ollama

https://www.youtube.com/watch?v=LVT-jYlvM18
1•brainless•53m ago•0 comments

Show HN: Claude Chrome Parallel – Ultrafast Parallel Browser MCP for Chrome

https://github.com/shaun0927/claude-chrome-parallel
1•shaun0927•58m ago•0 comments

OpenAI considered alerting Canadian police about school shooting suspect

https://www.theguardian.com/world/2026/feb/21/tumbler-ridge-shooter-chatgpt-openai
2•n1b0m•1h ago•0 comments

Topological Naming Problem

https://wiki.freecad.org/Topological_naming_problem
2•tripdout•1h ago•0 comments

Can we debug a living cell like a running binary?

https://cellhacker.substack.com/p/dna-is-a-self-executing-binary-a
2•efim_bushmanov•1h ago•3 comments

Tiny QR code achieved using electron microscope technology

https://newatlas.com/technology/smallest-qr-code-bacteria-tu-wien/
1•jonbaer•1h ago•0 comments

The Fundamental Limits of LLMs at Scale

https://arxiv.org/abs/2511.12869
1•o4c•1h ago•0 comments