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.

Why DuckDB is my first choice for data processing

https://www.robinlinacre.com/recommend_duckdb/
1•tosh•5m ago•0 comments

AI Agent Testing

1•dinasorous•8m ago•0 comments

Ask HN: Need ArXiv endorsement for LLM inference paper

1•logotype•8m ago•0 comments

Apache Paimon is a lake format that enables building a Realtime Lakehouse

https://github.com/apache/paimon
1•tosh•10m ago•0 comments

Meta has discontinued its metaverse for work, too

https://www.theverge.com/tech/863209/meta-has-discontinued-its-metaverse-for-work-too
2•cebert•10m ago•0 comments

Show HN: SnapCan – Get AI Photos of You "Anywhere" in the World (iOS)

https://snapcan.app/
1•marsorange•12m ago•0 comments

Tips for Building Better Enterprise GraphRAG Pipelines with Memgraph CTO

https://www.graphgeeks.org/blog/graphrag-pipelines
1•taubek•14m ago•0 comments

Agent Skills Changed How I Work with AI

https://everything.intellectronica.net/p/agent-skills-changed-how-i-work-with
1•intellectronica•14m ago•0 comments

Show HN: WatchLLM – Debug AI agents step-by-step with cost attribution

1•Kaadz•15m ago•0 comments

Show HN: VoGen – A web app for hyper-realistic voice generating and cloning

https://www.vogen.app/
1•leoyixing•15m ago•0 comments

Open-source tool to control drones using natural language

https://github.com/evangelosmeklis/deepdrone
1•twelvechess•16m ago•1 comments

The stress management app that doesn't even pretend things are OK (cartoon)

https://www.theguardian.com/lifeandstyle/picture/2026/jan/16/stress-management-app-stephen-collin...
1•beardyw•16m ago•0 comments

New map reveals landscape beneath Antarctica in unprecedented detail

https://www.bbc.co.uk/news/articles/c9qpx2qqeq7o
1•lifeisstillgood•17m ago•0 comments

Understanding Watchmaking

https://the-naked-watchmaker-masterclass.thinkific.com/courses/understanding-watchmaking-an-intro...
1•ClaudeGustav2•17m ago•0 comments

Show HN: Proxy MCP server that lazy loads tools to save tokens

https://github.com/mquan/nimble
1•quan•18m ago•0 comments

So You've Decided to Become Dead to the World: On Becoming an Anchoress

https://medievalmarginalia.substack.com/p/so-youve-decided-to-become-dead-to
1•dmazin•23m ago•0 comments

More than 100k people urge MPs to ban social media for under-16s in UK

https://www.theguardian.com/media/2026/jan/15/people-urge-mps-ban-social-media-under-16s-uk-starmer
1•giuliomagnifico•26m ago•0 comments

Is CAPTCHA still a thing in 2026 (Captcha 3.5)?

1•jarirajari•31m ago•1 comments

Tool Search Now in Claude Code

https://twitter.com/trq212/status/2011523109871108570
1•michaelmior•31m ago•0 comments

How the Base UI team prevents UI regressions at scale

https://argos-ci.com/customers/mui
1•neoziro•32m ago•0 comments

Crane Lowers Rocq Safely into C++ [pdf]

https://bloomberg.github.io/crane/papers/crane-rocqpl26.pdf
2•pjmlp•33m ago•0 comments

VS Code extensions to code with AI that only a few know about

https://diploi.com/blog/exploring-7-lesser-known-ai-coding-extensions
1•javierpresnsr•33m ago•1 comments

Show HN: Free AI Image Upscaler (100% local, private, and free)

https://freeaitoolforthat.com/ai-image-upscaler
2•tamnv•39m ago•2 comments

Wikimédia to Partner with Amazon, Meta, Microsoft, Mistral AI, and Perplexity

https://enterprise.wikimedia.com/blog/wikipedia-25-enterprise-partners/
1•mrtnmrtn•40m ago•0 comments

Hands-On: Exploit RISC-V CPU Using Return-Oriented Programming

https://www.bogdandeac.com/hands-on-exploit-risc-v-cpu-using-return-oriented-programming/
2•bg2d•42m ago•0 comments

Promoting AI Agents

https://world.hey.com/dhh/promoting-ai-agents-3ee04945
1•kiyanwang•42m ago•0 comments

On Being a Human Being in the Time of Collapse (2022) [pdf]

https://web.cs.ucdavis.edu/~rogaway/papers/crisis/crisis.pdf
32•barishnamazov•42m ago•2 comments

The Magic of the Better Software Conference

https://www.rfleury.com/p/the-magic-of-the-better-software
1•kruuuder•43m ago•0 comments

Chrome 145 Adds Experimental Support for Vertical Tabs

https://www.bram.us/2026/01/16/chrome-145-adds-experimental-support-for-vertical-tabs/
2•tobr•43m ago•0 comments

I vibed a CMS with live-preview/users/click-through-edit in an afternoon

https://github.com/mj1618/yolo-cms
2•mj2718•46m ago•1 comments