frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Trzsz-SSH (tssh): An SSH Client Alternative

https://github.com/trzsz/trzsz-ssh
1•csmantle•47s ago•0 comments

The "Bucket Bumping" problem of airline tickets, and how to minimise your fare

https://www.dodgycoder.net/2026/01/the-bucket-bumping-problem-of-airline-tickets.html
2•damian2000•3m ago•0 comments

Pre-Agent Nostalgia

https://www.pcloadletter.dev/blog/pre-coding-agent-nostalgia/
2•ronbenton•3m ago•0 comments

Privacy Engineering at Scale: Building Automated Data Retention Systems

https://medium.com/@sandhyavinjam/privacy-engineering-at-scale-building-automated-data-retention-...
1•sandhyavinjam•15m ago•0 comments

There's no corpus large enough

https://www.swiftcraft.io/articles/no-corpus-large-enough
1•rad_val•16m ago•0 comments

Mermaid as a programming language for AI agents

https://twitter.com/xiaoxxchan/status/2011825791408226618
1•xxchan22•19m ago•0 comments

Show HN: N.codes – Let users generated mini-apps in your app

1•yungookim•22m ago•0 comments

Show HN: PixelMotion:AI video generation with Sora 2, Veo 3.1, and 9 more models

https://www.pixelmotion.io/
1•anotherbuilder•25m ago•0 comments

Tesla investigates whether its self-driving technology caused traffic violations

https://www.latimes.com/business/story/2026-01-16/tesla-is-probing-if-its-self-driving-technology...
1•MilnerRoute•26m ago•0 comments

I built a proper UI for e-com product image generation (chat doesn't scale)

https://aimockupgenerator.co/
1•dairis•26m ago•1 comments

Ask HN: Could we replace Job Descriptions with actual Git Issues?

1•A1aM0•27m ago•0 comments

Gemini Research MCP Server

https://github.com/fortaine/gemini-research-mcp
2•gfortaine•30m ago•0 comments

New Framework Proposes Spacetime as an Emergent Irreversible Information Process

https://zenodo.org/records/17911993
1•emsti•34m ago•2 comments

Show HN: Webship.dev – custom web systems, legacy code and performance

https://webship.dev/
1•pragidi•36m ago•0 comments

Disqus but Using GitHub. Cool Idea

https://giscus.app/
2•razodactyl•38m ago•0 comments

High-speed train crash in southern Spain leaves at least 21 dead

https://www.theguardian.com/world/2026/jan/18/high-speed-train-crash-in-adamuz-cordoba-southern-s...
3•perihelions•39m ago•0 comments

Show HN: Shared Device Group – scheduler-level GPU sharing for Kubernetes

https://github.com/sceneryback/shared-device-group
1•ChuangLabs•40m ago•0 comments

Clip to Siglip > Migrating Our 200M+ MultiModal Embeddings

https://huggingface.co/vulturelabs/vector-rosetta-clip-vit-base-patch32-to-siglip-vit-base-patch1...
1•teocalin37•46m ago•1 comments

Bring Back Ops Pride

https://substack.com/home/post/p-184825970
3•gpi•48m ago•0 comments

Your Problem framing is sabotaging your strategy

https://productpicnic.beehiiv.com/p/your-problem-framing-is-sabotaging-your-strategy
3•gpi•49m ago•0 comments

SFU prof launches legal-AI collaboration with Caseway to improve justice

https://www.sfu.ca/fas/computing/news-events/news/2026/january/sfu-launches-legal-ai-collaboratio...
1•ClearwayLaw•51m ago•1 comments

Semihemidemisemiquaver

https://en.wikipedia.org/wiki/Hundred_twenty-eighth_note
1•petethomas•57m ago•0 comments

Ask HN: How many local logins do you have on your computer?

2•bahmboo•1h ago•5 comments

Show HN: I quit coding years ago. AI brought me back

https://calquio.com
17•ivcatcher•1h ago•14 comments

Repo: Language Models with Context Re-Positioning

https://pub.sakana.ai/repo/
1•hardmaru•1h ago•0 comments

The Paper

https://zenodo.org/records/18294248
2•KaoruAK•1h ago•0 comments

Metro MCP: MCP Server for DC and NYC Metro

https://metro-mcp.anuragd.me/
2•Aarekaz•1h ago•0 comments

Claude Is Taking the AI World by Storm, and Even Non-Nerds Are Blown Away

https://www.wsj.com/tech/ai/anthropic-claude-code-ai-7a46460e
4•alhazrod•1h ago•4 comments

YouTube is silently deleting uploaded SRV3 (styled) subtitles

https://old.reddit.com/r/youtube/comments/1qdvgmc/uploading_srv3_subtitles_got_brokendisabled/
9•slowdog•1h ago•2 comments

Show HN: Why I forked Gemini CLI - a FOSS Cowork alt that *is* the OS

https://github.com/Prof-Harita/terminaI
1•ProfHarita•1h ago•1 comments