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.

Pushing the smallest possible change to production

https://ankursethi.com/blog/smallest-possible-change/
1•todsacerdoti•53s ago•0 comments

Log Levels: A Better Strategy with OpenTelemetry

https://www.dash0.com/knowledge/log-levels
1•ayoisaiah•1m ago•0 comments

Vercel Launches Skills – "NPM for AI Agents" with React Best Practices Built-In

https://jpcaparas.medium.com/vercel-launches-skills-npm-for-ai-agents-with-react-best-practices-b...
1•zenoware•9m ago•0 comments

The Instant Pot Failed Because It Was a Good Product (2023)

https://www.theatlantic.com/technology/archive/2023/06/instant-pot-bankrupt-private-equity/674414/
1•rwmj•9m ago•1 comments

The 600-year-old origins of the word 'hello'

https://www.bbc.com/culture/article/20260113-hello-hiya-aloha-what-our-greetings-reveal
2•1659447091•11m ago•0 comments

Ask HN: Favorite YouTube Channels?

1•chistev•17m ago•0 comments

Ask HN: Any tools for managing multi Claude Code instances?

1•tansan•19m ago•0 comments

Why Saunas, Bathhouses, Cold Plunge Pools Are So Popular

https://www.bloomberg.com/news/features/2026-01-16/why-saunas-bathhouses-cold-plunge-pools-are-so...
1•Anon84•20m ago•0 comments

Show HN: Termilingo – a terminal-based spaced repetition flashcard app

https://github.com/ayakovlenko/termilingo
1•ayakovlenko•20m ago•0 comments

Case Study: Hydration Latency in Enterprise E-Commerce (Nike vs. New Balance)

https://websiteaiscore.com/blog/nike-vs-new-balance-technical-audit
1•aggeeinn•28m ago•1 comments

Photo was taken in 1911 using glass plate technology

https://bsky.app/profile/alanjstedman.bsky.social/post/3mcm2qdizg22u
4•doener•28m ago•1 comments

Don't be a human alarm clock

https://mannhowie.com/human-alarm-clock
1•elephant_burger•30m ago•0 comments

WhisperPair: Hijacking Bluetooth Accessories Using Google Fast Pair

https://whisperpair.eu/
2•xerosic•31m ago•0 comments

Vercel's Web Interface Guidelines

https://vercel.com/design/guidelines
1•handfuloflight•32m ago•0 comments

I used an agent to hunt vulns

https://blazelight.dev/blog/agent-vuln-hunting.mdx
1•theblazehen•33m ago•0 comments

Fitdrop: Personal exploration of fashion from 1980 to 2025

https://fitdrop.cc/
2•num42•34m ago•0 comments

What predicts success in AI coding? (Analysis of 4.6k amp threads)

https://amp-analysis-casestudy.vercel.app/
1•njpatel•36m ago•0 comments

Incarnation Lease

https://atmankalena.substack.com/p/incarnation-lease
1•Trifectorium•38m ago•1 comments

Why Mixing Colors on Your Computer Makes Mud

https://spectraljs.com/blog/why_mixing_colors_on_your_computer_makes_mud/
2•rvanwijnen•40m ago•0 comments

Architecture for Disposable Systems

https://tuananh.net/2026/01/15/architecture-for-disposable-systems/
3•tuananh•44m ago•1 comments

The modern, full-stack TypeScript framework that makes T3 Stack look like 2022

https://github.com/yazcaleb/c4-template
2•plawlost•46m ago•0 comments

Multi armed bandit resource allocation in Near Memory Processing architectures

https://www.sciencedirect.com/science/article/pii/S277306462500012X
4•rbanffy•47m ago•0 comments

Cold Truths

https://newsletter.doomberg.com/p/cold-truths
1•simonebrunozzi•47m ago•0 comments

ASCII characters are not pixels: a deep dive into ASCII rendering

https://alexharri.com/blog/ascii-rendering
42•alexharri•48m ago•2 comments

Gut micro-organisms associated with health, nutrition and dietary intervention

https://www.nature.com/articles/s41586-025-09854-7?lid=t94o71j7gslg
5•lonelyasacloud•48m ago•1 comments

A collection of my weird, fun, and fast-built side projects

https://www.pankajtanwar.in/side-hustles
2•jaynate•50m ago•0 comments

AV1 Image File Format Specification Gets an Upgrade with AVIF v1.2.0

https://aomedia.org/blog%20posts/AV1-Image-File-Format-Specification-Gets-an-Upgrade-with-AVIF/
4•breve•53m ago•0 comments

Ask HN: Is there a free MCP for web and documentation search?

1•terabytest•54m ago•1 comments

Show HN: Curated index page for my subdomains

https://index.priyavkaneria.com/
1•diginova•54m ago•0 comments

Your 'Ideal Customer Profile' Is a Hallucination

https://pathak.ventures/essays/the-segment-of-one
1•ninadpathak•55m ago•0 comments