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.

Mother Earth Mother Board

https://efdn.notion.site/Mother-Earth-Mother-Board-WIRED-a8ff97e460bc4ac1b4a7b87f3503a55c
1•thunderbong•1m ago•0 comments

US recession probabilities implied by the yield curve

https://www.stlouisfed.org/on-the-economy/2023/sep/what-probability-recession-message-yield-spreads
1•latentframe•5m ago•1 comments

Show HN: AnyHabit – A minimalist habit tracker for Raspberry Pi and Docker

https://github.com/Sparths/AnyHabit
1•bebedi•8m ago•0 comments

Highlights from Git 2.54

https://github.blog/open-source/git/highlights-from-git-2-54/
1•tux3•10m ago•0 comments

Enhancing Sporting Organisation Efficiency with Generative AI

https://sinankprn.com/posts/enhancing-sporting-organisation-efficiency-with-generative-ai/
1•sminchev•11m ago•0 comments

Reconstructing a Vue and Three.js app from a single Webpack bundle

1•YufanZhang•11m ago•0 comments

Show HN: Tiltbump – another game in a single HTML file

https://tiagosimoes.github.io/tiltbump/
1•eropatori•13m ago•0 comments

WebP to PNG Converter – Convert WebP to PNG Online Free

https://www.wps.com/tools/webp-to-png/
2•morganglow•19m ago•1 comments

AI agents are a security nightmare. Moving the dev workflow to QEMU

https://hozan23.com/posts/ai-security-nightmare/
1•hozan23•21m ago•0 comments

Kiss Principle

https://en.wikipedia.org/wiki/KISS_principle
1•edu•23m ago•0 comments

Pagetok – an all-in-one AI assistant on your Mac

https://pagetok.web.app/
1•loopstring•25m ago•0 comments

Strongest Phentermine for Women: What to Expect Before You Start

https://finance.yahoo.com/sectors/healthcare/articles/strongest-phentermine-alternatives-weight-l...
1•JoniThani•27m ago•0 comments

Show HN: TapFi – Join any WiFi just by scanning the details with your camera

https://tapfi.uno/
1•jmkni•33m ago•2 comments

Help Me with Multiverse OS

https://chatgpt.com/gg/v/69e7247e7e408196817bdce7534163a8?token=4gu4Y6NwS3xz3phbYpOpeA
1•liljoe•34m ago•0 comments

CC: A P2P Network for Reproducible Autoresearch Code Experiments

https://community.computer/
4•aiw1nt3rs•34m ago•0 comments

All your agents are going async

https://zknill.io/posts/all-your-agents-are-going-async/
2•zknill•35m ago•0 comments

The Anatomy of Tool Calling in LLMs: A Deep Dive

https://martinuke0.github.io/posts/2026-01-07-the-anatomy-of-tool-calling-in-llms-a-deep-dive/
1•tonyl•35m ago•0 comments

Xonsh shell 0.23 REFORGED – not just a release

https://github.com/xonsh/xonsh/releases/tag/0.23.0
1•combisearch•37m ago•1 comments

Show HN: DSS, a new human-readable and plain format for XLS and spreadsheets

https://github.com/Datastripes/DataSheetStandard/
1•vinserello•38m ago•0 comments

Show HN: OpenBridge – turn web chat access into an OpenAI-compatible endpoint

2•linuz•39m ago•0 comments

The Pirate Bay's Oldest Torrent Turned 22

https://torrentfreak.com/the-pirate-bays-oldest-torrent-turned-22/
4•franczesko•45m ago•0 comments

Heatwaves in the Indo-Gangetic Plains: Why Local Conditions Matter

https://www.iitb.ac.in/research-highlight/heatwaves-indo-gangetic-plains-why-local-land-and-atmos...
1•akbarnama•49m ago•0 comments

A Hot-Air Balloon Landed in a California Backyard. The Owner Says It's A '

https://www.wired.com/story/a-brief-interview-with-the-owner-of-the-hot-air-balloon-that-landed-i...
1•joozio•51m ago•0 comments

Less human AI agents, please

https://nial.se/blog/less-human-ai-agents-please/
17•nialse•56m ago•8 comments

Show HN: Alignear – Client communication layer for Linear teams

https://alignear.com/
4•madatbay•57m ago•1 comments

The Fencing Visualization System

https://bsky.app/profile/kcimc.bsky.social/post/3mjxchuwkzs2v
7•mariuz•1h ago•0 comments

Berea college makes tuition free with its endowment

https://www.theatlantic.com/education/archive/2018/10/how-berea-college-makes-tuition-free-with-i...
4•KnuthIsGod•1h ago•0 comments

Iran claims US backdoors knocked out networking equipment

https://www.theregister.com/2026/04/21/iran_claims_us_used_backdoors/
5•defrost•1h ago•1 comments

Writing Node.js Addons with .NET Native AOT

https://devblogs.microsoft.com/dotnet/writing-nodejs-addons-with-dotnet-native-aot/
3•soheilpro•1h ago•0 comments

Using Changesets in a polyglot monorepo

https://luke.hsiao.dev/blog/changesets-polyglot-monorepo/
5•lwhsiao•1h ago•3 comments