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.

HiRTOS: A high-integrity multi-core RTOS kernel written in SPARK Ada

https://github.com/jgrivera67/HiRTOS
1•jacques_chester•3m ago•0 comments

Multiplying our way out of division

https://xania.org/202512/07-division-again
1•ibobev•7m ago•0 comments

Show HN: I replaced my premium workout app with vibecode

https://strengthquest.lovable.app/
2•maddmann•7m ago•0 comments

NY judge orders ChatGPT conversation handover in newspaper copyright win

https://www.nydailynews.com/2025/12/03/ny-judge-orders-openai-to-hand-over-chatgpt-conversations-...
3•gnabgib•16m ago•0 comments

Oath of the Horatii

https://en.wikipedia.org/wiki/Oath_of_the_Horatii
1•andsoitis•17m ago•0 comments

AI chatbots can sway voters better than political advertisements

https://www.technologyreview.com/2025/12/04/1128824/ai-chatbots-can-sway-voters-better-than-polit...
1•gnabgib•20m ago•0 comments

Linux GPIB Drivers Declared Stable 53 Years After HP Introduced the Bus

https://www.phoronix.com/news/GPIB-De-Staged-Linux-6.19
2•LorenDB•21m ago•0 comments

Spinlocks vs. Mutexes: When to Spin and When to Sleep

https://howtech.substack.com/p/spinlocks-vs-mutexes-when-to-spin
7•birdculture•24m ago•0 comments

What Folk Can Do

https://folk.computer/guides/what-folk-can-do
2•luu•25m ago•1 comments

List of Common Misconceptions (Wikipedia)

https://en.wikipedia.org/wiki/List_of_common_misconceptions
3•greazy•27m ago•0 comments

Energy efficiency task scheduling algorithm for multi-core embedded platforms

https://www.sciencedirect.com/science/article/abs/pii/S0045790625008298
1•stevenjgarner•29m ago•1 comments

A Look into NASA's Coding Philosophy (2017)

https://observer.com/2017/07/a-look-into-nasa-coding-philosophy-kennedy-space-center-programming/
2•kristianp•31m ago•0 comments

Toyota Unintended Acceleration and the Big Bowl of "Spaghetti" Code(2013)

https://www.safetyresearch.net/toyota-unintended-acceleration-and-the-big-bowl-of-spaghetti-code/
2•SoKamil•31m ago•1 comments

The Ilya Sutskever interview – my key takeaways

https://quickchat.ai/post/ilya-sutskever-interview
1•piotrgrudzien•36m ago•0 comments

Show HN: Cdecl-dump - represent C declarations visually

https://github.com/bbu/cdecl-dump
1•bluetomcat•37m ago•0 comments

An Attempt at a Compelling Articulation of Forth's Practical Strengths and Eter

https://im-just-lee.ing/forth-why-cb234c03.txt
2•todsacerdoti•37m ago•0 comments

Algebraic Constraints [pdf]

http://reports-archive.adm.cs.cmu.edu/anon/scan/CMU-CS-83-132.pdf
1•andsoitis•38m ago•0 comments

A Grand Social Media Experiment Begins in Australia

https://www.nytimes.com/2025/12/07/world/asia/australia-social-media-ban-under-16.html
1•apparent•38m ago•1 comments

The era of jobs is ending

https://www.thepavement.xyz/p/the-era-of-jobs-is-ending
4•SturgeonsLaw•39m ago•3 comments

India's request for satellite-aided iPhone location data is a privacy nightmare

https://appleinsider.com/articles/25/12/06/indias-request-for-satellite-aided-iphone-location-dat...
3•walterbell•44m ago•0 comments

Network extensible Window System (1986)

https://en.wikipedia.org/wiki/NeWS
1•hbbio•45m ago•1 comments

Show HN : WealthYogi - Net worth Tracker

https://apps.apple.com/gb/app/wealthyogi-net-worth-tracker/id6753881658
2•aalbatross•49m ago•0 comments

Show HN: LLM-Powered Log Analysis Wrapper (Python)

https://github.com/IncidentAI-Dev/incident-summarizer-wrapper/blob/main/README.md
1•joe117•53m ago•1 comments

Does hockey tape hide fingerprints on weapons?

1•pointbob•59m ago•5 comments

Data Processing Inequalities and Function-Space Variational Inference (2023)

https://blog.blackhc.net/2023/08/sdpi_fsvi/
1•measurablefunc•1h ago•0 comments

Transformers Are Multi-State RNNs

https://huggingface.co/papers/2401.06104
1•tesserato•1h ago•0 comments

Analyze Your Domain Authority

https://domainrank.app
1•wantering•1h ago•0 comments

Array Signal Processing: Concepts and Techniques

https://archive.org/details/arraysignalproce0000john
1•teleforce•1h ago•0 comments

Indus script (Harappan script, Indus Valley script)

https://en.wikipedia.org/wiki/Indus_script
3•1659447091•1h ago•0 comments

When exercising copyrights puts a gamedev under threat: My take on GBCOMPO 25

https://allalonegamez.itch.io/zoryad/devlog/1135761/when-exercising-copyrights-puts-a-gamedev-und...
11•embedding-shape•1h ago•1 comments