frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Stream music, movies, books, and live TV from torrents and IPTV

https://bittorrented.com
1•cranberryturkey•45s ago•0 comments

Trump's Embrace of Weaker Dollar Fuels Bets on New Downtrend

https://www.bloomberg.com/news/articles/2026-01-28/trump-s-embrace-of-weaker-dollar-seen-as-start...
1•petethomas•5m ago•0 comments

Ask HN: Doctors Using AI Workflow Automation – What's the Real Impact?

1•spenceXu•6m ago•0 comments

Ask HN: If Everyone Can "Build" a SaaS, What Becomes Valuable?

1•spenceXu•11m ago•0 comments

Amazon One palm authentication services ending June 3, 2026

https://amazonone.aws.com/help
1•stepquiet•13m ago•1 comments

Calculation of the Digits of π by the Spigot Algorithm of Rabinowitz and Wagon

https://www.cut-the-knot.org/Curriculum/Algorithms/SpigotForPi.shtml
1•measurablefunc•14m ago•0 comments

Sherlock – See what's being sent to LLM APIs in real-time

https://github.com/jmuncor/sherlock
1•jmuncor•16m ago•0 comments

The ICE the Dildo Distribution Delegation

https://www.closertotheedge.net/p/the-dildo-distribution-delegation
2•_wire_•16m ago•2 comments

Ask HN: Vibe Researching" with AI – Anyone Using It for Real?

1•spenceXu•18m ago•0 comments

ArticleCast – Turn Articles and PDFs into AI Podcasts

https://www.articlecast.ai
1•imomoh•19m ago•0 comments

Programming Sucks (2014)

https://www.stilldrinking.org/programming-sucks
1•fragmede•20m ago•1 comments

Moondala – Social commerce where users earn from referrals, not ads

https://moondala.one/r/2S7B6R
1•HamoodBahzar•26m ago•1 comments

Moltbot Security Guide: Protect Your VPS from Hackers and Vulnerabilities

https://veerhost.com/moltbot-security-guide/
3•aymanaljunaid•28m ago•0 comments

HetGPU: The pursuit of making binary compatibility towards GPUs

https://arxiv.org/abs/2506.15993
1•adgnaf•32m ago•1 comments

Ask HN: Do you verify AI-generated content before publishing?

1•kumathpratik•35m ago•0 comments

Teleology

https://en.wikipedia.org/wiki/Teleology
2•downboots•36m ago•1 comments

Decoded: GNU Coreutils (2019)

https://www.maizure.org/projects/decoded-gnu-coreutils/
3•Ariarule•39m ago•0 comments

The novelist who took on the Italian mafia – and lived to tell the tale

https://www.thetimes.com/culture/books/article/sicilian-man-leonardo-sciascia-rise-mafia-struggle...
3•Thevet•40m ago•0 comments

AI Coloring Pages Generator

https://aicoloringpage.org/
3•Evan233•44m ago•0 comments

I built a Cursor for writers" on a $7 budget using Groq and Llama 3

2•minotauris•44m ago•2 comments

AI discovers 12 of 12 OpenSSL zero-days (while curl cancelled its bug bounty)

https://www.lesswrong.com/posts/7aJwgbMEiKq5egQbd/ai-found-12-of-12-openssl-zero-days-while-curl-...
2•excited-dev-11•45m ago•0 comments

Use FinTinAI to Master Your Financial Future

https://fintinai.com
1•Xmire•45m ago•1 comments

I built hidden emotion detection application

https://subtlesense.lovable.app
3•naiyya_thapa•46m ago•0 comments

A "Pure Go" Linux Environment, Ported by Claude

https://www.jtolio.com/2026/01/tinyemu-go/
1•varunramesh•46m ago•0 comments

Claude Code and Executive Function

https://til.andrew-quinn.me/posts/claude-code-and-executive-function/
1•hiAndrewQuinn•46m ago•0 comments

The Rise, Fall, and Rebirth of Clawdbot in 72 Hours

https://www.everydev.ai/p/the-rise-fall-and-rebirth-of-clawdbot-in-72-hours
4•devhouse•47m ago•0 comments

How the new era of CEO supervillains are trapped in their own ideology

https://statmodeling.stat.columbia.edu/2026/01/27/facebook-and-the-inherent-incoherence-of-the-id...
2•mandevil•51m ago•0 comments

What happened to the technical drawings of old and obsolete vehicles, machines?

https://old.reddit.com/r/AskEngineers/comments/1af8muv/what_happened_to_the_blueprintstechnical_d...
2•num42•53m ago•1 comments

Show HN: A browser-only image processing pipeline inspired by macOS Automator

https://picturekit.app/en
1•margox•54m ago•0 comments

Boston – MIT grad, Polaroid engineer's basement band side hustle

https://www.youtube.com/watch?v=OYyaKZ2aiiU
1•burnt-resistor•57m ago•0 comments