frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

1•ramsicandra•7mo 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•7mo 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•7mo 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: Pointa – Point-and-click annotations for AI coding agents (open source)

https://www.pointa.dev/
1•jberthom•28s ago•0 comments

Letting Claude Play Text Adventures

https://borretti.me/article/letting-claude-play-text-adventures
1•zdw•29s ago•0 comments

Show HN: Dutix – set default apps for file types and URL schemes on macOS

https://github.com/jackchuka/dutix
1•jackchuka•1m ago•0 comments

Show HN: App Logo AI – Your generated application logo

https://applogoai.com/
1•goingmerryapps•6m ago•0 comments

Fed Served with DOJ Subpoenas, Powell Vows to Stand Firm

https://www.bloomberg.com/news/articles/2026-01-12/powell-says-justice-department-served-fed-with...
1•ZeroCool2u•6m ago•1 comments

Everybody's Got a Claim

https://ipcopilot.ai/2026/01/03/everybodys-got-a-claim/
1•lettergram•7m ago•0 comments

16 Best Practices for Reducing Dependabot Noise

https://nesbitt.io/2026/01/10/16-best-practices-for-reducing-dependabot-noise.html
1•zdw•8m ago•0 comments

DOJ has subpoenaed central bank, threatens criminal indictment

https://apnews.com/article/federal-reserve-trump-subpoena-bf4fc6c690fa248fbc531bc9bc7f1758
1•SilverElfin•12m ago•1 comments

Ask HN: Claude Code Degradation

2•lobito25•14m ago•0 comments

Duplicate tab notifications and auto closures

https://chromewebstore.google.com/detail/newtab/ajpgplnoabdfhpnlepboonoocpipmhkg
1•pinestack•19m ago•0 comments

Complete Guide to Agentic Commerce Standards

https://curateclick.com/blog/2026-universal-commerce-protocol
2•QingWu•20m ago•0 comments

Advancing Claude in healthcare and the life sciences

https://www.anthropic.com/news/healthcare-life-sciences
1•meetpateltech•22m ago•0 comments

California governor proposes $200M in new state EV tax rebates

https://www.reuters.com/world/california-governor-proposes-200-million-new-state-ev-tax-rebates-2...
2•MilnerRoute•22m ago•0 comments

Show HN: AI Cleaner:Phone Cleaner and Storage Analyzer App

https://ai-cleaner.net/
1•jumpdong•23m ago•0 comments

India proposes forcing smartphone makers to give source code

https://www.reuters.com/world/china/india-proposes-forcing-smartphone-makers-give-source-code-sec...
4•Brajeshwar•36m ago•0 comments

Show HN: Personal news curator running 24/7 on a Raspberry Pi 4 with local LLM

https://life-of-utkarsh.medium.com/pushing-the-limits-running-local-llms-and-a-24-7-personal-news...
2•ukrocks007•41m ago•0 comments

Show HN: Tiktokify Your Blog

https://github.com/NISH1001/tiktokify
2•igaurab•42m ago•0 comments

Universal Commerce Protocol: Open standard for agentic commerce interoperability

https://a2aprotocol.ai/blog/2026-universal-commerce-protocol
2•czmilo•43m ago•2 comments

Patela v2: From Certificates to Hardware

https://osservatorionessuno.org/blog/2025/12/patela-v2-from-certificates-to-hardware/
1•todsacerdoti•46m ago•0 comments

LLVM: The Bad Parts

https://www.npopov.com/2026/01/11/LLVM-The-bad-parts.html
1•zdw•47m ago•0 comments

Subformer: Multilingual video dubbing with speaker diarization and voice cloning

https://subformer.com/en-US
1•mashreghi•48m ago•1 comments

Show HN: `tc` like `wc` but for LLM tokens

https://github.com/jamierpond/tokencount
2•jamiepond•48m ago•0 comments

Training an LLM to Play Diplomacy with RL

https://www.benglickenhaus.com/blog/diplomacy_rl_part_1
1•pawalt•50m ago•0 comments

Show HN: An LLM-optimized programming language

https://github.com/ImJasonH/ImJasonH/blob/main/articles/llm-programming-language.md
5•ImJasonH•52m ago•0 comments

Data Trust (or lack of it) is many paper cuts, not one BIG error

https://www.rudderstack.com/blog/data-trust-clickstream-discrepancy/
1•soumyadeb•52m ago•0 comments

Ask HN: How to automate aesthetic photo cropping? (CV/AI)

1•icons•54m ago•1 comments

Debian goes retro with a spatial desktop that time forgot

https://www.theregister.com/2026/01/09/desktop_classic_system/
3•mmphosis•1h ago•0 comments

Official TypeScript Cheat Sheets

https://www.typescriptlang.org/cheatsheets/
1•doodlesdev•1h ago•0 comments

Himalayas bare and rocky after reduced winter snowfall, scientists warn

https://www.bbc.com/news/articles/clyndv7zd20o
4•koolhead17•1h ago•0 comments

Rethinking Helix

https://asta.boserup.eu/forest/rethinking-helix/
2•todsacerdoti•1h ago•0 comments