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.

Ask HN: Why do so many people dislike the Opera browser?

1•tavro•12s ago•0 comments

Show HN: Sniptail – Turn Slack into a team interface for AI coding agents

https://github.com/Justkog/sniptail
1•Justkog•29s ago•0 comments

How to teach Claude to write better code

https://www.ponylang.io/blog/2026/02/teaching-claude-to-write-pony/
1•carlsverre•55s ago•0 comments

Urgent recall for trendy supplement over contamination with deadly bacteria

https://www.dailymail.co.uk/health/article-15571317/Urgent-recall-trendy-supplement-moringa-salmo...
1•Bender•1m ago•0 comments

Show HN: Data Processing Recipes for Edge Computing and AI Agents

1•TheIronYuppie•1m ago•0 comments

We need to act with urgency to address the growing AI divide

https://blogs.microsoft.com/on-the-issues/2026/02/17/acting-with-urgency-to-address-the-growing-a...
1•layer8•3m ago•0 comments

How Personal AI Agents and Agent Orchestrators Like OpenClaw or GasTown Are Made

https://gist.github.com/championswimmer/bd0a45f0b1482cb7181d922fd94ab978
1•simonpure•3m ago•0 comments

Notes on Clarifying Man Pages

https://jvns.ca/blog/2026/02/18/man-pages/
1•surprisetalk•4m ago•0 comments

Tesla avoids 30-day California sales suspension

https://electrek.co/2026/02/18/tesla-avoids-30-day-california-sales-suspension-after-dropping-mis...
1•Bender•4m ago•1 comments

Show HN: NarrateNow – Add AI audio to any blog post with a single script tag

https://narratenow.app
2•JoshTheNerd•5m ago•0 comments

Minimalist Design for Space Camera Flight Software

https://queue.acm.org/detail.cfm?id=3786613
2•matt_d•6m ago•0 comments

Turbocharging PostgreSQL Listen/Notify with 40x Boost

https://www.robins.in/2026/01/turbocharging-listennotify-with-40x.html
2•tanelpoder•6m ago•0 comments

Thinking Improves Thinking

https://idiallo.com/blog/taking-our-mind-for-granted
2•foxfired•6m ago•0 comments

PatchworkMCP – Agents report what's missing from your MCP server

https://github.com/keyton-weissinger/patchworkmcp
2•keytonw•7m ago•1 comments

How did we end up threatening our kids' lives with AI?

https://www.anildash.com/2026/02/18/threatening-kids-with-ai/
3•speckx•10m ago•0 comments

Next-single-file – Make a Next.js project into one HTML file with Regex (0 deps)

https://github.com/simples-tools/next-single-file
2•brrock•11m ago•0 comments

Rtk – High-performance CLI proxy to minimize LLM token consumption

https://github.com/rtk-ai/rtk
2•giancarlostoro•11m ago•0 comments

Monado at the Core of Android XR

https://www.collabora.com/news-and-blog/news-and-events/monado-at-the-core-of-android-xr.html
2•losgehts•12m ago•0 comments

Donald Trump's AI push fuels revolt in Maga heartlands

https://www.ft.com/content/0c9ec7b1-f9a6-41db-9493-3ff09f6943ef
2•zerosizedweasle•12m ago•0 comments

Hiring: Senior Web/Mobile Medical Device Engineer

https://innolitics.com/join/senior-web-mobile-medical-device-engineer/
2•prahlaad•13m ago•1 comments

Pestraid Kenya

https://pestraid.co.ke/
2•shemmwaka•14m ago•1 comments

Show HN: SVO Voxelization for Gaussian Splat Collisions

https://old.reddit.com/r/GaussianSplatting/comments/1r85wsx/svo_voxelization_for_gaussian_splat_c...
2•slimbuck•15m ago•0 comments

Updating the Verge's Background Policy

https://www.theverge.com/press-room/22772113/the-verge-on-background-policy-update
3•eric_h•16m ago•0 comments

Atlassian freezes hiring amid global software sell-off

https://www.afr.com/technology/atlassian-freezes-hiring-amid-global-software-sell-off-20260216-p5...
2•TMWNN•16m ago•0 comments

Hashgrid MCP – Discovery Network for Agents

https://github.com/hashgrid-labs/sdk/tree/main/mcp
2•bicsi•16m ago•1 comments

My Honest Views

https://colinmcginn.net/my-honest-views/
2•bookofjoe•16m ago•0 comments

Private Software Companies Release Earnings Early to Calm AI Nerves

https://www.bloomberg.com/news/articles/2026-02-17/private-software-companies-open-their-books-ea...
2•TMWNN•17m ago•0 comments

WebWorld: A Large-Scale World Model for Web Agent Training

https://arxiv.org/abs/2602.14721
2•gmays•22m ago•0 comments

Microsoft team creates 'revolutionary' data storage system that lasts millennia

https://www.nature.com/articles/d41586-026-00502-2
5•gnabgib•22m ago•1 comments

The political effects of X's feed algorithm

https://werd.io/the-political-effects-of-xs-feed-algorithm/
5•benwerd•22m ago•0 comments