frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Show HN: Tuber – YouTube client for productive watching

https://tuber.guzus.xyz
1•uncanny_guzus•38s ago•0 comments

Hyperagent, Made by the Team at Airtable

https://www.hyperagent.com/
1•puppion•1m ago•0 comments

Blocking XMPP Spam

https://cweiske.de/tagebuch/xmpp-spam.htm
1•speckx•1m ago•0 comments

Who fixes the zero-days AI finds in abandoned software?

https://martinalderson.com/posts/anthropic-found-500-zero-days/
1•martinald•3m ago•0 comments

Show HN: Rclone UI

https://github.com/rclone-ui/rclone-ui
1•ftchd•4m ago•0 comments

Agent harness for Postgres to ClickHouse migration

https://clickhouse.com/blog/ai-powered-migraiton-from-postgres-to-clickhouse-with-fiveonefour
1•craneca0•6m ago•0 comments

Ex-Google engineer charged for stealing trade secret related to Tensor processor

https://timesofindia.indiatimes.com/technology/tech-news/ex-google-engineer-along-with-her-husban...
3•newusertoday•6m ago•0 comments

Show HN: Personalized Newsletters

https://news.chadnauseam.com/
1•ChadNauseam•6m ago•0 comments

We built an anonymous social network specifically for workplace organizing

https://old.reddit.com/r/labor/comments/1r9kcb1/we_built_an_anonymous_social_network_specifically/
1•robtherobber•7m ago•0 comments

iOS Port of Pocket TTS from Kyutai Labs

https://github.com/UnaMentis/pocket-tts-ios
1•unamentis•7m ago•0 comments

Yamaha DX7 reverse-engineering, part III: Inside the log-sine ROM (2021)

https://www.righto.com/2021/12/yamaha-dx7-reverse-engineering-part-iii.html
2•tosh•8m ago•0 comments

Freeform raises $67M Series B to scale up laser AI manufacturing

https://freeform.co/newsroom/update/delivering-on-the-promise-of-scale
1•antlax•9m ago•0 comments

Jolla Phone update – lights on, technical bits and the schedule

https://forum.sailfishos.org/t/jolla-phone-update-lights-on-technical-bits-and-the-schedule/27821
1•mrbn100ful•10m ago•0 comments

Show HN: Turn Figma Design into Detailed User Story

https://www.figma.com/community/plugin/1596740872864756358/figma-to-story
1•bagusfarisa•12m ago•0 comments

PDF Hell: Why is extracting data still a nightmare?

https://unstract.com/blog/pdf-hell-and-practical-rag-applications/
2•naren87•12m ago•0 comments

One Terabyte of Kilobyte Age, Digging Through the Geocities Torrent

https://blog.geocities.institute/
3•senotrusov•12m ago•1 comments

Ask HN: Who Should I Talk To?

2•3pt14159•13m ago•1 comments

Acpx: A CLI for the Agent Client Protocol (ACP)

https://github.com/janitrai/acpx
1•hosolmaz•14m ago•0 comments

Brat, a parallel TAP testing harness for the POSIX shell

https://codeberg.org/sstephenson/brat
2•todsacerdoti•14m ago•0 comments

romforth: Ultra Portable, Small, Baremetal Forth for Various Processors

https://github.com/romforth/romforth
1•tosh•15m ago•0 comments

SealSkin – Self Hosted VDI and Collaboration

https://sealskin.app/
1•thelamer•15m ago•0 comments

The November 2025 AI Coding Surprise, Model by Model

https://www.goodeyelabs.com/insights/november-2025-ai-coding-surprise
1•rhiever•15m ago•1 comments

LLM Compare – side-by-side benchmark viewer for 250 models (static, no back end)

https://broskees.github.io/llm-compare/
3•broskees•17m ago•3 comments

Step-by-Step Math Problem Solver

https://www.quickmath.com/
1•pablowegw•18m ago•0 comments

Ask HN: Anyone else exhausted by too rapid development of news?

2•notsimonw•18m ago•3 comments

Show HN: Trained an LLM to predict "What will Trump do?"

https://huggingface.co/LightningRodLabs/Trump-Forecaster
6•bturtel•20m ago•0 comments

AI Authorship Scale

https://depue.net/writings/depue-ai-authorship-scale/
2•chadd•20m ago•0 comments

GNU/Linux Open Hardware PowerPC Notebook

https://www.powerpc-notebook.org/2026/01/schematics-reviewed-and-published/
2•ibobev•21m ago•0 comments

Tesla to pay $243M judgement over Autopilot crash

https://electrek.co/2026/02/20/tesla-has-to-pay-historical-243-million-judgement-over-autopilot-c...
4•jeffbee•21m ago•0 comments

Show HN: Apple Notes CLI for Agents

https://github.com/longas/anotes
2•longas•21m ago•0 comments