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.

"There are more Japanese [VRChat] creators than all other countries combined "

https://twitter.com/chyadosensei/status/2001356290531156159
1•numpad0•1m ago•0 comments

Why This Mario Maker Level Will Never Be Beaten [video]

https://www.youtube.com/watch?v=BllSbDg7Y6I
1•minimaxir•1m ago•0 comments

Ask HN: What are good presents/projects for 10yo?

1•mezod•2m ago•0 comments

The Way of the Tiger – "Please Insert Pole Fighting"

https://superchartisland.com/the-way-of-the-tiger/
1•elvis70•2m ago•0 comments

Apple TV's New Fanfare

https://daringfireball.net/2025/12/apple_tv_new_fanfare
4•tosh•5m ago•1 comments

Show HN: A tool to practice video interviews (looking for feedback)

https://speak-app-479917.web.app/
1•emanuelaromano•6m ago•0 comments

Why do commercial spaces sit vacant?

https://archive.strongtowns.org/journal/2025/5/21/why-do-commercial-spaces-sit-vacant
1•NaOH•6m ago•0 comments

Elo System for Poker

https://pokerino.io
1•swupel•7m ago•1 comments

Helm Cheat Sheet

2•eon01•7m ago•0 comments

Latent Collaboration in Multi-Agent Systems

https://arxiv.org/abs/2511.20639
1•walterbell•7m ago•0 comments

Mistral Small Creative

https://docs.mistral.ai/models/mistral-small-creative-25-12
2•cmoriam•7m ago•2 comments

2025 in Review: A Year of Events

https://docs.eventsourcingdb.io/blog/2025/12/18/2025-in-review-a-year-of-events/
1•goloroden•11m ago•0 comments

AIs were left to build their own village and the weirdest civilisation emerged

https://www.sciencefocus.com/future-technology/ai-agents-village
1•ryan_j_naughton•12m ago•0 comments

Ask HN: Are LLMs worth more than the entire tech industry?

1•ivape•12m ago•0 comments

Parents of sextortion victim sue Meta for alleged wrongful death

https://www.theguardian.com/uk-news/2025/dec/17/parents-of-sextortion-victim-sue-meta-for-alleged...
3•n1b0m•12m ago•0 comments

File d'attente – file-based job queue tool

https://git.sr.ht/~marcc/filed
1•birdculture•18m ago•0 comments

The Last Job on Earth [video]

https://www.youtube.com/watch?v=dcolM6W5Odc
1•Deprogrammer9•19m ago•1 comments

Discrete Geometric Origins of Standard Model Mass Ratios from Jeandel-Rao Tiling

https://zenodo.org/records/17958101
1•rikeanimer•20m ago•1 comments

Pornhub extorted after hackers steal Premium member activity data

https://www.bleepingcomputer.com/news/security/pornhub-extorted-after-hackers-steal-premium-membe...
2•coloneltcb•21m ago•0 comments

Built a small tool to decouple prompts from code

https://github.com/mkarots/grompt
1•cepstrum9•25m ago•1 comments

Debugging AWS API Gateway HTTP with OIDC-JWT Authorizers

https://loige.co/debugging-api-gateway-http-oidc-jwt-authorizer/
1•speckx•25m ago•0 comments

Show HN: Minimal DL library in C – 24 NAIVE CUDA/CPU ops, autodiff, Python API

https://github.com/IaroslavElistratov/ml-systems-course
1•iaroo•25m ago•0 comments

The Real Story of the WW I Christmas Truce

https://www.iwm.org.uk/history/the-real-story-of-the-christmas-truce
2•petethomas•25m ago•0 comments

The Inference Efficiency Paradox

https://substack.com/inbox/post/181904932
2•anima-core•26m ago•0 comments

Show HN: A focus timer that adapts to circadian rhythm (WIP)

https://flowcycle.vercel.app
1•adamaskun•27m ago•0 comments

FIFA Arrives on Netflix Games

https://www.netflix.com/tudum/articles/fifa-mens-world-cup-2026-game-on-netflix
7•0xedb•29m ago•1 comments

Learn Lisp/Fennel Programming Against Neovim

https://github.com/humorless/fennel-fp-neovim
1•veqq•30m ago•0 comments

Show HN: 16 year old building a SaaS

https://crovise.netlify.app/
2•adamoufkir•30m ago•1 comments

Why Consciousness Evolved

https://neurosciencenews.com/consciousness-evolution-neuroscience-30055/
3•stevenjgarner•33m ago•0 comments

Selective Applicative Functors

https://blog.veritates.love/selective_applicatives_theoretical_basis.html
1•todsacerdoti•34m ago•0 comments