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.

AWS has officially removed all EC2 instances in Bahrain from their docs

https://twitter.com/astuyve/status/2039777883485254081
1•mirzap•44s ago•0 comments

Agent-Friendly Documentation Spec

https://agentdocsspec.com/
1•nlawalker•1m ago•0 comments

Tehran Accuses Ukraine of Active Participation in Iran War

https://www.kyivpost.com/post/72965
1•alephnerd•2m ago•0 comments

The Path Forward for WordPress 7.0

https://make.wordpress.org/core/2026/04/02/the-path-forward-for-wordpress-7-0/
1•jantissler•2m ago•0 comments

Are still using copy/pasting in GPT to correct your text?

https://rewritecmd.com/
1•Louis9•4m ago•0 comments

ON1 Restore AI Turns Old Family Photos into Grotesque Nightmare Fuel

https://petapixel.com/2026/03/25/on1-restore-ai-turns-old-family-photos-into-grotesque-nightmare-...
1•My_Name•4m ago•0 comments

Show HN: Run Claude Code, Codex, and more with auto-approval in a Container

https://github.com/VibePod/vibepod-cli
1•nezhar•5m ago•0 comments

Spyware Vendor Creates Fake WhatsApp App

https://securityaffairs.com/190276/malware/italian-spyware-vendor-creates-fake-whatsapp-app-targe...
1•lschueller•5m ago•0 comments

Velvetluxbox

https://bdsm-box.com/
1•Velvetluxbox•5m ago•0 comments

Show HN: Hey Aio – Video Editing Assistant You Can Talk To

https://www.heyaio.com/
1•misellu•7m ago•0 comments

Hallucinated citations are polluting scientific literature. What can be done?

https://www.nature.com/articles/d41586-026-00969-z
2•Kaibeezy•10m ago•2 comments

What the FCC router ban means for FOSS

https://sfconservancy.org/blog/2026/apr/02/fcc-router-ban/
1•speckx•11m ago•0 comments

The Friend Compound Field Guide

https://livenearfriends.com/fieldguide
1•cjbarber•12m ago•0 comments

Public Anger Is Rising

https://www.theatlantic.com/politics/2026/04/congress-government-shutdown-tsa/686653/
2•paulpauper•14m ago•0 comments

Open Source 13" E-Ink Spectra Color Display with ESP32-S3

https://www.crowdsupply.com/soldered/inkplate-13spectra
2•car•15m ago•0 comments

Show HN: I built an offline and privacy-first photo sorter

https://photosort-production-d4d1.up.railway.app/
2•Sensotix•16m ago•1 comments

Show HN: Is autoresearch better than classic hyperparameter tuning?

https://www.weco.ai/blog/autoresearch-vs-classical-hpo
2•WecoAI•16m ago•0 comments

Show HN: Claude Code leak forced this early – Dojo dual agents vs. Claude/Codex

https://ehaye.io
1•avidcoder•16m ago•0 comments

Epstein files: Buffett says he hasn't talked to Bill Gates

https://www.cnbc.com/2026/03/31/warren-buffett-bill-gates-epstein.html
1•paulpauper•17m ago•0 comments

Anthropic says its leak-focused DMCA unintentionally hit legit GitHub forks

https://arstechnica.com/ai/2026/04/anthropic-says-its-leak-focused-dmca-effort-unintentionally-hi...
1•josephcsible•18m ago•1 comments

Show HN: AutoLoop – Let coding agents run optimization loops on real repos

https://github.com/armgabrielyan/autoloop
1•armen99•18m ago•0 comments

Oldest known tortoise still alive, as reports of death revealed as hoax

https://www.bbc.com/news/articles/c393xmpzjwko
2•BeetleB•20m ago•0 comments

Marc Andreessen Is Right That AI Isn't Killing Jobs. Interest Rate Hikes Are

https://www.governance.fyi/p/marc-andreessen-is-right-that-ai
3•bigbobbeeper•20m ago•0 comments

Republic – the best way to monitor the situation in SF

https://republic.civlab.org/
1•m_adams•20m ago•0 comments

Sidekiq to Temporal: a zero-downtime migration strategy

https://firehydrant.com/blog/sidekiq-to-temporal-a-zero-downtime-migration-strategy/
2•btables•21m ago•0 comments

Why Gen Z is taking up boomer hobbies

https://www.economist.com/culture/2026/04/01/why-gen-z-is-taking-up-boomer-hobbies
1•andsoitis•24m ago•0 comments

Show HN: I built a Python-based app for Windows security diagnostics

https://github.com/secuditor/secuditor-lite/tree/main
1•mennylevinski•24m ago•0 comments

Show HN: AI tax filing – upload W-2s and 1099s, get completed IRS forms back

https://autocook.io
2•hetelek•24m ago•0 comments

Anthropic says: nothing wrong with our usage limits, you're hallucinating

https://www.reddit.com/r/ClaudeAI/s/u7aJKSDmfy
4•aeneas_ory•25m ago•1 comments

Drowning in Data Sets?

https://www.nature.com/articles/d41586-026-00880-7
1•gnabgib•25m ago•0 comments