frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

LLMs and Your Career

https://notes.eatonphil.com/2026-01-19-llms-and-your-career.html
1•cod1r•38s ago•0 comments

Only Known Photos of Computing Pioneer Ada Lovelace Join UK National Collection

https://news.artnet.com/art-world/ada-lovelace-daguerreotypes-uk-national-portrait-gallery-2735593
1•gnabgib•3m ago•0 comments

Show HN: Bubblewrap TUI – makes bubblewrap's command lines less terrifying

https://github.com/reubenfirmin/bubblewrap-tui
1•smallerfish•4m ago•1 comments

Geoff Huntley, creator of the Ralph loop, goat farmer, prophet

https://www.youtube.com/watch?v=lQ2ihlUzlfs
1•claudiacsf•4m ago•0 comments

OpenSplitDeck

https://github.com/tommybee456/OpenSplitDeck
1•euazOn•4m ago•1 comments

A fun trick for getting discovered by LLMs and AI tools

https://cassidoo.co/post/ai-llm-discoverability/
1•mooreds•4m ago•0 comments

The Problem with Lying Is Keeping Track of All the Lies

https://materialize.com/blog/keeping-track-lies/
1•transpute•5m ago•0 comments

30-Days Growth Challenge

https://docs.google.com/forms/d/e/1FAIpQLSe0zS7tIGAOLvKNJ-m6GWW_t-CKCadkKuS97tTdeG4UhmYiAA/viewfo...
1•shurman81•9m ago•1 comments

Ask HN: Where Are the Primary Sources?

2•lionkor•10m ago•0 comments

Show HN: NPM/uv for Claude Code – install skills from GitHub with one command

https://github.com/kasperjunge/agent-resources/blob/main/README.md
1•juunge•11m ago•0 comments

Iran's partial internet shutdown may be a windfall for cybersecurity Intel

https://www.csoonline.com/article/4116890/irans-partial-internet-shutdown-may-be-a-windfall-for-c...
2•bookofjoe•13m ago•0 comments

Can Highlighting Help GitHub Maintainers Track Security Fixes?

https://arxiv.org/abs/2411.11646
1•walterbell•16m ago•0 comments

Ask HN: Is your email inbox also filled with Zendesk reply spam?

3•TazeTSchnitzel•17m ago•3 comments

If you want to understand any society, look at what it creates

https://notes.philippdubach.com/0009
1•7777777phil•24m ago•0 comments

Vibe Engineering in 2026.1

https://me.0xffff.me/vibe_engineering_202601.html
1•c4pt0r•25m ago•0 comments

The Date Data Type in Oracle vs. PostgreSQL

https://hexacluster.ai/blog/the-date-data-type-in-oracle-vs-postgresql/
1•avivallssa•25m ago•1 comments

Show HN: Pharos – "yarn why" but shows the full chain and what to update (Rust)

https://github.com/vaskort/pharos
1•vaskort•25m ago•0 comments

Train Your Tenacity

https://playtechnique.io/blog/train-your-tenacity.html
1•gwynforthewyn•27m ago•1 comments

Estimation of Variance by a Recursive Equation (pdf, 1969)

https://ntrs.nasa.gov/api/citations/19690028796/downloads/19690028796.pdf
1•slow_typist•27m ago•1 comments

Why sandboxing coding agents is harder than you think

https://martinalderson.com/posts/why-sandboxing-coding-agents-is-harder-than-you-think/
2•jnord•29m ago•1 comments

Frankenwine: Multiple Personas in a Wine Process

https://nullprogram.com/blog/2026/01/19/
3•zdw•30m ago•1 comments

ChatVault – Local-first semantic search for WhatsApp (Rust and WASM)

https://github.com/marcoshernanz/ChatVault
1•marcoshernanz•30m ago•1 comments

Show HN: Cmdarg, argument parsing for bash scripts

https://github.com/akesterson/cmdarg
1•akesterson•32m ago•0 comments

James Prinsep, polymath who deciphered Kharosthi and Brahmi scripts

https://en.wikipedia.org/wiki/James_Prinsep
1•like_any_other•32m ago•0 comments

Even citing NSF funded research is an ACM premium feature

https://win-vector.com/2026/01/19/even-citing-nsf-funded-research-is-an-acm-premium-feature/
1•jmount•33m ago•0 comments

GLM-4.7-Flash 30B-A3B MoE

https://xcancel.com/zai_org/status/2013261304060866758
2•Alifatisk•33m ago•0 comments

The average 50-something American is now worth $1.4M

https://www.usatoday.com/story/money/2026/01/19/average-net-worth-1-million-50-year-olds/88202093...
7•lxm•34m ago•6 comments

The US is buying icebreakers from Finland

https://www.bbc.co.uk/news/articles/c0q48d71ke8o
5•perihelions•36m ago•0 comments

Systems design 3: LLMs and the semantic revolution

https://apenwarr.ca/log/20251120
1•gjadi•36m ago•0 comments

Anthropic's Interpretability Research Blog

https://transformer-circuits.pub/
2•philipfweiss•37m ago•1 comments