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.

Show HN: Claude-relais – A plan/build/judge loop mixing Claude with Cursor

https://github.com/clementrog/claude-relais
1•crog•1m ago•0 comments

Can agentic coding raise the quality bar?

https://lpalmieri.com/posts/agentic-coding-raises-quality/
1•LukeMathWalker•1m ago•0 comments

Learning Kubernetes with the official docs and NotebookLM

https://randomwrites.com/
1•mutahirs•1m ago•1 comments

List of Sports Clichés

https://en.wikipedia.org/wiki/List_of_sports_clich%C3%A9s
1•carlos-menezes•2m ago•0 comments

State Attorneys General Want to Tie Online Access to ID

https://reclaimthenet.org/40-attorneys-general-back-ids-online-safety-act
2•computerliker•4m ago•0 comments

Python Fiddle – Online Python IDE, Compiler, and Interpreter

https://python-fiddle.com
1•Curiositry•4m ago•0 comments

Large Language Model Reasoning Failures

https://arxiv.org/abs/2602.06176
1•kawera•5m ago•0 comments

When Your Ally Turns Narcissistic: Manual for Navigating Transatlantic Relations

https://gppi.net/2025/10/12/when-your-ally-turns-narcissistic
1•rendx•6m ago•1 comments

The Second Half of the Chessboard

https://joshs.bearblog.dev/the-second-half-of-the-chessboard/
1•psychedare•11m ago•1 comments

Peter Steinberger: I need AI that scans every PR and Issue and de-dupes

https://twitter.com/steipete/status/2023057089346580828
1•vibeprofessor•12m ago•0 comments

Show HN: VOOG – Moog-style polyphonic synthesizer in Python with tkinter GUI

https://github.com/gpasquero/voog
3•gpasquero•12m ago•0 comments

Accessibility Is All You Need – Why agent protocols for the web are redundant

https://github.com/webmachinelearning/webmcp/issues/91
1•lulzx•12m ago•1 comments

Miller – CLI tool for querying, shaping, and reformatting data in many formats

https://miller.readthedocs.io/en/6.16.0/
2•smartmic•13m ago•0 comments

What Happened in El Paso? – By James Fallows

https://fallows.substack.com/p/what-happened-in-el-paso
2•MaysonL•14m ago•0 comments

I made a real BMO local AI agent with a Raspberry Pi and Ollama

https://www.youtube.com/watch?v=l5ggH-YhuAw
1•emigre•18m ago•0 comments

Show HN: Let AI agents try things without consequences

https://github.com/multikernel/branching
2•wang_cong•18m ago•0 comments

California Drastically Reduces Creditor Exemptions for Qualified Accounts (2024)

https://www.forbes.com/sites/jayadkisson/2024/10/07/california-drastically-reduces-creditor-exemp...
1•dataflow•23m ago•0 comments

Show HN: A tool to keep dotfiles and system configs in sync with a Git repo

https://github.com/senotrusov/etcdotica
1•senotrusov•24m ago•0 comments

Show HN: Design Memory – Extract design systems from live websites via CLI

https://github.com/memvid/design-memory
1•saleban1031•25m ago•0 comments

Kimi Claw

https://twitter.com/Kimi_Moonshot/status/2023029674549596301
1•tosh•26m ago•1 comments

Show HN: GPU Perpetual Futures Prototype

https://github.com/zacharyfrederick/compex
1•ozzymandiaz96•32m ago•0 comments

Show HN: DoScript – Automation language with English-like syntax

https://github.com/TheServer-lab/DoScript
1•server-lab•32m ago•0 comments

Ideas for an Agent-Oriented Programming Language

https://davi.sh/blog/2026/02/markov-ideas/
1•davish•32m ago•0 comments

I've Seen the Future of the Software "Engineer" Gig – It's Orwellian AF

4•burnerToBetOut•38m ago•1 comments

Attack. Attack. Attack

https://twitter.com/ADoricko/status/1868113416805814530
1•mold_aid•38m ago•1 comments

Dell XPS Core Ultra 7 355 Panther Lake: Still great, but not nearly as special

https://www.notebookcheck.net/Dell-XPS-14-Core-Ultra-7-355-review-Still-great-but-not-nearly-as-s...
1•cromka•39m ago•1 comments

DDD: Back to Basics

https://docs.eventsourcingdb.io/blog/2026/02/16/ddd-back-to-basics/
1•goloroden•41m ago•0 comments

Show HN: OpenContext – Bring Your Own Coding Agent, Local-First, No Vendor Lock

https://github.com/adityak74/opencontext
1•akarnam37•42m ago•0 comments

We Uncovered the Scheme Keeping Grocery Prices High [video]

https://www.youtube.com/watch?v=odhVF_xLIQA
6•dataflow•44m ago•0 comments

Show HN: iherb-CLI – An agent-optimized CLI for AI-driven supplement research

https://github.com/SeverinAlexB/iherb-cli
1•sebubu•45m ago•0 comments