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.

What Notion, Figma and Pokémon got right about multi-player

https://medium.com/@yamatokaneko/what-notion-figma-and-pokémon-got-right-about-multi-player-be448...
1•yamatokaneko•2m ago•0 comments

Show HN: Skillwave – agent orchestrator with async comms and goal-alignment loop

https://github.com/loudinthecloud/skillwave
1•guyzana•3m ago•0 comments

Show HN: Hush – auto-detect screen sharing and blur your desktop for macOS

https://apps.apple.com/us/app/hush-screen-share-focus/id6757990863?mt=12
1•Olersandrkoz•3m ago•0 comments

Show HN: Open-source running app on the App store

https://github.com/sawtoothdata/FreePace
1•jmcminis•5m ago•1 comments

Show HN: I built a tool that audits docs for agent readability

https://doc-audit.clouddani.com
2•Dwashington•5m ago•0 comments

Child Centric Search and Rescue

https://beacon-rescue-os.base44.app
1•wesley-Alan•6m ago•0 comments

Why "predict the next word" shouldn't work this well (but does)

https://medium.com/@tudor.malene/why-predict-the-next-word-shouldnt-work-this-well-but-does-d0333...
1•tudorhn•6m ago•0 comments

A woman who alerts the world when an asteroid could hit

https://www.theguardian.com/science/ng-interactive/2026/mar/30/asteroid-warning-earth-un-office-f...
1•hackernj•6m ago•0 comments

Why Some Criticisms Matter More Than Others

https://gnupg.org/blog/20260320-some-criticism-matter.html
1•bitcrshr•8m ago•0 comments

Colleague.skill

https://github.com/titanwings/colleague-skill/blob/main/README_EN.md
1•csmantle•8m ago•0 comments

A survey of tools for remote AI development

https://www.dgt.is/blog/2026-03-30-ai-ide/
1•jonotime•8m ago•0 comments

customermates.com – AI-first, Open-source, self-hostable CRM

https://github.com/customermates/customermates
1•benjiwagn•9m ago•1 comments

Dawkin's paradox: dissecting the body's battle to keep selfish genes in check

https://www.nature.com/articles/d41586-026-00970-6
1•salkahfi•9m ago•0 comments

Wilkinson's Grammar of Graphics

https://en.wikipedia.org/wiki/Wilkinson%27s_Grammar_of_Graphics
1•tosh•10m ago•0 comments

Particle Experiments at Kolar Gold Fields

https://en.wikipedia.org/wiki/Particle_experiments_at_Kolar_Gold_Fields
1•sauravmaheshkar•11m ago•0 comments

Show HN: WorldSim – Monte Carlo simulation for 195 countries to 2050

https://worldsimlab.com/
1•Yiannis91•12m ago•0 comments

Everything Claude Saw: A Transparent Account of the Chardet v7 Rewrite

http://dan-blanchard.github.io/blog/chardet-rewrite-controversy/
1•dan_blanchard•12m ago•1 comments

Maestri – Infinite canvas where coding agents work side by side (macOS)

https://www.themaestri.app/en
1•evertjr•13m ago•0 comments

A Control Panel for N8n to Simplify Self-Hosting

1•openhosst•14m ago•0 comments

The Team PCP Snowball Effect: A Quantitative Analysis

https://blog.gitguardian.com/team-pcp-snowball-analysis/
2•chillax•14m ago•0 comments

StatPhone – the one number you share for calls you can't afford to miss

https://statphone.com/
1•popupeyecare•14m ago•1 comments

Show HN: AgentLair – Give your AI agent an email identity and credential vault

https://agentlair.dev/
1•hawk_aa•15m ago•0 comments

Ask HN: Where have you found the coding limits of current models?

5•logicallee•17m ago•1 comments

Tiiny

https://tiiny.ai/
1•hasheddan•17m ago•0 comments

Spain closes airspace to aircraft involved in Iran war

https://english.elpais.com/international/2026-03-30/spain-closes-airspace-to-aircraft-involved-in...
5•prmph•19m ago•0 comments

China's DeepSeek AI chatbot suffers longest outage

https://www.reuters.com/technology/chinas-deepseek-ai-chatbot-suffers-longest-outage-since-viral-...
2•01-_-•20m ago•2 comments

Vivinavi World

https://world.vivinavi.com/
1•gregsadetsky•20m ago•0 comments

Show HN: Native HTML/CSS UI in C++ (5MB) + demo with animated charts

https://github.com/M4iKZ/mui-captable-demo
1•M4iKZ•20m ago•0 comments

The Alluring Beauty of Small Engineering Teams

https://managerstories.co/the-alluring-beauty-of-small-engineering-teams/
1•gpi•25m ago•0 comments

Infinite Jest Extraction

https://mattlakeman.org/2026/03/26/infinite-jest-extraction/
1•surprisetalk•26m ago•0 comments