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.

Show HN: Yolt – safer YOLO mode for LLMs (recover deleted or overwritten files)

https://github.com/mvanderlinde/yolt
1•marc92•23s ago•0 comments

A systematic search for wormholes across all public astronomical data

https://www.blankline.org/research/hunting-for-real-wormholes-we-searched-every-available-dataset
1•DarenWatson•1m ago•0 comments

Show HN: I kept missing Claude Code prompts, so I built a tray dot in Rust

https://github.com/sprklai/agenttray
1•rakeshd•1m ago•0 comments

500 MWe Nuclear Fast Breeder Reactor built by India attains first criticality

https://www.pib.gov.in/FactsheetDetails.aspx?Id=150617
1•saharshpruthi•2m ago•0 comments

Fish Climbing in the Upper Congo Basin (Central Africa)

https://www.nature.com/articles/s41598-026-42534-8
1•Someone•3m ago•0 comments

Show HN: Heron is open-source security auditor that interviews your AI agents

https://github.com/theonaai/Heron
1•IlyaIvanov0•5m ago•0 comments

What Holds

https://www.kevinsdias.com/posts/what-holds.html
2•speckx•5m ago•0 comments

Show HN: Flowcost – know what your AI workflow is likely to cost

https://www.flowcost.ai/
1•erikms•5m ago•0 comments

Mythos Quest

https://thezvi.substack.com/p/ai-163-mythos-quest
1•7777777phil•7m ago•0 comments

Ask HN: What's Up with Azure DevOps

1•teekert•7m ago•0 comments

How to Set Up Your Monitoring System Alerts

https://blog.appsignal.com/2026/04/07/how-to-set-up-your-monitoring-system-alerts.html
1•malinavojvodic•8m ago•0 comments

Updu: Lightweight, self-hosted uptime monitoring in a single binary

https://github.com/nwpeckham88/updu
1•meysamazad•8m ago•0 comments

Used EV sales spike alongside gas prices

https://arstechnica.com/cars/2026/04/used-ev-sales-spike-alongside-gas-prices/
2•rbanffy•9m ago•0 comments

LLM inference engine from scratch in C++ – why output tokens cost 5x

https://www.anirudhsathiya.com/blog/transformer
4•ani17•11m ago•0 comments

Ask HN: How to learn web design in general

1•andrew-v•13m ago•1 comments

Early precursor signals observed before incidents (RTT/DNS/HTTP telemetry)

2•ravensystems•17m ago•0 comments

Bill Gates to testify before House Oversight in Epstein probe

https://www.politico.com/news/2026/04/07/bill-gates-testify-congress-epstein-probe-00861678
4•RickJWagner•18m ago•1 comments

Show HN: Skrun – Deploy any Agent Skill as an API (open source)

https://github.com/skrun-dev/skrun
4•frizull•22m ago•2 comments

Bonsai: The Spatial Index That Tunes Itself

https://github.com/anurag-as/bonsai
1•sampathanurag3•25m ago•0 comments

How Do I Integrate News for B2B Domain

1•Teleglobal•26m ago•0 comments

Show HN: SuperUtter – A lightweight macOS TTS app (Kokoro local and ElevenLabs)

https://superutter.com/
2•jotaefea•29m ago•0 comments

Show HN: I turned professional genealogy methodology into a system prompt

https://github.com/DigitalArchivst/Open-Genealogy/tree/main/skills/gra
1•DigitalArchivst•29m ago•0 comments

US cities are axing Flock Safety surveillance technology

https://www.cnet.com/home/security/when-flock-comes-to-town-why-cities-are-axing-the-controversia...
8•giuliomagnifico•31m ago•1 comments

FBI: Americans lost a record $21B to cybercrime last year

https://www.bleepingcomputer.com/news/security/fbi-americans-lost-a-record-21-billion-to-cybercri...
4•anonhaven•31m ago•1 comments

Show HN: Domternal – Rich text editor toolkit on ProseMirror with Angular UI

https://github.com/domternal/domternal
1•thomasnowhere•33m ago•0 comments

MegaTrain: Full Precision Training of 100B+ Parameter LLMs on a Single GPU

https://arxiv.org/abs/2604.05091
9•chrsw•38m ago•1 comments

Takeaways from Our Search for Bitcoin's Creator

https://www.nytimes.com/2026/04/08/business/takeaways-satoshi-nakamoto-bitcoin-adam-back.html
2•glenstein•39m ago•0 comments

Show HN: I pipe free sports streams into Jellyfin – no ads, just HLS

https://github.com/pcruz1905/hls-restream-proxy
2•pruz•39m ago•0 comments

The Empty Middle of AI Coding

https://xificurc.github.io/blog/the-empty-middle-of-ai-coding/
3•comma_at•42m ago•1 comments

Apple II emulator with extensive analytical tools

https://bsky.app/profile/jtauber.com/post/3mixa6ywbbk2l
1•mariuz•44m ago•0 comments