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.

Inline RBS Comments for a Rubygem Codebase

https://yob.id.au/2025/12/30/inline-rbs-comments.html
1•jhealy•53s ago•0 comments

Velox is a port of Tauri to Swift

https://github.com/velox-apps/velox
2•todsacerdoti•1m ago•0 comments

Lottery Wins: Separating Myths from Evidence

https://www.medscape.com/viewarticle/lottery-wins-separating-myths-evidence-2025a10010a2
1•wjb3•2m ago•0 comments

Home health aides, another government vector for fraud

https://twitter.com/katiemiller/status/2005251273277177925
1•reop2whiskey•2m ago•0 comments

My 20-Year-Old Program

https://seminumerical.io/post/my-20-year-old-program/
1•nice_byte•6m ago•0 comments

Language Gives Authority to Weak Ideas

https://hichvg.substack.com/p/elegance-is-bullshit
1•chvgchvg•6m ago•0 comments

Making end-to-end encrypted AI chat feel like logging in

https://confer.to/blog/2025/12/passkey-encryption/
3•arkadiyt•12m ago•0 comments

PhDs Can't Find Work as Boston's Biotech Engine Sputters

https://www.wsj.com/tech/biotech/ph-d-s-cant-find-work-as-bostons-biotech-engine-sputters-729f0036
2•JumpCrisscross•12m ago•0 comments

Benchmarking Self-Hosted S3-Compatible Storage

https://www.repoflow.io/blog/benchmarking-self-hosted-s3-compatible-storage-a-practical-performan...
1•wener•13m ago•0 comments

Show HN: ARES Dashboard – Open-Source AI Red-Teaming and Governance Platform

https://github.com/Arnoldlarry15/ARES-Dashboard
1•LA_Builds•17m ago•0 comments

Zuck buys Chinese AI company Manus that claims it deals in actions, not words

https://www.theregister.com/2025/12/30/meta_acquires_manus/
1•rguiscard•23m ago•1 comments

Engineering compact Goldenberry to promote its potential as a global crop

https://nph.onlinelibrary.wiley.com/doi/10.1002/ppp3.70140
1•PaulHoule•23m ago•0 comments

Merriam-Webster: LLM [video]

https://www.youtube.com/watch?v=RvQnl3O0D3c
1•cdrnsf•25m ago•0 comments

Show HN: BSH – A Git-aware, IntelliSense-style predictive Zsh history daemon

https://github.com/joshikarthikey/bsh
1•karthikeyjoshi•26m ago•0 comments

How We Use the Word Cyber

https://www.bbc.com/news/magazine-35765276
1•andsoitis•30m ago•0 comments

Convoy Steamroller

https://tedium.co/2025/12/25/cb-radio-cw-mccall-mannheim-steamroller-christmas/
1•chatmasta•30m ago•0 comments

Show HN: OMyTree – Turning AI chats into durable process assets

https://www.omytree.com
1•isbeingto•30m ago•0 comments

TLHO – A domain-agnostic, fail-closed verification substrate

1•tlho•31m ago•0 comments

&udm=14: the disenshittification Konami code (remove AI from Google results)

https://udm14.com/
3•chatmasta•32m ago•1 comments

Data Model Dependency Is a Trap

https://medium.com/@HobokenDays/the-fate-of-shared-data-model-cf8a3dc88ac9
1•HideInNews•36m ago•0 comments

Ship comms: how do they work?–lightning talk by Matt Kulukundis [video]

https://www.youtube.com/watch?v=RFvnXCHS57M
3•leoc•42m ago•0 comments

Show HN: Beauty Arena – An Elo-based experiment to measure aesthetics

https://beautyarena.vercel.app
1•railing1024•42m ago•0 comments

"For You" Feed for Science

https://www.researchhub.com/
1•Tardigrade10•43m ago•0 comments

Outside, Dungeon, Town: Integrating the Three Places in Videogames (2024)

https://keithburgun.net/outside-dungeon-town-integrating-the-three-places-in-videogames/
4•vector_spaces•44m ago•0 comments

39C3 – Breaking architecture barriers: Running x86 games and apps on ARM

https://www.youtube.com/watch?v=3yDXyW1WERg
1•doener•45m ago•0 comments

Stranger Things Creator: Turn Off TV's Garbage Settings

https://www.instagram.com/reel/DRiaP2zEow4/
1•next_xibalba•46m ago•1 comments

Why A.I. Didn't Transform Our Lives in 2025

https://www.newyorker.com/culture/2025-in-review/why-ai-didnt-transform-our-lives-in-2025
1•petethomas•47m ago•0 comments

Show HN: McNeal – Encrypted messaging over acoustic channels

https://github.com/AntonioLambertTech/McnealV2
1•netIsNewWrldOrd•48m ago•0 comments

Show HN: SAFi, a Governance Engine for LLMs

https://safi.selfalignmentframework.com
1•jnamaya•50m ago•0 comments

'Capital of capital': how Abu Dhabi rose as a sovereign wealth powe

https://www.ft.com/content/711e670c-4832-49cb-8b10-5018103ec785
1•petethomas•54m ago•0 comments