frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Introducing Merge Labs

https://merge.io/blog
1•cafebeen•23s ago•1 comments

Show HN: Public Apache Iceberg datasets via a REST catalog

https://opensource.googleblog.com/2026/01/explore-public-datasets-with-apache-iceberg-and-biglake...
1•talatuyarer•1m ago•0 comments

Only 16% of Europeans still see the US as an ally, new survey finds

https://davekeating.substack.com/p/only-16-of-europeans-still-see-the
2•mdhb•2m ago•0 comments

Ask HN: One IP, multiple unrealistic locations worldwide hitting my website

1•nacho-daddy•2m ago•0 comments

Rewriting memory in the lab might one day heal humans

https://www.latimes.com/science/story/2025-12-27/could-memory-manipulation-one-day-help-treat-pts...
1•PaulHoule•3m ago•0 comments

Open Roberta Lab

https://lab.open-roberta.org/#
1•Tomte•3m ago•0 comments

Rift at top of the Taliban: BBC reveals clash of wills behind internet shutdown

https://www.bbc.co.uk/news/articles/cg7vdpy1l2vo
1•edward•5m ago•0 comments

Show HN: Control Claude permissions using cloud-based decision tables

https://github.com/rulebricks/claude-code-guardrails
1•sidgarimella•7m ago•0 comments

Senator Cruz wants commercial space stations up and manned before ISS retires

https://arstechnica.com/space/2026/01/key-senate-staffer-is-begging-nasa-to-get-on-with-commercia...
1•bell-cot•8m ago•1 comments

Anthropic Economic Index economic primitives

https://www.anthropic.com/research/anthropic-economic-index-january-2026-report
1•atlasunshrugged•8m ago•0 comments

Finding Matrices that you can multiply wrong, right

https://www.hgreer.com/BadMatrixMultiply/
1•aebtebeten•8m ago•0 comments

Ask HN: How can we combat fascism?

1•indisposable•9m ago•1 comments

Zuck#: A programming language for connecting the world. And harvesting it

https://jayzalowitz.github.io/zucksharp/
10•kf•10m ago•3 comments

Show HN: Turn Steam reviews into personas and insights, without agent chatting

https://steam-review.dexmage.com/
1•dexteryy•13m ago•0 comments

Show HN: A-MEM – Memory for Claude Code that links and evolves on its own

https://github.com/DiaaAj/a-mem-mcp
4•AttentionBlock•13m ago•1 comments

Yasu – AI agents that fix cloud waste, not just report it

https://yasu.cloud/
1•johninthout•14m ago•1 comments

Where does data help in real estate – and where does it fail?

1•D___R___•15m ago•0 comments

Important Paper

https://zenodo.org/records/18234712
1•KaoruAK•17m ago•0 comments

Cursor may be switching from Solid to React

https://cursor.com/blog/scaling-agents#running-for-weeks
2•benatkin•17m ago•1 comments

AI as life coach: experts say what works, what doesn't and what to look out for

https://www.theguardian.com/wellness/2026/jan/15/ai-life-coach
3•billybuckwheat•19m ago•0 comments

Open Responses

https://www.openresponses.org/
2•davidbarker•20m ago•0 comments

Nothing new under the sun: everything is a file

https://turso.tech/blog/nothing-new-under-the-sun
2•weinzierl•21m ago•0 comments

Every journey feels better when you're part of it

https://otieu.com/4/8908706
1•abdpatil•22m ago•0 comments

Paul Graham Claude Code Skill

https://www.aibuilder.sh/skills/acossta/paul-graham
1•acossta•22m ago•0 comments

Ask HN: Anyone have a good solution for modern Mac to legacy SCSI converters?

7•stmw•23m ago•8 comments

You can't tell people anything

https://habitatchronicles.com/2004/04/you-cant-tell-people-anything/
2•evo_9•23m ago•0 comments

Analysis of ServiceNow's AI Vulnerability (85% of Fortune 500 Affected)

https://opena2a.org/blogs/servicenow-ai-vulnerability
2•abdelfane•24m ago•1 comments

Show HN: Okiro – spin up ephemeral codebases for parallel AI coding

https://github.com/ygwyg/okiro
4•burcs•25m ago•0 comments

Scrollbars in Scrollbars

https://matoseb.com/scrollbars-scrollbars/
1•birdculture•26m ago•0 comments

Solving TaxCalcBench: LLMs solve taxes by navigating IRS taxcode like a codebase [pdf]

https://prime-meridian-papers.s3.us-west-2.amazonaws.com/solving_taxes_like_code.pdf
1•rishabhjain1198•26m ago•2 comments