frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

A $5k Lean Startup Experiment Became Michigan's Innovation Hub

https://www.jointhequarter.com/blog/bamboo
1•rmason•3m ago•0 comments

Jqp: TUI Playground to Experiment with Jq

https://github.com/noahgorstein/jqp
1•ingve•7m ago•0 comments

Show HN: SmoothTalk – Practice Cold Approaching and Flirting with Voice AI

https://www.smoothtalk.app/download
1•shehbaj•8m ago•0 comments

Milwaukee M18 tool battery communication protocol

https://github.com/mnh-jansson/m18-protocol
2•jakogut•10m ago•1 comments

Tesla Is Redesigning Door Handles That Drew Safety Scrutiny

https://www.bloomberg.com/news/articles/2025-09-17/tesla-is-redesigning-door-handles-that-drew-sc...
2•JumpCrisscross•10m ago•0 comments

Upgrading by Downgrading

https://grizzlygazette.bearblog.dev/upgrading-by-downgrading/
1•fatfox•11m ago•0 comments

Show HN: Veors – A platform showcasing AI videos created by people

https://veors.com
1•Oxidome•12m ago•0 comments

Tell HN: EasyList break more than just YouTube

1•LiamPowell•12m ago•0 comments

I'd Like to Believe (1999)

https://condenaststore.com/featured/id-like-to-believe-were-redefining-the-way-bruce-eric-kaplan....
1•mitchbob•13m ago•0 comments

Praxos: A personal AI companion. Works with WhatsApp and Telegram text and voice

https://www.mypraxos.com/
4•soheils9•19m ago•0 comments

Show HN: Keplar – Voice AI for qualitative research at quantitative scale

https://www.keplar.io/
1•dgul994•26m ago•0 comments

Ask HN: What is the most useful AI tool you use outside of Cursor/ChatGPT?

4•break_the_bank•28m ago•2 comments

Show HN: A new platform similar to codepen.io

https://crazycontext.com/
1•liszper•29m ago•0 comments

Firefox 143 Released

https://www.firefox.com/en-US/firefox/143.0/releasenotes/
2•akyuu•30m ago•1 comments

Attack on SonicWall's cloud portal exposes customers' firewall configurations

https://cyberscoop.com/sonicwall-cyberattack-customer-firewall-configurations/
1•speckx•30m ago•0 comments

Tutorial: From drone video to photo-realistic 3D scene:Blender Conference 2025

https://www.youtube.com/watch?v=Q5FISs0gkiE
1•shankysingh•31m ago•0 comments

Cosmonauts Algorithm: Soviet Scientists Invented Predictive Health Monitoring

https://valeman.medium.com/the-cosmonauts-algorithm-how-soviet-scientists-invented-predictive-hea...
2•dvfjsdhgfv•34m ago•0 comments

Image-GS: Content-Adaptive Image Representation via 2D Gaussians

https://github.com/NYU-ICL/image-gs
1•jasonjmcghee•34m ago•0 comments

Wild Performance Tricks in Rust

https://davidlattimore.github.io/posts/2025/09/02/rustforge-wild-performance-tricks.html
2•fanf2•35m ago•0 comments

Claude Code Degradation: A postmortem of three recent issues

https://www.anthropic.com/engineering/a-postmortem-of-three-recent-issues
8•moatmoat•36m ago•1 comments

Integrating ClickHouse into your React app

https://clickhouse.com/blog/clickhouse-powered-apis-in-react-app-moosestack
2•craneca0•38m ago•0 comments

Build AI Voice Mobile Apps

https://www.bravostudio.app/ai-voice
1•petercod•39m ago•0 comments

Government gestures leave roots of Indonesia protests intact

https://www.japantimes.co.jp/news/2025/09/02/asia-pacific/politics/indonesia-protests-root-proble...
1•PaulHoule•41m ago•0 comments

A better future for JavaScript that won't happen

https://drewdevault.com/2025/09/17/2025-09-17-An-impossible-future-for-JS.html
3•er0k•41m ago•0 comments

Is AI a Bubble? [Exponential View]

https://www.exponentialview.co/p/is-ai-a-bubble
3•witch-king•42m ago•0 comments

Building a "Yogurt Phone" with eBPF and netkit: a surprisingly deep rabbit hole

https://blog.yadutaf.fr/2025/09/16/creating-a-yogurt-phone-with-netkit-ebpf/
2•jtlebigot•42m ago•0 comments

Rethink Robotics shuts down again

https://www.therobotreport.com/rethink-robotics-shuts-down-again/
2•ilamont•43m ago•0 comments

Uber Kraken is back? New releases

https://github.com/uber/kraken/releases
1•vkoshkarov•43m ago•0 comments

They tested video games in the nineties

https://spillhistorie.no/2025/09/17/how-they-tested-video-games-in-the-nineties/
1•huftis•44m ago•0 comments

Trade Joe's Does Not Have Surveillance Cameras

https://dan.bulwinkle.net/blog/trader-joes-does-not-have-surveillance-cameras/
3•pilingual•50m ago•0 comments