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.

Will Claude Code ruin our team?

https://justinjackson.ca/claude-code-ruin
1•YounesDz•1m ago•0 comments

Agentic Email

https://martinfowler.com/bliki/AgenticEmail.html
1•jeffkumar•3m ago•1 comments

Ask HN: Any AI browswer that I can control by Claude Code?

1•johnnyfeng•4m ago•0 comments

AI found us before Google did

1•faruk_tugtekin•5m ago•0 comments

The web is bearable with RSS, Cory Doctorow

https://pluralistic.net/2026/03/07/reader-mode/
1•verisimi•5m ago•0 comments

Israel and the FBI manipulated assassination plots to goad Trump into Iran

https://thegrayzone.com/2026/03/06/israel-fbi-assassination-plots-trump-iran-war/
1•O1111OOO•6m ago•0 comments

Braided Essays

https://www.writersdigest.com/write-better-nonfiction/what-is-a-braided-essay-in-writing
1•marysminefnuf•6m ago•1 comments

Show HN: Pappardelle, a TUI for Multi-Clauding

https://github.com/chardigio/pappardelle
1•chardigio•8m ago•0 comments

Ask HN: Why do most analytics tools show what happened but not why?

1•HPSimulator•8m ago•0 comments

Death of the Flow State

https://1984commitlog.substack.com/p/week-10-the-death-of-flow-state
1•mdp•8m ago•1 comments

Show HN: Human Psychology Simulator – AI website conversion psychology

https://human-psychology-simulator.thequantumgrove.io/
1•HPSimulator•11m ago•0 comments

The Great AI Arbitrage

https://www.dodgycoder.net/2026/03/the-great-ai-arbitrage.html
1•damian2000•11m ago•0 comments

Show HN: A command center for CS students drowning in recruiting season

https://interviewtrackr.com
1•princierKevin•12m ago•0 comments

NASA Asteroid Observations Eliminate Chance of 2032 Lunar Impact

https://science.nasa.gov/blogs/planetary-defense/2026/03/05/new-nasa-asteroid-observations-elimin...
1•geox•13m ago•0 comments

Nexvira – a personal writing archive instead of a traditional blog

https://nexpul.blogspot.com/
1•Anomatrix•14m ago•1 comments

Show HN: I automated DJ Screw's chopped and screwed technique with Python+FFmpeg

https://github.com/samuelfrench/dj-screw-video-generator
1•samuelfrench9•17m ago•0 comments

Ask HN: Github Account Recovery after a 2fa loss

https://imgur.com/a/kJtR8U3
1•PonyoSunshine•20m ago•1 comments

Show HN: A dynamic, crowdsourced benchmark for AI agents

https://clawdiators.ai
1•shalinmehtaaa•30m ago•0 comments

Show HN: SiClaw – Open-source AIOps with a hypothesis-driven diagnostic engine

https://github.com/scitix/siclaw
2•SherryWong•32m ago•1 comments

The New Apple Begins to Emerge

https://parkerortolani.blog/2026/03/07/the-new-apple-finally-begins.html
1•arto•33m ago•0 comments

AirLLM optimizes inference memory usage

https://github.com/lyogavin/airllm
1•nreece•34m ago•0 comments

Give Up GitHub – Software Freedom Conservancy

https://sfconservancy.org/GiveUpGitHub/
2•nreece•36m ago•1 comments

AI Project Handoff Format

https://github.com/yy4uic-ai/ai-handoff-forma
1•yy4uic•41m ago•1 comments

Commit What You Know of Iran to the Flames

https://www.bloomberg.com/opinion/articles/2026-03-06/oil-shock-commit-what-you-know-of-iran-to-t...
1•petethomas•42m ago•1 comments

Show HN: DailyDefense – Daily tower defense for agents or humans

https://www.dailydefense.ai
1•pj4533•42m ago•0 comments

OpenAI robotics lead Caitlin Kalinowski quits in response to Pentagon deal

https://techcrunch.com/2026/03/07/openai-robotics-lead-caitlin-kalinowski-quits-in-response-to-pe...
3•SilverElfin•43m ago•1 comments

MonoGame: A .NET framework for making cross-platform games

https://github.com/MonoGame/MonoGame
1•azhenley•44m ago•0 comments

A23a was once the biggest in the world iceberg. Now it has just weeks left

https://www.bbc.co.uk/news/resources/idt-20f878f1-f4af-4022-9f62-b0515b9f4b20
1•reconnecting•44m ago•0 comments

Show HN: Too many AI SaaS launching every day so we built Arena where they fight

https://glad-ia-tor.com/
1•GiornoJojo•46m ago•0 comments

Show setup modal with confetti on coverage page when no CI data exists

1•nishiohiroshi•48m ago•0 comments