frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

You can issue a 15-year SSL certificate today. Why almost nobody does

1•panelica•4m ago•1 comments

What it takes to run an AI coworker on iMessage

https://opencomputer.dev/blog/what-it-takes-to-run-an-ai-coworker-on-imessage/
1•iacguy•5m ago•0 comments

The Bitcoin Governance Event Horizon

https://earthchronicles.substack.com/p/the-bitcoin-governance-event-horizon
1•taguniversalsw•6m ago•0 comments

Megalodon: Mass GitHub Repo Backdooring via CI Workflows

https://safedep.io/megalodon-mass-github-repo-backdooring-ci-workflows/
1•pabs3•20m ago•0 comments

Vince Is Dead (2023)

https://tildas2.tildas.org/art/sad/dead.htm
1•kniffy•22m ago•1 comments

Instant YouTube channel analysis using public metrics

1•Aafy•25m ago•0 comments

Draft – Teams of BYOA Collaborating and Building

https://foundryworks.dev/
1•trilobyte•27m ago•1 comments

Didgeridoo playing as alternative treatment for obstructive sleep apnea(2006)

https://pmc.ncbi.nlm.nih.gov/articles/PMC1360393/
2•kelseyfrog•27m ago•0 comments

Google is currently struggling to define words like disregard, stop and ignore

https://www.engadget.com/2179762/google-is-currently-struggling-to-define-words-like-disregard-st...
2•mattas•35m ago•6 comments

Show HN: 1 big microfilm of WAR.GOV/UFO files - 332,144 pages

https://hypergrid.systems/war.gov-ufo-viewer/microfilm5
1•keepamovin•38m ago•0 comments

Malicious Postinstall Hook Found in 700 GitHub Repos, Including Node Projects

https://socket.dev/blog/malicious-postinstall-hook-found-across-700-github-repos
2•882542F3884314B•45m ago•1 comments

FigMirror – Plot your data in a reference paper's style

https://github.com/VILA-Lab/FigMirror
1•xiaohan_zhao•46m ago•0 comments

Shelf – Describe a tool in plain English, get a local app forever

https://getmyshelf.app/
2•nagabandaru•50m ago•2 comments

94% companies will keep spending on AI even when it fails

https://readuncut.com/94-will-keep-spending-on-ai-even-when-it-fails/
3•jslat•54m ago•0 comments

Trump directs legal migrants to return to home country to apply for green cards

https://thehill.com/homenews/administration/5891580-legal-migrants-green-cards/
7•KnuthIsGod•55m ago•2 comments

Laravel-Lang Supply Chain Attack

https://github.com/Laravel-Lang/http-statuses/issues/277
1•varunsharma07•59m ago•1 comments

Nano: Coding agent in under 200 lines

https://github.com/pnegahdar/nano
1•kristianp•59m ago•0 comments

The abstractions in SICP need a revisit

https://www.khola.blog/p/sicp-an-architectural-trace-of-pointer
6•bcapchickadee•1h ago•1 comments

Denuvo has been cracked day 1

https://old.reddit.com/r/CrackWatch/comments/1tkvwbb/legobatmanlegacyofthedarkknightvoices38/
1•lazylion2•1h ago•0 comments

The Prehistory of A.I. Slop

https://www.newyorker.com/magazine/2026/05/25/the-prehistory-of-ai-slop
2•fortran77•1h ago•0 comments

Frustrated franchisee sues Pizza Hut over crappy kitchen AI

https://www.theregister.com/ai-ml/2026/05/19/frustrated-franchisee-sues-pizza-hut-over-crappy-kit...
4•gnabgib•1h ago•1 comments

WordPress 7.0

https://wordpress.org/download/releases/7-0/
1•Curiositry•1h ago•0 comments

Show HN: Claude Code for Customer Support

3•darweenist•1h ago•0 comments

SF Bay Ferry

https://sanfranciscobayferry.com/
1•Austin_Conlon•1h ago•0 comments

FBI director's Based Apparel site has been spotted hosting a 'ClickFix' attack

https://www.pcmag.com/news/kash-patels-apparel-site-is-trying-to-trick-visitors-into-installing-m...
39•bilalq•1h ago•10 comments

Why Your Calls to Congress Matter More Than You Think [Video]

https://odysee.com/@techlore:3/why-your-calls-to-congress-matter-more:6
1•Cider9986•1h ago•0 comments

Notiqo – Visual analytics tracking validation directly from Figma

https://notiqoapp.com/
1•abeltarazona•1h ago•0 comments

Is the Government Running a Dragnet on VPN Users? [video]

https://www.youtube.com/watch?v=hV9QEVf6CgI
1•Cider9986•1h ago•0 comments

The Resilience Premium

https://artsabintsev.substack.com/p/the-resilience-premium
1•Arts86•1h ago•0 comments

Show HN: Agentikus

https://agentikus.com
1•Modecir•1h ago•0 comments