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.

Dissociating Direct Access from Inference in AI Introspection

https://arxiv.org/abs/2603.05414
1•3willows•2m ago•0 comments

Show HN: I vibe-coded the chopsticks faux pas app for you so you don't have to

https://hashihabits.com
1•stunnAR•12m ago•0 comments

Show HN: I Built Grid View for Switchboard – Claude Code CLI Manager

https://github.com/doctly/switchboard/releases/tag/v0.0.16
1•kapitalx•14m ago•0 comments

Graphics Gems Repository (2021)

https://www.realtimerendering.com/resources/GraphicsGems/
1•turtleyacht•15m ago•0 comments

T0-GPU: Bare-metal kernel compiler for RDNA3

https://github.com/GeisYaO/t0-gpu
1•woctordho•16m ago•0 comments

Goalless AI Agents: What They Build When No One Is Watching

https://changkun.substack.com/p/goalless-agents
1•changkun•16m ago•0 comments

Using AI makes writing more bland, study finds

https://www.nbcnews.com/tech/tech-news/ai-changing-style-substance-human-writing-study-finds-rcna...
1•cdrnsf•17m ago•0 comments

Crisis Engineering: The Book

https://crisisengineering.layeraleph.com/crisis-engineering-the-book/
1•riknos314•18m ago•0 comments

Humans share acoustic preferences with other animals: study

https://www.mcgill.ca/newsroom/channels/news/humans-share-acoustic-preferences-other-animals-stud...
1•hhs•18m ago•0 comments

Adapt or Die

https://www.youtube.com/watch?v=k51k2AyUpao
1•vinhnx•22m ago•0 comments

Storing 2 bytes of data in your Logitech mouse

https://www.timwehrle.de/blog/what-if-i-stored-data-in-my-mouse/
4•birdculture•25m ago•0 comments

Creating prediction markets with Tarek Mansour and Luana Lopes Lara

https://cheekypint.substack.com/p/creating-prediction-markets-and-suing
1•hhs•26m ago•0 comments

In this economy? Sydney In Me has created a free tool using free services

https://taplink.cc/sydneyinme
1•iiviivix•29m ago•1 comments

Looking at Unity made me understand the point of C++ coroutines

https://mropert.github.io/2026/03/20/unity_cpp_coroutines/
1•ingve•33m ago•0 comments

Inside the AI labs training China's humanoid robots

https://www.ft.com/content/85bca5c7-f64b-4011-bc7c-9ce3254a2b78
2•hhs•36m ago•0 comments

WikiWikiWiki

https://github.com/minguhong/WikiWikiWiki
2•cookingoils•38m ago•0 comments

AI systems are more similar to indent(1), than to the human process

https://marc.info/?l=openbsd-tech&m=177411863202734&w=2
2•longislandguido•48m ago•1 comments

I built a FIX protocol engine in Rust that's 4.5x faster than QuickFIX/J

https://github.com/matthart1983/velocitas-fix-engine
2•matthart1983•48m ago•0 comments

B_slope: Factual answers converge 1.4x–2.6x faster than hallucinations

https://www.orsonai.com/publications/tes1-pre-generative-epistemic-signal.html
1•JakubCwi•51m ago•0 comments

Management Craft

https://www.managementcraft.co/
1•handfuloflight•51m ago•0 comments

AI for Particle Physics: Searching for Anomalies

https://spectrum.ieee.org/particle-physics-ai
1•rbanffy•55m ago•0 comments

Martial Arts Robots

https://www.youtube.com/watch?v=UTq2o7RQs0A
1•M4v3R•55m ago•0 comments

AI Chatbots Keep Encouraging Violence. That's by Design

https://weaponizedspaces.substack.com/p/ai-chatbots-keep-encouraging-violence
2•rbanffy•57m ago•0 comments

How are you so sure this is not just another winter

3•shoman3003•57m ago•1 comments

Ask HN: Latitude.sh / datapacket.com for bare metal servers?

1•truetraveller•59m ago•1 comments

PyTorch Init Functions Explained Visually [video]

https://www.youtube.com/shorts/pp6jV-sd_a0
1•0bytematt•1h ago•0 comments

Show HN: Cycle – inverse-Pomodoro for macOS that nags you to rest

https://github.com/saint-angels/CYCLE
1•saint_angels•1h ago•0 comments

George R. R. Martin Is "Not in the Mood" to Finish the Winds of Winter

https://www.esquire.com/entertainment/books/a64917333/george-rr-martin-the-winds-of-winter-update...
2•randycupertino•1h ago•2 comments

Yeah: LLM-powered yes/no CLI tool

https://github.com/crawshaw/yeah
4•super_linear•1h ago•0 comments

Fun with CSF firmware (RK3588 GPU firmware)

https://icecream95.gitlab.io/fun-with-csf-firmware.html
2•M95D•1h ago•0 comments