frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Google Chrome Developer Tools: AI Powered Suggestions

https://developer.chrome.com/docs/devtools/settings/ai-innovations
1•rasengan•1m ago•0 comments

Fran Sans – font inspired by San Francisco light rail displays

https://emilysneddon.com/fran-sans-essay
1•ChrisArchitect•1m ago•0 comments

Apple to focus on 'quality and underlying performance' with iOS 27 next year

https://9to5mac.com/2025/11/23/apple-focusing-on-software-quality-improvements-ios-27-next-year-r...
1•jb1991•2m ago•0 comments

G0-G3 corners, visualised: learn what "Apple corners" are

https://www.printables.com/model/1490911-g0-g3-corners-visualised-learn-what-apple-corners
1•dgroshev•5m ago•0 comments

The Housing Strategy That Has California NIMBYs in a Corner

https://www.nytimes.com/2025/11/20/business/economy/california-housing-nimby.html
1•mitchbob•6m ago•1 comments

Whisperer AI Note Taker – Automatic meeting transcription and summary on iPhone

https://apps.apple.com/gb/app/whisperer-ai-note-taker/id6755069300
1•deepskyapps•7m ago•1 comments

Show HN: Menu Bar World Clocks

https://sindresorhus.com/zone-bar
1•mofle•7m ago•0 comments

HPE Launches AMD EPYC Venice Instinct MI400 and Nvidia Vera Rubin Compute Blades

https://www.servethehome.com/hpe-launches-new-amd-venice-instinct-mi400-and-nvidia-vera-rubin-com...
1•PaulHoule•9m ago•0 comments

Interstellar Space Travel Will Never, Ever Happen

https://jasonpargin.substack.com/p/interstellar-space-travel-will-never
3•billybuckwheat•9m ago•0 comments

Non-Tech Communities That Inform Our DevRel Activities

https://www.communitypulse.io/101-non-tech-communities
1•mooreds•10m ago•0 comments

The Most Dangerous Genre

https://www.newyorker.com/news/the-lede/the-most-dangerous-genre
1•mitchbob•10m ago•1 comments

Ruby Was Ready from the Start

https://obie.medium.com/ruby-was-ready-from-the-start-4b089b17babb
1•mooreds•11m ago•0 comments

Selecting the right product metrics (KPIs) (2023)

https://longform.asmartbear.com/product-metrics/
1•mooreds•11m ago•0 comments

ChatGPT for Teachers

https://chatgpt.com/plans/k12-teachers/
1•sunnybeetroot•15m ago•0 comments

Don't spill your guts to your chatbot friend – it'll hoover up info for training

https://www.theregister.com/2025/11/20/experts_warns_house_of_privacy/
2•Bender•16m ago•0 comments

Self-destructing thumb drive can brick itself and wipe your secret files away

https://www.theregister.com/2025/11/21/selfdestructing_external_ssd/
2•Bender•18m ago•0 comments

Ask HN: How do you teach a 7 year old visual editing on PC?

1•stmL•19m ago•0 comments

Early experiments in accelerating science with GPT-5

https://openai.com/index/accelerating-science-gpt-5/
2•sanjitb•21m ago•1 comments

Netflix, Comcast and Paramount Submit Warner Bros. Discovery Bids

https://www.nytimes.com/2025/11/20/business/media/warner-discovery-bids-paramount-netflix-comcast...
1•ChrisArchitect•21m ago•1 comments

Show HN: Genesis DB now provides a full gRPC API alongside HTTP

https://www.genesisdb.io/blog/posts/2025-11-23/grpc-endpoint
2•patriceckhart•24m ago•0 comments

Logic Puzzle from the University Quest

https://wondrousnet.blogspot.com/2025/11/solution-to-psychoanalytix-puzzle.html
2•morethenthis•26m ago•0 comments

Native Secure Enclave backed SSH keys on macOS

https://gist.github.com/arianvp/5f59f1783e3eaf1a2d4cd8e952bb4acf
52•arianvanp•27m ago•3 comments

A Swift Language Feature I wish existed in TypeScript

https://www.kxlaa.com/articles/a-swift-language-feature-i-wish-existed-in-typescript
1•kxlaa•27m ago•0 comments

97 'peace walls' still divide Belfast. Will they ever come down?

https://www.thetimes.com/uk/northern-ireland/article/belfast-peace-walls-divide-jxb9nfn0p
1•ilamont•27m ago•0 comments

The Agent Lab Thesis

https://www.latent.space/p/agent-labs
1•theptip•30m ago•0 comments

Show HN: Testing an AI-first HTML landing page for LLM crawling

https://ai.asasushi.pl/
1•marcustas•30m ago•1 comments

Tell HN: NYT put a JavaScript wall on page fetches

1•m-hodges•31m ago•0 comments

Enough with All the Raft

https://transactional.blog/talk/enough-with-all-the-raft
2•birdculture•35m ago•0 comments

iTorrent App Restored to AltStore in the EU

https://github.com/XITRIX/iTorrent/issues/401
3•NotPractical•38m ago•1 comments

Dev-CPP: open-source IDE

https://www.dev-cpp.com/
2•bitbasher•40m ago•1 comments