frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

1•ramsicandra•8mo 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•8mo 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•8mo 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: 900x downgrade of Perplexity Pro – anyone else hit?

1•pshirshov•5m ago•0 comments

I Built a System That Reads the Fine Print of 969 Corporate Legal Documents

https://leahey.org/blog/2026/02/06/tos-tracker-fine-print.html
1•tldrthelaw•5m ago•0 comments

I Chose Ruby over Python

https://batsov.com/articles/2025/09/12/why-i-chose-ruby-over-python/
1•tosh•6m ago•0 comments

Claude Code Is the Inflection Point

https://newsletter.semianalysis.com/p/claude-code-is-the-inflection-point
1•taubek•6m ago•0 comments

Show HN: GitClaw – An AI assistant that runs in GitHub Actions

https://github.com/SawyerHood/gitclaw
2•sawyerjhood•7m ago•0 comments

US job market posted the weakest growth outside of a recession since 2003

https://www.cnn.com/2026/02/05/economy/us-jobs-data-layoffs-hiring
1•speckx•7m ago•0 comments

The Chrysalis Backdoor: A Deep Dive into Lotus Blossom's Toolkit

https://www.rapid7.com/blog/post/tr-chrysalis-backdoor-dive-into-lotus-blossoms-toolkit/
2•9woc•8m ago•0 comments

macOS No Longer Ships with Emacs

https://batsov.com/articles/2025/01/12/macos-no-longer-ships-with-emacs/
1•tosh•11m ago•0 comments

Virgin Boy Egg

https://en.wikipedia.org/wiki/Virgin_boy_egg
1•2OEH8eoCRo0•11m ago•0 comments

Reading Buffer statistics in EXPLAIN output

https://boringsql.com/posts/explain-buffers/
2•enz•13m ago•0 comments

Clojure Tricks: Replace in String (2022)

https://batsov.com/articles/2022/07/31/clojure-tricks-replace-in-string/
2•tosh•14m ago•0 comments

Show HN: CopyKitten – Discord Voice Cloning

https://copykitten.gg/
1•TheSaltySeaCow•15m ago•0 comments

Bytes as Braille

https://www.engrenage.ch/i18n/scripts/bytes_as_braille/
2•apitman•15m ago•0 comments

Nvidia commits 3x more code across 30k developers with Cursor

https://cursor.com/blog/nvidia
1•onurkanbkrc•15m ago•0 comments

Show HN: SpaceMolt – an MMO that AI can play

https://www.spacemolt.com/
1•statico•16m ago•0 comments

Sourdough starters reveal a recipe for predicting microbial species survival

https://phys.org/news/2026-01-sourdough-starters-reveal-recipe-microbial.html
1•PaulHoule•16m ago•0 comments

My Programming Job Has Become an Intelligence Buying Job

https://frederic.vanderessen.com/posts/programming-is-now-buying-intelligence/
3•jlnthws•16m ago•0 comments

Date Arithmetic in Bash

https://blog.miguelgrinberg.com/post/date-arithmetic-in-bash
1•ibobev•17m ago•0 comments

anamnesis.fm – Radio from Any Era

https://anamnesis.fm/
1•wonger_•18m ago•0 comments

Steam Machine and Steam Frame delays are the latest product of the RAM crisis

https://arstechnica.com/gadgets/2026/02/ram-shortage-delays-valves-steam-machine-desktop-and-stea...
1•alsetmusic•18m ago•1 comments

What Happens When Technical Debt Vanishes?

https://ieeexplore.ieee.org/document/11316905
1•xnx•20m ago•0 comments

NASA Conducts Artemis II Fuel Test, Eyes March for Launch Opportunity

https://www.nasa.gov/blogs/missions/2026/02/03/nasa-conducts-artemis-ii-fuel-test-eyes-march-for-...
1•gmays•20m ago•0 comments

Show HN: Built a free moderation API after failing to find one

https://the-profanity-api.com/
1•Olehype•21m ago•0 comments

Why it's so hard to fuel the Artemis rockets

https://www.nationalgeographic.com/science/article/artemis-2-hydrogen-fuel-leak
1•bookofjoe•22m ago•1 comments

Sheldon Brown's Bicycle Technical Info

https://www.sheldonbrown.com/
1•ostacke•23m ago•0 comments

Tree hug dream unlocked by AI

https://tiago.rio.br/work/olxbrasil/hackday/tree-hug-dream-unlocked-ai/
1•timotta•23m ago•1 comments

Claude Trolled ChatGPT and Won

https://offmenu.substack.com/p/claude-just-trolled-chatgpt-and-won
1•gk1•24m ago•0 comments

Hiring for a Director, Operations at Rockwallet

https://secure.collage.co/jobs/rockwallet/59538
1•RockWalletHR•26m ago•0 comments

Vritual Screen: macOS utility that captures a region of your screen and mirrors

https://github.com/alexolivier/virtual-screen
1•emreb•26m ago•0 comments

NIMBYs Aren't Just Shutting Down Housing

https://inpractice.yimbyaction.org/p/nimbys-arent-just-shutting-down-housing
36•toomuchtodo•29m ago•15 comments