frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

1•ramsicandra•11mo 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•11mo 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•11mo 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 More Young People Use AI, the More They Hate It

https://www.theverge.com/ai-artificial-intelligence/920401/gen-z-ai
1•karakoram•44s ago•0 comments

OpenUsage: Track all your AI usage, in one panel

https://github.com/robinebers/openusage
1•nateb2022•1m ago•0 comments

Our Future Is Being Devoured by Feral Thought Experiments

https://www.programmablemutter.com/p/our-future-has-been-devoured-by-feral
1•m-hodges•2m ago•0 comments

VibeLens: Visualize and analyze your AI agent sessions

https://github.com/CHATS-lab/VibeLens
1•yejh•2m ago•0 comments

Show HN: Homebutler – a single-binary homelab ops tool with backup drills

https://github.com/Higangssh/homebutler
1•swq115•4m ago•0 comments

Bahtiarbeck95 Gmail.com

https://blog.cloudflare.com/deploy-planetscale-postgres-with-workers/
1•korikorikori•4m ago•0 comments

What if the universe isn't expanding, but stabilizing?

https://archive.org/details/tenants-of-the-long-sunday
1•voxsignals•5m ago•0 comments

Thoughts on the Boiling Frogs 2026 conference

https://blog.allegro.tech/2026/04/thoughts-on-boiling-frogs-2026.html
1•karmazyn•5m ago•0 comments

The AI Productivity Scorecard Is Broken

https://engineeredinsight.substack.com/p/the-ai-productivity-scorecard-is
1•hacked_brain•5m ago•0 comments

Show HN: Iso.me – Open-source, on-device iOS location tracker

https://github.com/CodyBontecou/isome
1•codybontecou•6m ago•0 comments

Coding Stats

https://github.com/Chrisiisx/Coding-Stats
1•chrisiisx•6m ago•0 comments

Microsoft open-sources "the earliest DOS source code discovered to date"

https://arstechnica.com/gadgets/2026/04/microsoft-open-sources-the-earliest-dos-source-code-disco...
1•DamnInteresting•6m ago•0 comments

Octopus 'krakens' as large as semi-trucks stalked ancient seas

https://www.science.org/content/article/octopus-krakens-large-semi-trucks-stalked-ancient-seas
1•gmays•7m ago•0 comments

Exp-Minus-Log made practical for RPN calculators

https://tangentsoft.com/rpn/wiki?name=R47%3A+Programming+Project+-+EML
1•wyoung2•8m ago•1 comments

Top Best Business Books for Entrepreneurs to Read in 2026

https://1-pagedigitalmarketingplan.com/top-19-best-business-books-for-entrepreneurs-to-read-in-2026/
2•TechOnionKing•8m ago•0 comments

I scraped 1.94M Airbnb photos for opium dens, pet cameos, and messy kitchens

https://burla-cloud.github.io/examples/airbnb-burla-demo/
4•jmp1062•8m ago•0 comments

Restraining AI development for the sake of safety

https://joecarlsmith.com/2026/03/19/on-restraining-ai-development-for-the-sake-of-safety/
1•speckx•8m ago•0 comments

StupidStackLanguage

https://esolangs.org/wiki/StupidStackLanguage
1•optimalsolver•9m ago•0 comments

I over-engineered my simple AI backend: distillation, router, embedding etc.

https://sisyphusconsulting.org/case-studies/2026/04/01/scaling-llms-at-the-edge/
1•bhagyeshsp•9m ago•1 comments

Finding a RCE in my old TP-Link router

https://mrbruh.com/tplink/
3•MrBruh•9m ago•0 comments

Neo Factory – Building Your Neo

https://www.1x.tech/discover/neo-factory
1•denysvitali•9m ago•0 comments

Nothing Is Broken. So Why Does Everything Feel Off?

https://therealitydrift.substack.com/p/nothing-is-broken-so-why-does-everything-feel-wrong
1•realitydrift•13m ago•1 comments

Show HN: Agent-recall-AI – Auto-save for AI agents that die mid-task

https://github.com/srinathsankara/agent-recall-ai
1•SrinathSankara•14m ago•1 comments

OpenHabitat v0.1: open-source Health Automation Systems

https://www.openhabitat.build/habitat/intro/
1•michaelrm•15m ago•1 comments

Rich People Didn't Used to Look Like This

https://www.nytimes.com/2026/04/30/opinion/plastic-surgery-rich-face.html
5•prmph•16m ago•4 comments

AI, Tractors, and the Productivity Paradox

https://read.technically.dev/p/the-ai-productivity-paradox
3•dkrevitt7•17m ago•0 comments

Noisemaker, a shader art engine for the browser (WebGL2/WebGPU)

https://noisemaker.app/
4•aayars•17m ago•1 comments

Lina Lapelytė Fills Hamburger Bahnhof with 400k Blocks for Communal Building

https://www.thisiscolossal.com/2026/04/lina-lapelyte-we-make-years-out-of-hours/
1•cainxinth•18m ago•0 comments

PostgreSQL 19 features I'm excited about

https://www.bytebase.com/blog/postgres-19-features-im-excited-about/
4•tianzhou•20m ago•0 comments

Knee surgery for cartilage damage does not benefit patients, study suggests

https://www.theguardian.com/science/2026/apr/29/knee-surgery-cartilage-damage-patients-study
2•bookofjoe•20m ago•1 comments