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.

No one has defined the "AI Strategist" role – so I wrote an OSS book to do it

https://github.com/Leading-AI-IO/the-ai-strategist
1•leading-AI•1m ago•0 comments

Aqua: A CLI message tool for AI agents

https://github.com/quailyquaily/aqua
1•lyricat•4m ago•0 comments

Cheers to the NetJets captain that made the call

https://twitter.com/BOCOjetSales/status/2025724280923910425
1•hnburnsy•7m ago•0 comments

2008 "Peak Oil" = 2026 "Peak Memory"

https://twitter.com/benkizemchuk/status/2025570398495015100
1•walterbell•8m ago•0 comments

Your Thirst Is Mine, My Water Is Yours

https://www.stopcar.ing/your-thirst-is-mine-my-water-is-yours/
2•cwal37•10m ago•0 comments

Jujutsu Version Control Explained – Video by Brian Will

https://www.youtube.com/watch?v=mM4nrhDenC8
2•Lanedo•13m ago•0 comments

Tesla Begins Production of Cybercab

https://www.barrons.com/articles/tesla-stock-price-cybercab-9e8a08ed
2•lateforwork•15m ago•1 comments

The lack of reliable financial infrastructure

https://www.tumblr.com/archive-is/809233317252710400/on-growth-limits-finances-and-why-osinters-f...
1•cnst•16m ago•0 comments

$30B for laptops yielded a generation less cognitively capable than parents

https://www.yahoo.com/news/articles/u-spent-30-billion-ditch-110200869.html
10•walterbell•17m ago•1 comments

Giving feedback is about to get much harder. Get better at it

https://thebestworstcase.substack.com/p/giving-feedback-is-about-to-get-harder
1•shmublu•22m ago•0 comments

HN Algolia API is currently stalled

https://hn.algolia.com/api/v1/search_by_date?tags=story
1•cylo•25m ago•1 comments

A Primer on Terrorism, Insurgency and Protest

https://acoup.blog/2026/02/13/collections-against-the-state-a-primer-on-terrorism-insurgency-and-...
1•colinprince•25m ago•0 comments

Show HN: SEOBot – open-source AI SEO tool (keyword research, content gen, audit)

https://github.com/George3307/seobot
1•starwildcard•29m ago•0 comments

Show HN: Debugger MCP for VS Code

https://marketplace.visualstudio.com/items?itemName=Speakeasy.debugger-mcp
1•indybonez•30m ago•0 comments

Show HN: Magnetic Tower of Hanoi – 8 disks = 2026 moves (the year)

https://george3307.github.io/magnetic-hanoi/
1•starwildcard•30m ago•0 comments

Container GUI – Native macOS GUI for Apple's Container CLI

https://github.com/FeSens/container-gui
2•fesens•31m ago•1 comments

JIT: A header-only, cross-platform JIT compiler library in C

https://github.com/abdimoallim/jit
3•linkdd•33m ago•0 comments

AI portrait generator for MSX: Bringing neural networks to 8-bit computers

https://github.com/astrowar/msx_gan
1•astrowar•35m ago•1 comments

SQLite Is the Evolutionary Perfection of the KeePass KDBX Format

https://mketab.org/blog/sqlite_kdbx/
2•wps•36m ago•0 comments

Playing CSS-defined animations with JavaScript

https://benhatsor.medium.com/99573ef4738b
1•barhatsor•39m ago•0 comments

The Single File Agent Manifesto – RappterHub

https://kody-w.github.io/rappterhub/single-file-agents.html
1•bothangles•40m ago•0 comments

Database Federation: Decentralized and ACL-Compliant Hive Databases

https://www.uber.com/blog/database-federation/
2•umairnadeem123•40m ago•0 comments

Carl Sagan 1996: Democracy Ends with a Charlatan [video]

https://www.youtube.com/shorts/X8qR4aVaI4g
1•keernan•42m ago•1 comments

Authorization before execution: deciding if an AI should be allowed to act

https://github.com/celestinestudiosllc/ai-action-authorization-test-harness
1•celestinestudio•47m ago•1 comments

The surprising power of daily rituals

https://www.bbc.com/future/article/20210914-how-rituals-help-us-to-deal-with-uncertainty-and-stress
10•andsoitis•48m ago•5 comments

Jordi Bonada (MTG): 20 years of singing voice synthesis at the MTG [video]

https://www.youtube.com/watch?v=t2ZcSluABKY&list=PL0EPT_FWD3Ml_U_WvQZ3uHb76ACJA6mRH&index=16
2•q7m•50m ago•0 comments

Bonobos Grin During Sex

https://nautil.us/grinning-during-sex-isnt-contagious-but-it-does-require-tempo-1269012/
2•marojejian•51m ago•1 comments

Star Chamber

https://en.wikipedia.org/wiki/Star_Chamber
2•barrister•53m ago•0 comments

Classic Prank Knife Design

https://imgur.com/a/cW0u7Rz
1•anonym29•55m ago•0 comments

Kangaroo Court

https://www.law.cornell.edu/wex/kangaroo_court
1•barrister•58m ago•0 comments