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.

Show HN: Tachyon – A C++ JSON parser matching simdjson's throughput via AVX-512

https://github.com/wilkolbrzym-coder/Tachyon.JSON
1•wilkolbrzym•17s ago•0 comments

Multi REPL Sessions in Calva for Clojure [video]

https://www.youtube.com/watch?v=36dTtyfa_OY
1•TheWiggles•1m ago•0 comments

Maine company in the spotlight after Maduro apparently wore one of their hoodies

https://www.boston.com/news/business/2026/01/06/maine-company-maduro-venezuela-hoodie/
2•mhb•2m ago•0 comments

Researchers develop efficient, reusable compound for capturing CO2 from air

https://phys.org/news/2025-12-efficient-reusable-compound-capturing-carbon.html
2•PaulHoule•2m ago•0 comments

Grok Is Generating Sexual Content More Graphic Than What's on X

https://www.wired.com/story/grok-is-generating-sexual-content-far-more-graphic-than-whats-on-x/
2•doener•3m ago•0 comments

Extracting books from production language models

https://arxiv.org/abs/2601.02671
2•articsputnik•4m ago•0 comments

Dialogue Between a Developer and a Kid

https://riggraz.dev/dialogue-developer.html
2•todsacerdoti•5m ago•0 comments

Show HN: Agent Workflow Language – Open-Source Scala DSL for LLM Orchestration

https://awfl.us
2•Dezmo•5m ago•0 comments

ChatGPT Health Is a Marketplace, Guess Who Is the Product?

https://consciousdigital.org/chatgpt-health-is-a-marketplace-guess-who-is-the-product/
2•yoaviram•6m ago•0 comments

Show HN: Nucleus - AI prompt framework

https://github.com/michaelwhitford/nucleus
1•dulakian•8m ago•0 comments

Ask HN: Why has "using" gone out of fashion?

2•bf9d413906•9m ago•0 comments

JPMorgan Chase Reaches Deal to Take over Apple Credit Card

https://www.wsj.com/finance/banking/jpmorgan-chase-reaches-a-deal-to-take-over-the-apple-credit-c...
1•throw0101d•10m ago•0 comments

The Tyranny of the Complainers

https://marginalrevolution.com/marginalrevolution/2026/01/the-tyranny-of-the-complainers.html
1•mhb•10m ago•1 comments

Show HN: GhostMark – A bookmark manager that uses GitHub Gists as its database

https://github.com/abishekvenkat/ghostmark
1•abishekvenkat•11m ago•0 comments

How far can decentralized training over the internet scale?

https://epoch.ai/gradient-updates/how-far-can-decentralized-training-over-the-internet-scale
1•gmays•12m ago•0 comments

Interactive map: the EU's banned pesticide trade

https://unearthed.greenpeace.org/2025/12/16/interactive-map-eu-banned-pesticide-trade/
1•stareatgoats•13m ago•0 comments

Ask HN: Feeling irrelevant in back end. How to pivot to automotive software?

1•culopatin•14m ago•1 comments

Show HN: MirrorMate – A self-hosted AI smart mirror with local LLMs

https://github.com/orangekame3/mirrormate
1•orangekame3•16m ago•0 comments

NASA considers whether to bring sick crew member back to Earth

https://www.bbc.co.uk/news/articles/cx2l9perw68o
2•fredoralive•17m ago•0 comments

Show HN: Input password by twist your MacBook's lid

https://github.com/huanglizhuo/Flappy.Lid
2•huang4fun•17m ago•0 comments

The C-word: What security people don't get about compliance

https://bfswa.substack.com/p/the-c-word
1•zdw•18m ago•0 comments

Splitting a Linux Kernel Package

https://blog.brixit.nl/splitting-a-linux-kernel-package/
1•speckx•18m ago•0 comments

Neovim as a Window Manager

https://www.reddit.com/r/neovim/s/CotIhGMeIb
1•fork-bomber•18m ago•0 comments

Against Bravery Debates (2013)

https://slatestarcodex.com/2013/05/18/against-bravery-debates/
1•cassepipe•19m ago•0 comments

'Autofocus' glasses can change their lenses in real time

https://www.cnn.com/science/autofocus-glasses-ixi-change-lenses-spc
2•Brajeshwar•19m ago•0 comments

Flexible mimics octopus skin with nanoscale color and texture transformations

https://phys.org/news/2026-01-flexible-material-mimics-octopus-skin.html
1•Brajeshwar•20m ago•0 comments

Thank You for Being Annoying

https://www.experimental-history.com/p/thank-you-for-being-annoying
1•tmseidman•20m ago•0 comments

The first commercial space stations will start orbiting Earth in 2026

https://www.newscientist.com/article/2509494-the-first-commercial-space-stations-will-start-orbit...
1•ashishgupta2209•22m ago•0 comments

The equation of making money changed forever in 2026

https://hel1.your-objectstorage.com/buildsherpa/media/ckeditor_uploads/2026/01/08/new-reality.jpg
2•bayeslaw•22m ago•2 comments

Show HN: I built a health score for 1,800 Laravel packages

https://laraplugins.io
1•danielpetrica•22m ago•0 comments