frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

The Wayback Machine's snapshots of news homepages plummet after a "breakdown"

https://www.niemanlab.org/2025/10/the-wayback-machines-snapshots-of-news-homepages-plummet-after-...
1•consumer451•25s ago•0 comments

Netherlands registered 474 hours with negative electricity prices

https://www.pv-magazine.com/2025/09/01/netherlands-registers-record-number-of-negative-energy-pri...
1•giuliomagnifico•1m ago•0 comments

Project Indigo – a computational photography camera app

https://research.adobe.com/articles/indigo/indigo.html
1•tosh•2m ago•0 comments

Ask HN: What is best LLM-powered OCR?

1•closetkantian•10m ago•0 comments

Zudoku – Great self-hosted developer docs

https://zudoku.dev
1•martypitt•11m ago•1 comments

Show HN: Pong Wars Idle Game

https://verse8.io/P4s6w3C
2•wancomplete•12m ago•0 comments

Show HN: I tried to imagine HN as a newspaper

https://abstractanuj.github.io/hn/
2•Seasons•13m ago•0 comments

Smarta Tradition

https://en.wikipedia.org/wiki/Smarta_tradition
1•userbinator•17m ago•0 comments

Method tracing and system-wide process sampling

https://github.com/async-profiler/async-profiler/releases/tag/v4.2
1•tanelpoder•18m ago•0 comments

The last voyage of the Scandies Rose

https://www.theguardian.com/news/2025/oct/21/i-knew-in-my-head-we-were-dying-the-last-voyage-of-t...
1•nickcotter•20m ago•0 comments

French ex-president Sarkozy begins jail sentence

https://www.bbc.com/news/articles/cvgkm2j0xelo
2•begueradj•23m ago•0 comments

Trump Sees Successful Xi Meeting, but Allows It Might Not Happen

https://www.bloomberg.com/news/articles/2025-10-21/trump-sees-successful-xi-meeting-but-allows-it...
2•zerosizedweasle•23m ago•1 comments

Remote command injection in TP-Link Omada Gateway devices

https://ccb.belgium.be/advisories/warning-remote-command-injection-tp-link-omada-gateway-devices-...
2•beala•27m ago•0 comments

Modesto teachers say classroom chaos has gotten out of control [video]

https://www.youtube.com/watch?v=ErJio51eypo
1•shinryudbz•27m ago•0 comments

Protests Erupt in New York City After Ice Raids Chinatown

https://www.theguardian.com/us-news/2025/oct/21/new-york-city-chinatown-ice-raids-protests
4•KnuthIsGod•27m ago•0 comments

Airbnb CEO says ChatGPT isn't ready

https://www.latimes.com/business/story/2025-10-21/chesky-says-openai-tools-not-ready-for-chatgpt-...
1•makeavish•28m ago•1 comments

Why SSA?

https://mcyoung.xyz/2025/10/21/ssa-1/
1•ingve•29m ago•0 comments

Software engineering primer for non software engineers

https://thetechenabler.substack.com/p/software-engineering-primer-for-non
1•ingve•34m ago•0 comments

First images of Antares photosphere from spectropolarimetry

https://arxiv.org/abs/2507.08614
1•r4um•36m ago•0 comments

AI and the Golem

https://going-medieval.com/2025/10/21/on-ai-and-the-golem/
1•rwl•37m ago•0 comments

A deep dive into BPF LPM trie performance and optimization

https://blog.cloudflare.com/a-deep-dive-into-bpf-lpm-trie-performance-and-optimization/
1•r4um•38m ago•0 comments

Advent of Code 2025 will have 12 problems and no global leaderboard

https://old.reddit.com/r/adventofcode/comments/1ocwh04/changes_to_advent_of_code_starting_this_de...
3•nneonneo•38m ago•0 comments

A real estate boom is coming – are you ready?

https://thehill.com/opinion/finance/5563627-real-estate-boom-prediction/amp/
1•harambae•42m ago•1 comments

But can it run doom?

https://archiveofourown.org/works/31295183
1•KuSpa•42m ago•0 comments

Pain tolerance found to increase during social interaction in VR

https://medicalxpress.com/news/2025-10-pain-tolerance-social-interaction-vr.html
1•PaulHoule•42m ago•0 comments

Robot touch sensing with torque sensors only [video]

https://www.youtube.com/watch?v=rgoKUmdIRnU
2•msadowski•47m ago•0 comments

Styx Telegram Bot Framework

1•OasisPioneer•52m ago•0 comments

OpenAI Looks to Replace the Drudgery of Junior Bankers' Workload

https://www.bloomberg.com/news/articles/2025-10-21/openai-looks-to-replace-the-drudgery-of-junior...
2•polskibus•57m ago•0 comments

Lenovo Coupon Codes and Deals: $5k Off

https://www.wired.com/story/lenovo-coupon-code/
2•quapster•58m ago•0 comments

Dr. Beatriz Villarroel with Dr. Garry Nolan: UAP Detected in Orbit B4 Satellites [video]

https://www.youtube.com/watch?v=rs1XRf21m7o
3•handfuloflight•1h ago•0 comments