frontpage.
newsnewestaskshowjobs

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Ask HN: Is there an AI app that generates a feed of personalized cheat sheets

1•ElegantTrader•11s ago•0 comments

Claude Code skill allowing orchestration of edits beat-synced to songs

https://github.com/ZiadAbdelkarim/beat-synced-edit
1•iwasastreamer•1m ago•0 comments

Ask HN: Alternatives to Hotline Telegram CRM?

1•hotline•2m ago•0 comments

Loop Engineering in Claude

https://claude.com/blog/getting-started-with-loops
1•abrbhat•5m ago•0 comments

An explosion of new AI-written genealogy tree tools

https://blog.dnapainter.com/blog/an-explosion-of-new-tree-tools/
1•vintermann•5m ago•0 comments

Build a crappy ring-0 toy antivirus in eBPF with the IMA LSM

https://blog.prizrak.me/post/ima/
1•razighter777•6m ago•0 comments

Kitesurf: Agent-first browser that runs in V8 isolates

https://blog.cloudflare.com/kitesurf/
2•m3h•8m ago•0 comments

Crubit: C++/Rust Bidirectional Interop Tool Rust Workflow

https://crubit.rs/
1•zombot•11m ago•0 comments

Bulldozers Plow Through Big Bend National Park, Sparking Fury in Texas

https://www.newsweek.com/bulldozers-plow-through-big-bend-national-park-sparking-fury-in-texas-12...
1•almog•17m ago•0 comments

US strikes $1.2B deal to pay German firm to halt offshore wind projects

https://www.bbc.com/news/articles/c1e1vg0gjl5o
4•defrost•21m ago•0 comments

Sonic Pi v5 Released

https://www.patreon.com/samaaron/posts/sonic-pi-v5-166001392
1•samaaron•24m ago•1 comments

Kalshi and Polymarket bets on clinical trials criticized as 'ghastly'

https://text.npr.org/nx-s1-5922530
2•1659447091•28m ago•0 comments

HeroBot

https://herobot.co/
1•ingersol•28m ago•0 comments

Notes Compound, Blogs Compile: When I Update a Note vs. Publish a Post

https://www.ssp.sh/brain/sharing-as-second-brain-note-vs-a-blog-post/
2•zazuke•29m ago•0 comments

Honey, I Shrunk the Agency (Our journey building a software factory)

https://vibegui.com/en/article/honey-i-shrunk-the-agency/
1•gadr90•31m ago•1 comments

AI danger explained for people who don't understand full sentences

https://www.reddit.com/r/OpenAI/s/ASRcUl35lX
2•nemo_captain•31m ago•1 comments

Coldcard Postmortem from MicroPython's Perspective

https://github.com/orgs/micropython/discussions/19588
1•mech4bg•35m ago•0 comments

Tears in Rain Monologue

https://en.wikipedia.org/wiki/Tears_in_rain_monologue
1•tosh•36m ago•0 comments

Ask HN: What observability stack are you using for AI agents in production?

2•varda_62892•37m ago•0 comments

Asking "Was EvoMap's Evolver a reference" for Hermes gets deleted and blocked

https://github.com/NousResearch/hermes-agent/issues/10232
2•sundarurfriend•38m ago•0 comments

DeepMind Says Its AI Can Predict Hurricanes Earlier Than Everyone Else

https://www.wired.com/story/deepmind-ai-model-can-predict-hurricanes-earlier/
2•WithinReason•42m ago•0 comments

The Illusion of Visual Tool-Use: A Causal Audit of Thinking with Images

https://arxiv.org/abs/2608.06270
1•sbulaev•43m ago•0 comments

A Rare 4.5-Hour Interview with Huawei's Chief Scientist for Semiconductors

https://chinatechbite.substack.com/p/a-rare-45-hour-interview-with-huaweis
2•hunglee2•44m ago•0 comments

Jujutsu 0.44.0

https://github.com/jj-vcs/jj/releases/tag/v0.44.0
1•birdculture•45m ago•0 comments

A Mirror of Trumps Posts

https://fakenews.social/
1•calypso•47m ago•0 comments

How to Keep Thinking

https://www.seangoedecke.com/how-to-keep-thinking/
2•vcfgdev•47m ago•0 comments

Building an LLM Agent to Play Dwarf Fortress

https://blog.trine.dev/posts/2026-02-28-df-ai-exp/
1•totetsu•49m ago•0 comments

Regex is (almost) all you need (2025)

https://lookingatcomputer.substack.com/p/regex-is-almost-all-you-need
1•aleda145•50m ago•0 comments

Blaise v0.14.0 is here: macOS, BlaiseGuard, math and more

https://github.com/graemeg/blaise/discussions/211
1•mariuz•50m ago•0 comments

Anthropic AI Mandatory Employee Stock Trading Plans for IPO – This Will Not Go W [video]

https://www.youtube.com/watch?v=zi9LsoKISEc
1•xbmcuser•51m ago•0 comments