frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

MCP for a Coffee Machine... Worked!

https://archestra.ai/blog/brew-by-ai
1•motakuk•49s ago•0 comments

TS/JS Type-safe thread pool multithreading using Web Workers

https://github.com/imramkrishna/thready-js
1•handfuloflight•2m ago•0 comments

Show HN: A minimal demo showing coherence collapses when state is transported

https://github.com/jspchp63/rcircuit-phase-engine
1•jspchp63•3m ago•0 comments

I asked an AI to create "Unhackable Code". It gave me this. I'm scared

1•VOIDMAKINA•4m ago•1 comments

Show HN: Certgrep – The Certificate Transparency Search Engine

https://certgrep.sh/
2•juxhindb•5m ago•0 comments

AMD Stole Intel's Future: The Untold x86-64 Story [video]

https://www.youtube.com/watch?v=WyX8TO3awfw
1•tosh•6m ago•0 comments

Manifesto claiming responsibility for terror attack on energy infrastructure [D]

https://www.berliner-zeitung.de/news/stromterror-in-berlin-das-bekennerschreiben-im-wortlaut-li.1...
1•FrojoS•6m ago•0 comments

Signal Contingency Plan (DeltaChat)

https://signal-contingency-plan.info/
1•xeonmc•6m ago•1 comments

DeGoogled Phones Reviews

https://tuta.com/blog/degoogled-phones
1•nreece•7m ago•0 comments

Directional Searching for Light Dark Matter with Quantum Sensors [pdf]

https://journals.aps.org/prl/abstract/10.1103/cwx5-2n1y
1•thunderbong•9m ago•0 comments

I Built the Redis GUI Client as an Alternative to Redis Insight – Redimo

https://www.redimo.dev
1•shh6155•15m ago•0 comments

Watching myself say things I would never say. Deepfake menace we must confront

https://www.theguardian.com/commentisfree/2026/jan/05/deepfakes-youtube-menace-yanis-varoufakis
1•defrost•15m ago•1 comments

ECB’s digital euro project faces decisive vote in 2026

https://www.ft.com/content/99a7b61b-c5df-4794-8c5f-2e3acd291333
1•nutjob2•21m ago•0 comments

Decorative Cryptography

https://www.dlp.rip/decorative-cryptography
16•todsacerdoti•26m ago•0 comments

Unifying QFT and GR Through Topologically Protected Gauge Theory

https://www.academia.edu/145737035/The_Renormalization_of_Gravity_Resolving_the_Goroff_Sagnotti_D...
1•MohskiBroskiAI•26m ago•0 comments

FL Studio Linux Setup Guide

https://github.com/Torbuntu/fl-studio-linux-setup
1•luckys•28m ago•0 comments

The Death and Rebirth of Programming – The Phoenix Architecture

https://aicoding.leaflet.pub/3malrv6poy22a
1•kiyanwang•31m ago•0 comments

The Naibbe cipher: encrypts Latin and Italian as Voynich-like ciphertext

https://www.youtube.com/watch?v=ByARtG-GUPo
1•omega3•31m ago•1 comments

Microsoft Office renamed to "Microsoft 365 Copilot app"

https://www.office.com
5•LeoPanthera•31m ago•2 comments

League of Legends Client rendered unusable after 10 year certificate expired

https://gamefragger.com/pc/moba/league-of-legends/league-of-legends-global-login-outage-expired-c...
1•dracotomes•33m ago•0 comments

Show HN: Spectra – Visuals should be infrastructure, not manual work

2•JannikJung•37m ago•0 comments

FreeBSD Home NAS, part 5: ZFS pools, datasets, snapshots, and monitoring ZFS he

https://rtfm.co.ua/en/freebsd-home-nas-part-5-zfs-pool-datasets-snapshots-and-zfs-monitoring/
2•todsacerdoti•38m ago•0 comments

Show HN: Kandinsky AI Video Generator

https://www.kandinskyai.com
1•Zach_HE•38m ago•0 comments

Swan Upping

https://en.wikipedia.org/wiki/Swan_upping
1•cyode•39m ago•0 comments

Voice Coding on iPad with Claude

https://www.youtube.com/watch?v=Rptvh534lI4
2•siwakotisaurav•40m ago•1 comments

What are some good designer agents available now?

1•nmr521521•41m ago•0 comments

Blade Runner: Braun HL 1 Personal Fan, En español (2012)

http://camara-de-maravillas.blogspot.com/2012/11/el-objeto-de-la-semana-ventilador-de.html
1•exvi•44m ago•0 comments

Ilya Sutskever

https://en.wikipedia.org/wiki/Ilya_Sutskever
3•doener•45m ago•0 comments

Secure, lint, and harden GitHub Actions workflows

https://github.com/reugn/github-ci
1•jrzx•49m ago•0 comments

Show HN: Transform messy server logs into clean, readable format

https://prettylog.net
1•bardonadam•50m ago•0 comments