frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Why the Future of AI Depends on Smaller Models

https://jeevan.life/posts/why-the-future-of-ai-depends-on-smaller-models.html
1•lroe•1m ago•0 comments

The agent principal-agent problem

https://crawshaw.io/blog/agent-principal-agent
1•bumbledraven•3m ago•0 comments

Show HN: Sfera – sliding puzzle in curved space (open-sourced)

https://github.com/bntre/Sfera
1•bntr•3m ago•0 comments

SubQ: Sub-quadratic LLM built for 12M-token reasoning

https://subq.ai/
1•anujbans•6m ago•0 comments

Camera

https://online-camera.com
2•ent101•10m ago•0 comments

KDE Plasma 6.7 to Provide a Better Experience for CPU-Based Rendering

https://www.phoronix.com/news/KDE-Plasma-Faster-WL-SHM
2•doener•12m ago•0 comments

Live Global Hantavirus Outbreak Tracker

https://hantavirusmap.com
1•N19PEDL2•13m ago•0 comments

Interviewing/Applying to YC

1•chanlee514•17m ago•0 comments

I tried the viral 'jaw release' fascial massage

https://www.thetimes.com/life-style/health-fitness/article/jaw-release-facial-massage-v7rmgqsb9
2•petethomas•17m ago•0 comments

Ask HN: Who Uses a todo.txt?

2•mikenikles•17m ago•0 comments

Why does AI like goblins and Japan so much?

https://english.elpais.com/technology/2026-05-07/why-does-ai-like-goblins-and-japan-so-much.html
2•geox•18m ago•0 comments

Dirty Frag: Universal Linux LPE

https://github.com/V4bel/dirtyfrag
1•danaris•22m ago•0 comments

Root-based Android app backup tool powered by Restic

https://github.com/hddq/restoid
1•anotherevan•25m ago•0 comments

A man who blew up a nuclear power station and disappeared

https://www.theguardian.com/world/2026/may/05/the-man-who-blew-up-a-nuclear-power-station-koeberg...
3•Michelangelo11•33m ago•0 comments

Pronouns in Perl (2009)

https://www.modernperlbooks.com/mt/2009/11/from-novice-to-adept-pronouns-in-perl
1•tosh•33m ago•0 comments

Discussion on Fsnotify Maintainer Removal

https://github.com/fsnotify/fsnotify/issues/757
1•mjyut•34m ago•1 comments

3 Women Who Fundamentally Shaped Modern Software [Reddit!!!]

https://old.reddit.com/r/compsci/comments/1t6jo79/3_women_who_fundamentally_shaped_modern_software/
1•signa11•35m ago•0 comments

Eating eggs could cut Alzheimer's risk by 27%

https://www.sciencedaily.com/releases/2026/05/260506225214.htm
3•y1n0•35m ago•2 comments

Can houseplants purify the air in your home?

https://phys.org/news/2026-05-houseplants-purify-air-home-science.html
2•y1n0•37m ago•0 comments

AI giveth and AI taketh CPU

https://stackoverflow.blog/2026/05/08/ai-giveth-and-ai-taketh-cpu/
1•y1n0•37m ago•0 comments

When a Meta CTO and an AWS ProServe Principal Converge on the Same Philosophy

https://www.outcomeops.ai/blogs/the-o16g-manifesto-validates-what-weve-been-building-since-july
2•linsys•40m ago•0 comments

The layoffs will continue till we learn to use AI

https://twitter.com/championswimmer/status/2051807284691612099
3•s4i•43m ago•0 comments

Attenborough: The risk-taker who changed how we see Earth

https://www.bbc.co.uk/news/resources/idt-715ff936-f52a-429d-bb35-d1940d7f6111
2•mellosouls•44m ago•0 comments

European 8mm footage from the previous century

https://old-love.eu/
3•flixz_•48m ago•0 comments

Axavive Official Site 2026: Pricing, Bonuses, and 90-Day Guarantee

https://finance.yahoo.com/sectors/healthcare/articles/axavive-skin-exploding-2026-golden-22590060...
2•farjzaty•50m ago•0 comments

Krytonum – a bare-metal C++ OS built from scratch

https://github.com/Velo-Computing-Technologies/Krytonum
2•hs100000•51m ago•0 comments

The Nature of Programming

https://medium.com/@tautvilas/what-is-programming-22a72ef4fd02
4•brisky•52m ago•0 comments

Show HN: When the LLM Accidentally

2•lucid-dev•53m ago•0 comments

Building an In-Person System (NYC)

https://www.notion.so/Building-an-In-Person-System-NYC-354c7880b1938067843defd62925fec4?source=co...
2•field-trace•53m ago•0 comments

Show HN: SafeKibble – free email alerts when your pet's food is FDA-recalled

https://www.safekibble.app/
2•akest•54m ago•1 comments