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.

Ask HN: Are you using an agent orchestrator to write code?

1•gusmally•41s ago•0 comments

Gemini achieving "incredible numbers" (84.6%) on ARC-AGI-2 (Chollet)

https://twitter.com/fchollet/status/2021983310541729894
1•marojejian•54s ago•1 comments

Origami-inspired ring lets users 'feel' virtual worlds

https://techxplore.com/news/2026-01-origami-users-virtual-worlds.html
1•PaulHoule•2m ago•0 comments

Show HN: OptaReach – multi-platform lead generation and outreach

https://optareach.com/
1•betty_garet•2m ago•0 comments

Authenticated Workflows: A Systems Approach to Deterministic Agentic Controls

https://arxiv.org/abs/2602.10465
1•mrajagopalan•3m ago•1 comments

A party balloon shut down El Paso International Airport; estimated cost –$573k

https://log.jasongodfrey.info/questions/The-Most-Expensive-Party-Balloon-in-History
2•heifer•4m ago•0 comments

Codex 0.100.0

https://github.com/openai/codex/releases/tag/rust-v0.100.0
1•tosh•4m ago•0 comments

Show HN: Decision Guardian – Enforce ADRs on PRs

2•iamalizaidi•5m ago•2 comments

How to Have a Bad Career [video]

https://www.youtube.com/watch?v=Rn1w4MRHIhc
1•rombr•5m ago•0 comments

HDRify: True HDR image viewer, and tool set in pure JavaScript

https://hdrify.benhouston3d.com/?image=%2Fexamples%2Fmoonless_golf_1k.hdr
1•bhouston•6m ago•0 comments

Show HN: Mega LLMs – Universal AI chat client for any OpenAI-compatible API

https://sh0sh0.netlify.app/mega_llm
1•p32929•6m ago•0 comments

Trump administration set to revoke basis of US climate regulation

https://www.reuters.com/legal/litigation/trump-administration-set-revoke-basis-us-climate-regulat...
4•Bondi_Blue•6m ago•0 comments

2026 will not be the 'Year of Linux on the Desktop' – and I'm glad

https://kevinboone.me/not_desktop.html
1•speckx•7m ago•0 comments

Show HN: Spelling Riddle – Think Spellbee with crossword clue and visual hint

https://ravel.live/
1•nohat•7m ago•0 comments

Anthropic raises $30B in Series G funding at $380B post-money valuation

https://www.anthropic.com/news/anthropic-raises-30-billion-series-g-funding-380-billion-post-mone...
4•ryanhn•8m ago•2 comments

Moxie Marlinspike – Signal vs. Telegram, Private AI, Encryption – Sabrina Halper [video]

https://www.youtube.com/watch?v=cPRi7mAGp7I
1•DavideNL•9m ago•0 comments

Starlink speeds past terrestrial networks – and regulators

https://www.theregister.com/2026/02/12/starlink_challenges/
3•mooreds•9m ago•0 comments

Show HN: AccessLint – iOS accessibility static analysis (25 WCAG rules)

https://accesslint.app
1•synctek•11m ago•0 comments

Social media age verification is full of risks and unclear rewards

https://www.salon.com/2026/02/11/social-media-age-verification-is-full-of-risks-and-unclear-rewards/
3•mooreds•11m ago•0 comments

EPA reverses climate change finding, stripping own ability to regulate emissions

https://www.nbcnews.com/science/climate-change/epa-reverses-endangerment-climate-change-finding-r...
4•ceejayoz•12m ago•0 comments

In defense of not reading the code

https://www.benshoemaker.us/writing/in-defense-of-not-reading-the-code/
2•straydusk•13m ago•1 comments

The startup for startups, nobody remembers the almost

https://efferent.app
2•cxmjm•14m ago•1 comments

Noether's Theorem

https://grokipedia.com/page/Noether%27s_theorem
2•bilsbie•14m ago•2 comments

Apple security-focused developer event

https://twitter.com/radian/status/2014381898760651046
1•de_aztec•16m ago•0 comments

DanceJump for YouTube – Rhythm Dance Game – v0.4.1 Released

https://chromewebstore.google.com/detail/dancejump-for-youtube-rhy/hhdeflibphdghcpblkekakmbennfcaci
1•maaydin•16m ago•1 comments

Alabama offers three tricks to fix poor urban schools

https://www.economist.com/united-states/2026/02/12/alabama-offers-three-tricks-to-fix-poor-urban-...
1•alephnerd•17m ago•0 comments

Dyad 2.0: What Agentic AI Means for the Future of Computer Languages

https://juliahub.com/blog/announcing-dyad-2.0
2•ChrisRackauckas•17m ago•0 comments

Postgres Indexes, Partitioning and LWLock:LockManager Scalability

https://ardentperf.com/2024/03/03/postgres-indexes-partitioning-and-lwlocklockmanager-scalability/
1•abelanger•17m ago•0 comments

The Spy Who Found T. Rex

https://nautil.us/the-spy-who-found-t-rex-1267359/
1•speckx•21m ago•0 comments

Epstein Relationship Network

https://epstein-file-explorer.com/network
2•doener•21m ago•0 comments