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.

Show HN: Crypto data API where AI agents pay per request with USDC (x402)

https://crypto-enrich.up.railway.app
1•psamala•35s ago•0 comments

The first AI counter surveillance app

https://play.google.com/store/apps/details?id=app.sentryrf&hl=en_US
1•vidoluc•1m ago•1 comments

Loop Conference Channel [YouTube]

https://www.youtube.com/channel/UC_QIfHvN9auy2CoOdSfMWDw
1•vinhnx•2m ago•0 comments

The Mystery of Asjo.org

https://acid.vegas/blog/the-mystery-of-asjo-org/
1•gzread•4m ago•0 comments

How College Admissions Officers Spot Over-Coached Applications

https://www.forbes.com/sites/christopherrim/2026/02/27/how-college-admissions-officers-spot-over-...
1•paulpauper•5m ago•0 comments

Our Hospice System Subverts the Point of Hospice Care

https://www.nytimes.com/2026/03/02/opinion/hospice-care.html
1•paulpauper•5m ago•0 comments

SEIU Delenda Est

https://www.astralcodexten.com/p/seiu-delenda-est
2•paulpauper•7m ago•0 comments

Tell HN: Azure Data Factory pipeline execution delays in East US 2

1•dwoldrich•8m ago•0 comments

Show HN: ByeBrief – a local-first AI investigation canvas

https://github.com/byte271/ByeBrief/
1•yihac1•9m ago•0 comments

The Differentiated Engineer in the Era of Automated Development

https://substack.com/home/post/p-190017259
1•Carsten_Peters•9m ago•0 comments

Defense Devaluation – Starlink on American Drones

https://en.topwar.ru/278903-devalvacija-oborony-starlink-na-amerikanskih-dronah.html
1•B1FF_PSUVM•9m ago•0 comments

India Plans 30% Slash in Thermal Coal Imports This Year

https://oilprice.com/Latest-Energy-News/World-News/India-Plans-30-Slash-in-Thermal-Coal-Imports-T...
1•PaulHoule•9m ago•0 comments

I made a programming language with M&Ms

https://mufeedvh.com/posts/i-made-a-programming-language-with-mnms/
1•mufeedvh•10m ago•0 comments

Show HN: MysteryMaker AI

https://www.mysterymaker.ai
1•jhappy77•13m ago•0 comments

Peer-to-Peer Networking: Build a VPN Tunnel with Wintun on Windows – Part 2

https://www.0xmm.in/posts/peer-to-peer-windows-part2/
1•melson•19m ago•0 comments

UUID package coming to Go standard library

https://github.com/golang/go/issues/62026
2•soypat•20m ago•0 comments

US draws up strict new AI guidelines amid Anthropic clash

https://www.reuters.com/business/media-telecom/us-draws-up-strict-new-ai-guidelines-amid-anthropi...
4•ericsaf•21m ago•2 comments

T3 Code – a new OSS agentic coding app that wraps Codex

https://t3.codes/
1•theobr•21m ago•0 comments

Show HN: HyperClaw – self-hosted AI assistant that replies on Telegram/Discord/+

https://github.com/mylo-2001/hyperclaw
1•mylw•22m ago•0 comments

Rust 1.94.0

https://blog.rust-lang.org/2026/03/05/Rust-1.94.0/
1•tahazsh•27m ago•0 comments

Natural Language AutoCoder Open SOurce

https://github.com/tommoirnz/NLAutocoder
1•moirnz•31m ago•1 comments

Show HN: Claude-consensus – Multi-model code review plugin for Claude Code

https://github.com/AltimateAI/claude-consensus
1•k_git•32m ago•0 comments

BYD unveils Blade Battery 2.0: 10-70% in 5 mins, 10-97% in 9 mins

https://carnewschina.com/2026/03/05/byd-unveils-blade-battery-2-0-10-70-in-5-mins-10-97-in-9-mins...
1•xxfye•32m ago•0 comments

Show HN: Copyworks – Chinese character worksheets with tone colors

https://copyworks.loqu8.com
1•loqu8•36m ago•0 comments

Saulala

https://www.saulala.com/
2•matthberg•37m ago•0 comments

Qatar warns war will force Gulf to stop energy exports 'within days'

https://www.ft.com/content/be122b17-e667-478d-be19-89d605e978ea
5•geox•42m ago•0 comments

FASTEST LLM decode engine on Apple Silicon. 658 tok/s on M4-Max,beats MLX by 19%

https://www.runanywhere.ai/blog/metalrt-fastest-llm-decode-engine-apple-silicon
2•sanchitmonga•43m ago•2 comments

T3 Code: A Minimal Web GUI/Desktop App for Coding Agents

https://github.com/pingdotgg/t3code
1•vldszn•44m ago•0 comments

I built a database of verified YouTube channel revenues

https://ytmrr.com/
1•poissac•45m ago•1 comments

Cancellation of Army exercise fuels speculation about Mideast troop deployments

https://www.washingtonpost.com/national-security/2026/03/06/army-82nd-airborne-iran/
5•ParentiSoundSys•51m ago•0 comments