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.

AWS is 10x slower than a dedicated server for the same price [video]

https://www.youtube.com/watch?v=Ps3AI1kTIR4
10•wolfgangbabad•7m ago•0 comments

Show HN: I built directory of fashion brands because I didn't know how to dress

https://brandlist.it.com
1•EthanSeo•9m ago•1 comments

First make it fast, then make it smart

https://kix.dev/first-make-it-fast-then-make-it-smart/
1•kixpanganiban•11m ago•0 comments

Branch, Test, Deploy: A Git-Inspired Approach for Data

https://motherduck.com/blog/git-for-data-part-1/
2•articsputnik•12m ago•0 comments

Human_fallback

https://www.nplusonemag.com/issue-44/essays/human_fallback/
1•pizza•13m ago•0 comments

Tor switches to new Counter Galois Onion relay encryption algorithm

https://www.bleepingcomputer.com/news/security/tor-switches-to-new-counter-galois-onion-relay-enc...
1•N19PEDL2•13m ago•0 comments

AI Slop Recipes Are Taking over the Internet – and Thanksgiving Dinner

https://www.bloomberg.com/news/articles/2025-11-25/ai-slop-recipes-are-taking-over-the-internet-a...
1•thm•14m ago•0 comments

What's the point of learning functional programming?

https://blog.daniel-beskin.com/2025-11-13-point-of-learning-fp
1•imjacobclark•18m ago•0 comments

I Cloned a YC startup in 3 hours

https://kanjieight.vercel.app/
1•Mikecraft•20m ago•1 comments

Qiskit – software stack for quantum computing

https://www.ibm.com/quantum/qiskit
1•thinkingemote•20m ago•0 comments

Cloud Neocloud Lambda's vision of the future: 'One GPU, one person'

https://www.fierce-network.com/cloud/neocloud-lambdas-vision-future-one-gpu-one-person
1•oavioklein•26m ago•0 comments

Idempotency Keys

https://www.morling.dev/blog/on-idempotency-keys/
1•ingve•26m ago•0 comments

Why AI Safety Won't Make America Lose the Race with China

https://www.astralcodexten.com/p/why-ai-safety-wont-make-america-lose
2•pseudolus•29m ago•0 comments

Image Diffusion Models Exhibit Emergent Temporal Propagation in Videos

https://arxiv.org/abs/2511.19936
1•50kIters•30m ago•0 comments

The Thanksgiving Special

https://medium.com/luminasticity/the-thanksgiving-special-cc1f9b6fc9d5
1•bryanrasmussen•30m ago•0 comments

How to Run Profitable Pricing Experiments?

https://cleancommit.io/blog/pricing-experiments/
1•mrkaluzny•30m ago•0 comments

FAA probes Amazon after delivery drone snaps internet cable in Texas

https://www.reuters.com/business/retail-consumer/faa-probes-amazon-after-delivery-drone-snaps-int...
2•pseudolus•39m ago•0 comments

Can You Build a Product with Hard Single-Stack Developers?

https://oneknightinproduct.substack.com/p/can-you-really-build-a-product-with
1•imjacobclark•40m ago•0 comments

ML LLVM Project: Compiler Infrastructure for ML-Driven Optimizations

https://github.com/IITH-Compilers/ml-llvm-project
2•ingve•42m ago•0 comments

Lightweight Linux-compatible kernel written in Rust

https://github.com/maestro-os/maestro
1•coolcoder613•43m ago•0 comments

AISDR Human-First Alternative

https://dealmayker.com/alternative/aisdr
1•aleksam•43m ago•0 comments

SoftBank's 40% Slide from Peak Shows Worry over Giant OpenAI Bet

https://www.bloomberg.com/news/articles/2025-11-26/softbank-s-40-slide-from-peak-reflects-jitters...
4•zerosizedweasle•45m ago•0 comments

Plinko PIR Tutorial

https://vitalik.eth.limo/general/2025/11/25/plinko.html
1•sygma•45m ago•0 comments

AutoITE – An Algorithm for Estimating ITE

https://github.com/HotProtato/AutoITE
1•HotProtato•46m ago•1 comments

Show HN: YTShortsDL: A Bulk Downloader Built for Shorts Content Repurposing

https://ytshortsdl.net/
1•Franklinjobs617•48m ago•1 comments

A Tsunami of Cogs

https://betterthanrandom.substack.com/p/a-tsunami-of-cogs
1•gmays•54m ago•0 comments

Linux Kernel Establishes Official AI Coding Guidelines

https://lunduke.substack.com/p/linux-kernel-establishes-official
5•amcclure•55m ago•1 comments

Neural Annealing: Directing Psychedelic Trips Towards Healing

https://psychotechnology.substack.com/p/neural-annealing-directing-psychedelic
1•eatitraw•55m ago•0 comments

Show HN: Constitutional AI Agent OS (governance enforced at kernel level)

https://github.com/kimeisele/steward-protocol
2•harekrishna108•56m ago•1 comments

You're a Bad Parent but You Don't Need to Be

https://nurtureos.ai/
3•hamandcheese•56m ago•1 comments