frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Show HN: AI-powered SEO automation tool distilled from production agency systems

https://www.quicklyseo.com/
1•ralphqkly•2m ago•0 comments

Show HN: I built the fastest RSS reader in Zig

https://github.com/superstarryeyes/hys
2•superstarryeyes•2m ago•0 comments

Why mistletoe is thriving, even as its traditional orchards are lost

https://theconversation.com/why-mistletoe-is-thriving-even-as-its-traditional-orchards-are-lost-2...
1•zeristor•3m ago•1 comments

YouTube Creators Find a New Consumer for AI Slop: Babies

https://www.bloomberg.com/news/articles/2025-12-03/ai-slop-youtube-videos-for-kids-pretend-to-be-...
1•paulpauper•3m ago•0 comments

What Does a Database for SSDs Look Like?

https://brooker.co.za/blog/2025/12/15/database-for-ssd.html
1•KraftyOne•5m ago•0 comments

Run Codex CLI in a firewalled Docker sandbox

https://github.com/paulux84/codex-lockbox
1•p2dev•5m ago•0 comments

The Worst Thing About the RAM Shortage That Nobody's Talking About

https://gizmodo.com/the-worst-thing-about-the-ram-shortage-that-nobodys-talking-about-2000700185
2•_____k•7m ago•0 comments

Netflix Is Buying Nostalgia

https://12gramsofcarbon.com/p/tech-things-netflix-is-buying-nostalgia
1•theahura•9m ago•0 comments

Ask HN: What's up with the "model overloaded" on Gemini API?

2•worldsavior•9m ago•0 comments

SK Hynix Internal Analysis

https://twitter.com/BullsLab/status/1998691507956756547
1•_____k•10m ago•0 comments

Hacking group says it's extorting Pornhub after stealing users' viewing data

https://techcrunch.com/2025/12/16/hacking-group-says-its-extorting-pornhub-after-stealing-users-v...
2•SilverElfin•15m ago•1 comments

Codex Is Down

https://status.openai.com/incidents/01KCM7PAMQMCM8KAB6ZCWPKNK1
2•bartkappenburg•15m ago•0 comments

Using GitLab CI/CD with a GitHub Repository

https://docs.gitlab.com/ci/ci_cd_for_external_repos/github_integration/
1•ahmgeek•15m ago•0 comments

Letta Code: a memory-first coding agent

https://github.com/letta-ai/letta-code
4•pacjam•16m ago•1 comments

Neural Networks XD in JavaScript

https://chuwon.github.io/nn/
1•bicepjai•17m ago•0 comments

Feeding the Machine

https://www.theverge.com/cs/features/831818/ai-mercor-handshake-scale-surge-staffing-companies
1•paulpauper•17m ago•0 comments

We built an internal project management system – it became Dyversal AI

1•nivafy•18m ago•1 comments

The Order Is Backwards

https://granot.io/the-order-is-backwards/
1•tomgs•18m ago•0 comments

iRobot files for bankruptcy; Bought by its Chinese Manufacturer

https://apnews.com/article/irobot-roomba-bankruptcy-picea-amazon-7ef311c0b3848af2b30ba3921496efe1
1•Zaheer•18m ago•1 comments

How to Reclaim Aesthetic Vision from the Lean Startup?

https://medium.com/@gp2030/the-lean-startup-zen-the-art-of-failing-fast-and-reclaiming-aesthetic-...
1•light_triad•20m ago•0 comments

Lessons from building a content scanner for multiple social platforms

https://keywordspal.com/blog/building-multi-platform-content-aggregator
1•binsquare•23m ago•0 comments

Audio Plugin UI Components

https://www.audio-ui.com/
1•gregsadetsky•24m ago•0 comments

Ask HN: Tech Interview Coaches Worth the Money?

1•idontwantthis•25m ago•3 comments

The Arctic Is in Dire Straits, 20 Years of Reporting Show

https://www.scientificamerican.com/article/the-arctic-is-in-dire-straits-20-years-of-reporting-show/
7•quapster•27m ago•0 comments

Bitwarden / KeePass Diff

https://codeberg.org/cicko/bwkp-diff
2•cicko•27m ago•1 comments

From Experiment to Backbone: Adopting Rust in Production

https://blog.kraken.com/product/engineering/rust-part-2-from-bet-to-backbone
2•simag•27m ago•1 comments

How to Detect Browser-as-a-Service Scrapers in 2025

https://webdecoy.com/blog/browser-as-a-service-detection-baas-ai-agents-2025/
1•cport1•28m ago•0 comments

Will Creative Work Survive A.I.?

https://www.nytimes.com/2025/12/16/opinion/artists-creative-work-ai.html
3•thm•30m ago•0 comments

The Engine Is Not the Car

https://thinking.relica.io/the-engine-is-not-the-car/
1•m-xtof•33m ago•1 comments

Why Does Everyone Want to Be a Bank Now?

https://www.bloomberg.com/news/articles/2025-10-28/why-fintech-companies-want-to-become-banks-rig...
1•petethomas•34m ago•1 comments