frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

1•ramsicandra•9mo 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•9mo 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•9mo 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: R3forth Visual Debugger [video]

https://www.youtube.com/watch?v=_gvhfxM5hRg
2•phreda4•1m ago•0 comments

Show HN: Pidrive – File storage for AI agents (mount S3, use ls/cat/grep)

https://pidrive.ressl.ai/
2•abhishek203r•2m ago•0 comments

AI #159: See You in Court

https://thezvi.substack.com/p/ai-159-see-you-in-court
2•paulpauper•3m ago•0 comments

Show HN: Docgen – A C++ AI CLI to solve documentation hell with local LLMs

1•alonsovm•4m ago•0 comments

Ask HN: What was it like for programmers when spreadsheets became ubiquitous?

2•yodaiken•8m ago•0 comments

Is this real? Susceptibility to deepfakes in machines and humans

https://link.springer.com/article/10.1186/s41235-025-00700-y
1•PaulHoule•8m ago•0 comments

The Price of College [video]

https://www.youtube.com/watch?v=qiAZd_Ut9sg
1•gmays•8m ago•0 comments

Reddit Post 3

https://old.reddit.com/r/PisequaltoNP/comments/1rtu4j5/solving_monotone_sat_in_om_log_n_via_binary/
1•KaoruAK•9m ago•0 comments

Kalverion Bot Overdraft Stopper new release

https://github.com/bisbeebucky/ai-bot/
1•aajjwww•10m ago•1 comments

Expanding Ecosystems (Michigan)

https://midwesthumble.substack.com/p/expanding-ecosystems
1•rmason•10m ago•0 comments

Please don't write about AI with AI

1•Arete314159•11m ago•0 comments

The medical advice on peanut allergies flipped in a generation

https://www.cbc.ca/lite/story/9.7125919
1•colinprince•13m ago•0 comments

Microservices: Shackles on Your Feet

https://howtocenterdiv.com/beyond-the-div/microservices-shackles-on-your-feet
2•birdculture•20m ago•0 comments

Bellingcat: The Osint Gatekeepers Who Can't Secure Their Own Site

https://ringmast4r.substack.com/p/the-osint-gatekeepers-who-cant-secure
3•mostcallmeyt•23m ago•1 comments

Daily pill may cure deadly sleep disorder that affects 84M people

https://www.dailymail.co.uk/health/article-15643615/pill-cure-sleep-apnea-CPAP-breathing.html
4•Bender•24m ago•0 comments

Ask HN: How do you find collaborators?

1•voidss•24m ago•1 comments

Iran war's Qatari Helium production disruption is a blow to chipmakers like TSMC

https://finance.yahoo.com/news/iran-war-could-wreak-havoc-on-farmers-create-a-potential-bottlenec...
1•spenvo•24m ago•0 comments

Meta reportedly plans layoffs as AI costs increase

https://www.theguardian.com/technology/2026/mar/13/meta-layoffs-ai
4•saikatsg•26m ago•0 comments

Do you ship vibe coded apps with security issues?

https://usevibescore.com
1•terrythreatt•27m ago•1 comments

US told to brace for extreme weather in every single state

https://www.dailymail.co.uk/news/article-15645675/us-extreme-weather-forecast-weekend-heat-polar-...
1•Bender•27m ago•0 comments

Where Censored Words Find a Safe Haven: Inside Minecraft

https://www.nytimes.com/2026/03/11/arts/minecraft-uncensored-library-united-states.html
1•bookofjoe•29m ago•1 comments

The Washington Post Is Using Reader Data to Set Subscription Prices

https://washingtonian.com/2026/03/12/the-washington-post-is-using-reader-data-to-set-subscription...
2•kklisura•29m ago•0 comments

Postgres Is the Gateway Drug

https://viggy28.dev/article/postgres-gateway-drug/
7•vira28•31m ago•1 comments

Back End Aggregation Enables Gigawatt-Scale AI Clusters

https://engineering.fb.com/2026/02/09/data-center-engineering/building-prometheus-how-backend-agg...
1•y1n0•31m ago•0 comments

Library of Short Stories

https://www.libraryofshortstories.com/
1•debo_•32m ago•0 comments

Millennium Challenge: Iran Destroyed America in a War Game

https://nationalinterest.org/blog/reboot/millennium-challenge-iran-destroyed-america-war-game-197261
1•vrganj•32m ago•0 comments

AI Codemods for Secure-by-Default Android Apps

https://engineering.fb.com/2026/03/13/android/ai-codemods-secure-by-default-android-apps-meta-tec...
1•y1n0•32m ago•1 comments

Book: The Emerging Science of Machine Learning Benchmarks

https://mlbenchmarks.org/00-preface.html
1•jxmorris12•33m ago•0 comments

Pipechart – pipe any JSON into your terminal and get a chart, zero dependencies

https://github.com/davitotty/pipechart
1•Davitotty1•33m ago•0 comments

Show HN: An Open-Source Yoto Toy with Qwen3-TTS

https://github.com/akdeb/open-toys
2•akadeb•35m ago•1 comments