frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Exposing Claude Code Fake YouTubers (No Hate) [video]

https://www.youtube.com/watch?v=eh2OUZVQWdk
1•yunseo47•30s ago•0 comments

KeePassChi

https://codeberg.org/keepasschi
1•robin_reala•50s ago•0 comments

Production practices around a NestJS service

https://github.com/prod-forge/backend
1•alexsergey2•1m ago•1 comments

The Missing Context Layer: Why Your LLM Agent Can't Do More Than Text-to-SQL

https://airbyte.com/blog/the-missing-context-layer
1•eigenBasis•1m ago•0 comments

iPad is the best coding computer, which is why I always code on Mac

https://starikov.co/code-on-ipad/
1•iusevim•1m ago•0 comments

Kraken Refuses Extortion Demands After Criminal Group Films Internal Systems

https://www.blockhead.co/2026/04/14/kraken-refuses-extortion-demands-after-criminal-group-films-i...
1•thm•3m ago•0 comments

GenesisDB Syncra: Merge multiple event stores into one deterministic stream

https://syncra.genesisdb.io
5•patriceckhart•3m ago•0 comments

Ask HN: What standards or protocols exist for AI Agent permissions

1•lyfeninja•3m ago•0 comments

Claude Code Routines

https://code.claude.com/docs/en/routines
3•matthieu_bl•3m ago•0 comments

AI Approach Reveals Ocean Currents in Unprecedented Detail

https://today.ucsd.edu/story/new-ai-approach-reveals-ocean-currents-in-unprecedented-detail
1•gmays•3m ago•0 comments

An Oligarchy of Old People

https://www.theatlantic.com/magazine/2026/05/gerontocracy-wealth-power/686585/
3•sleepyguy•4m ago•1 comments

Mechanical Sympathy

https://vickiboykis.com/2026/04/13/mechanical-sympathy/
2•tosh•5m ago•0 comments

MCP Attack Atlas – 40 AI agent attack patterns catalogued

https://sunglasses.dev/mcp-attack-atlas
2•azrollin•5m ago•0 comments

Intel NPU based Speech To Text in any app at cursor

https://github.com/anubhavgupta/whisper-npu
2•anubhavgupta•6m ago•1 comments

Autonomous AI Agents Become Secure by Design with Nvidia OpenShell

https://blogs.nvidia.com/blog/secure-autonomous-ai-agents-openshell/
2•eigenBasis•7m ago•0 comments

Job Matcher – LLM-scored job listings from 10 boards, self-hosted

https://christopherbeaulieu.net/job-matcher/
2•cmb_dev•8m ago•0 comments

The toxic side of the Moon (2018)

https://www.esa.int/Science_Exploration/Human_and_Robotic_Exploration/The_toxic_side_of_the_Moon
3•tcp_handshaker•10m ago•0 comments

The State of Open Source Licensing in 2026

https://redmonk.com/sogrady/2026/03/25/open-source-licensing-2026/
2•Tomte•10m ago•0 comments

SHOW HN: DuckDB / DuckLake Server (With Arrow Flight SQL) for iOS

2•philbe77•11m ago•0 comments

Valve has just made it easier to run games on Linux with 8 GB cards

https://www.pcgamer.com/software/linux/a-valve-developer-has-just-made-it-easier-to-run-games-on-...
3•evo_9•11m ago•0 comments

Routines in Claude Code

https://claude.com/blog/introducing-routines-in-claude-code
3•meetpateltech•13m ago•0 comments

RAG-Forge – Framework-agnostic RAG toolkit with a maturity model

https://github.com/hallengray/rag-forge
2•hallengray•14m ago•0 comments

The Perimeter Problem

https://www.southwind.ai/blog/the-perimeter-problem
2•Eswo•14m ago•0 comments

Show HN: Java RocksDB Without JNI

https://github.com/dfa1/rocksdbffm
2•dfa11•14m ago•0 comments

I Discover New Blogs

https://kevquirk.com/how-i-discover-new-blogs
4•speckx•15m ago•0 comments

William Cecil's Succession Plan

https://www.historytoday.com/archive/history-matters/william-cecils-succession-plan
2•Petiver•15m ago•0 comments

Can LLMs Perform Synthesis?

https://arxiv.org/abs/2603.20264
3•PaulHoule•17m ago•0 comments

Add Animal Crossing events to your digital calendar

https://sethmlarson.dev/animal-crossing-calendar
2•SethMLarson•17m ago•0 comments

Show HN: Privacy-first retirement planner – no bank linking, AI-powered analysis

https://planwithclarity.app
2•smilano85•17m ago•0 comments

The problem with thinking you're part Neanderthal

https://www.technologyreview.com/2026/04/14/1135169/problem-thinking-part-neanderthal-human-evolu...
2•laurex•17m ago•1 comments