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.

Understanding and Coding the Self-Attention Mechanism of LLMs from Scratch

https://sebastianraschka.com/blog/2023/self-attention-from-scratch.html
1•onurkanbkrc•1m ago•0 comments

AI hallucinates. How do you keep it from fucking up automations?

1•Gioppix•6m ago•0 comments

LongCat Video – AI video generator for minutes-long 720p videos

https://longcat-video.org
3•charlie0simmon•10m ago•1 comments

Language

https://en.wikipedia.org/wiki/Language
1•downboots•12m ago•0 comments

Lessons learned from 10 years at Google

https://rahulrav.com/blog/10_years_at_google.html
1•rahulrav•12m ago•0 comments

Show HN: GitHub Action that analyzes CI failures using AI

https://github.com/calebevans/gha-failure-analysis
1•calebevans•15m ago•0 comments

Show HN: Reactive Resume v5 – A free and open-source resume builder

https://rxresu.me
1•AmruthPillai•17m ago•0 comments

What makes a good audio game?

https://old.reddit.com/r/indiegames/comments/1qloyo9/what_makes_a_good_audio_game_by_talon_games_...
1•robin_reala•20m ago•0 comments

Tim Cook Attended Melania's Movie Screening. Apple Customers Want a Boycott

https://wealthofgeeks.com/tim-cook-attended-melanias-movie-screening-apple-customers-are-calling-...
1•antfarm•22m ago•0 comments

Show HN: AI Compass:Daily AI Search Signals and Trends

https://theaidirection.com/
1•funcin•23m ago•0 comments

Scalable Machine Learning and Deep Learning

https://id2223kth.github.io/
1•teleforce•25m ago•0 comments

Writing a C Compiler in Clojure (2024)

https://shagunagrawal.me/writing-a-c-compiler-in-clojure/
1•alhazrod•28m ago•0 comments

Show HN: Resume Optimization Based on JDs

https://www.cvcomp.com
1•Prashasthajain•31m ago•0 comments

Who Uses LLMs?

https://neonvagabond.xyz/articles/who-uses-llms.html
1•shawndumas•33m ago•1 comments

Open-Source AI Audit Readiness Kit for Startups

https://github.com/GK-Edge/AI-Audit
1•Manos_Koulouris•33m ago•1 comments

When the Disclosure Committee Cannot Reconstruct the Record

https://www.aivojournal.org/when-the-disclosure-committee-cannot-reconstruct-the-record/
1•businessmate•35m ago•1 comments

Discover the Most Famous Landmarks

https://famous-landmarks.com/
1•songtianlun1•35m ago•0 comments

Show HN: Bytepiper – turn .txt files into live APIs

https://www.bytepiper.com
1•DhirajKadam27•38m ago•0 comments

The Holy Grail of Linux Binary Compatibility: Musl and Dlopen

https://github.com/quaadgras/graphics.gd/discussions/242
2•Splizard•42m ago•0 comments

Errors as Values in TypeScript

https://errore.org/
2•SouravInsights•43m ago•1 comments

Qualcomm CEO pockets 15% pay rise as profits fall 45%

https://www.theregister.com/2026/01/23/qualcomm_ceo_pay/
3•pjmlp•49m ago•0 comments

Trump Filter Chrome Extension

https://trumpfilter.com/
1•darktoto•50m ago•0 comments

Handling Long Branches

https://maskray.me/blog/2026-01-25-handling-long-branches
2•ingve•52m ago•0 comments

The end of the curl bug-bounty

https://daniel.haxx.se/blog/2026/01/26/the-end-of-the-curl-bug-bounty/
5•mmsc•54m ago•0 comments

Allowlist for .gitignore

https://blog.izissise.net/posts/gitignoreallowlist/
2•ingve•54m ago•0 comments

From zero traffic to launching my own independent news platform

1•ironspidex•55m ago•0 comments

'There's no other poem like it': Why this Robert Burns classic is a masterpiece

https://www.bbc.com/culture/article/20260122-why-this-robert-burns-poem-is-a-masterpiece
3•breve•56m ago•0 comments

Welcome to the "Email Expiration Date" Initiative

https://www.zerocarbon.email/
3•birdculture•56m ago•0 comments

Show HN: Image to 3D AI Generator – Instant 3D Models from Photos

https://heydream.im/image-to-3d/
2•HeyDream_AI•1h ago•0 comments

Ubuntu Pro subscription – should you pay to use Linux?

https://optimizedbyotto.com/post/ubuntu-pro-subscription-pay-for-linux/
3•ottoke•1h ago•3 comments