frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

SmallCode – A coding agent that gets 87% on benchmarks with a 4B parameter model

https://old.reddit.com/r/LocalLLaMA/comments/1tgecrq/i_built_a_coding_agent_that_gets_87_on_bench...
1•aagha•51s ago•0 comments

Your Most Improbable Life

https://kevinkelly.substack.com/p/your-most-improbable-life
2•jger15•1m ago•0 comments

Wayland Compositor in Minecraft [video]

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

Applied Discrete Structure

https://discretemath.org/
1•ibobev•2m ago•0 comments

I built a serverless WASM cloud using Nostr as a message broker

https://github.com/bmcc-DEV/RE-trolab.githttps://github.com/bmcc-DEV/ET-RNET.githttps://github.co...
1•DarthNihilus•2m ago•0 comments

DSV Limiting Factor

https://en.wikipedia.org/wiki/DSV_Limiting_Factor
1•dtj1123•2m ago•0 comments

What Do Gödel's Incompleteness Theorems Mean?

https://www.quantamagazine.org/what-do-godels-incompleteness-theorems-truly-mean-20260518/
1•baruchel•6m ago•0 comments

Decart AI's $300M Round Is a Positive Signal for Israeli AI

https://www.vccafe.com/decart-ais-300m-round-is-a-positive-signal-for-israeli-ai/↗
1•vccafe•6m ago•0 comments

Zero – the new programming language for AI – is basically Rust

https://thetechvillain.substack.com/p/zero-the-new-programming-language
1•interrupt86•7m ago•1 comments

&Mario – Digital Union for the People

https://andmario.com
1•pear01•7m ago•0 comments

Real-Time Order Flow and Whale Tracking

https://cryptoflowdata.com/
1•santys•7m ago•0 comments

Establishing a weekly user interview routine

https://destroytoday.com/blog/establishing-a-weekly-user-interview-routine
1•speckx•7m ago•0 comments

Elon Musk lost his case against Sam Altman

https://www.theverge.com/ai-artificial-intelligence/932383/jury-verdict-musk-v-altman-openai-trial
3•theahura•8m ago•0 comments

At least 100 deaths reported in Ebola outbreak in DR Congo

https://www.bbc.com/news/articles/cq6pz60p996o
2•saikatsg•8m ago•0 comments

McMansion Hell: The Devil Is in the Details

https://99percentinvisible.org/episode/mcmansion-hell-devil-details/
1•cwwc•9m ago•0 comments

Joule Index – AI benchmark for cost and Energy

https://joule.blankline.org
1•DarenWatson•9m ago•0 comments

Spec-Driven Development Workflow for Claude Code

https://github.com/sermakarevich/sddw
2•sermakarevich•9m ago•0 comments

Cutting inference cold starts by 40x with LP, FUSE, C/R, and CUDA-checkpoint

https://modal.com/blog/truly-serverless-gpus
6•charles_irl•9m ago•0 comments

Vanilla FP: The no-framework framework for building component-based UIs

https://github.com/abuseofnotation/vanilla-fp
1•boris_m•9m ago•0 comments

Iran will impose fees on subsea internet cables in Strait of Hormuz

https://www.cnn.com/2026/05/17/middleeast/iran-hormuz-undersea-cables-intl
1•ck2•9m ago•0 comments

Tag – Local-first trust and governance layer for AI agents|no cloud, no account

https://github.com/AIObuilt/TaG
1•Tag_AI•10m ago•0 comments

Ask HN: Has anyone here ever rebuilt themselves in their late 30s?

1•buildresiliency•13m ago•0 comments

Mythos for Offensive Security: XBOW's Evaluation

https://xbow.com/blog/mythos-offensive-security-xbow-evaluation
1•ianbutler•13m ago•0 comments

The Picture of Dorian Gray was censored before anyone read it

https://storica.club/blog/dorian-gray-was-censored/
3•verybad•13m ago•0 comments

Evaluation of Various MLX Quantizations

https://github.com/deepsweet/mlx-eval/blob/main/results/README.md
1•d-_-b•14m ago•1 comments

Russia Claims Ukraine Is Using AI Drones That Lock onto Faces and Heat Signature

https://united24media.com/war-in-ukraine/russia-claims-ukraine-is-using-ai-drones-that-lock-onto-...
2•jawiggins•15m ago•1 comments

Goodbye Fragmented Local AI Pipelines. Hello Foundry Local 1.1

https://medium.com/open-ai/goodbye-fragmented-local-ai-pipelines-hello-foundry-local-1-1-9c425b3d...
1•sukhpinder0804•17m ago•0 comments

Who Needs an Architect?

https://yusufaytas.com/who-needs-architect
3•kitecoder•17m ago•1 comments

We let four AIs run radio stations. Here's what happened

https://www.theverge.com/ai-artificial-intelligence/931479/andon-labs-ai-radio-companies
1•1317•18m ago•0 comments

Show HN: We automated Portugal visa appointment checks with computer-use agents

https://www.getfastvisa.com
1•gsunshinel•18m ago•0 comments