frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

I like it, but I hate it more (AI)

https://douxx.blog/i-like-it-but-i-hate-it-even-more
1•dblog•25s ago•0 comments

QEMUtiny - QEMU escape vulnerability

https://github.com/v12-security/pocs/tree/main/qemu
1•Tiberium•1m ago•0 comments

The Underground Market That Unlocks Stolen iPhones

https://www.infoblox.com/blog/threat-intelligence/lookalike-domains-expose-the-iphone-theft-economy/
1•speckx•2m ago•0 comments

Centralized Dead-Letter Queue for Easy Troubleshooting of Distributed Systems

https://carlosblanco.github.io/architecture/backend/2026/05/15/event-driven-architecture-troubles...
1•carlosomar2•3m ago•0 comments

Dear bartending, I still love you

https://spicymelonblog.com/dear-bartending-i-still-love-you/
1•aralsamuel•3m ago•0 comments

Typograms: A portable ASCII diagram format that renders to SVG

https://google.github.io/typograms/
2•matijash•6m ago•0 comments

Tech Layoff Wave Has Hit 100k Jobs This Year

https://www.statista.com/chart/36198/tech-and-startup-employees-laid-off-worldwide/
2•speckx•6m ago•0 comments

Earth is flying through ancient supernova dust

https://www.sciencedaily.com/releases/2026/05/260513221751.htm
1•flockyflock•7m ago•0 comments

Django LiveView vs. Phoenix LiveView: a real benchmark

https://en.andros.dev/blog/80134668/django-liveview-vs-phoenix-liveview-a-real-benchmark/
1•andros•8m ago•0 comments

GitLab is betting a 19th-century economic theory will shape its AI era

https://thenewstack.io/gitlab-ai-agents-jevons-paradox/
1•Brajeshwar•9m ago•0 comments

Show HN: CtxVault – receipts for AI context, not another memory store

https://ctxvault.github.io/ctxvault/
1•LuxBennu•9m ago•0 comments

Show HN: Cchost – Run multiple isolated Claude Code accounts on one machine

https://github.com/allenhack638/cchost
1•allenbenny038•9m ago•0 comments

TypeScript Refactoring Interview Questions

https://reactdevelopment.substack.com/p/typescript-refactoring-interview
1•javatuts•12m ago•0 comments

Mental bugs due to lack of imagination

https://nahurst.substack.com/p/mental-bugs-due-to-lack-of-imagination
2•nathanh•17m ago•0 comments

Show HN: Formal Verification with Lean

https://www.daniellowengrub.com/blog/2026/04/30/lean
1•lowdanie•22m ago•0 comments

Digital Twin – An AI Clone of Yourself (Claude and ElevenLabs and Cloudflare)

https://aimirrortwin.com
1•sumhead•22m ago•1 comments

Zig vs. Rust in 2026

https://zackoverflow.dev/writing/zig-vs-rust-in-2026/
3•ibobev•24m ago•0 comments

Microsoft and Apple bets on new mascots in bid to seem more cuddly

https://www.bbc.com/news/articles/c99l1zzp8xzo
1•reconnecting•24m ago•0 comments

Kicking the Tyres on Harbor for Agent Evals

https://rmoff.net/2026/04/09/kicking-the-tyres-on-harbor-for-agent-evals/
1•eigenBasis•26m ago•0 comments

There's a $50B company hiding inside Salesforce

1•emmanol•26m ago•0 comments

Recursant, the open source AI control plane, now supports OpenClaw

https://clawhub.ai/plugins/openclaw-recursant
1•hestefisk•26m ago•0 comments

From latency to instant: Modernizing GitHub Issues navigation performance

https://github.blog/engineering/architecture-optimization/from-latency-to-instant-modernizing-git...
1•Brajeshwar•27m ago•0 comments

Ask HN: What AI tools are you using every day?

1•tomchui157•28m ago•3 comments

Introducing Spend Caps (Google Cloud)

https://cloud.google.com/blog/topics/cost-management/introducing-spend-caps-ai-cost-visibility-ne...
1•markerbrod•31m ago•0 comments

Check Your Fucking Sources, People

https://brodzinski.com/2026/05/check-fcking-sources.html
7•flail•31m ago•0 comments

Our response to the TanStack NPM supply chain attack

https://openai.com/index/our-response-to-the-tanstack-npm-supply-chain-attack/
1•taubek•32m ago•0 comments

The SGI Buyer's Guide

https://hardware.majix.org/computers/sgi/buyers-guide.shtml
1•uticus•34m ago•1 comments

Crypto-Agility Is a Runtime Property, Not a Compliance Checkbox

https://mayckongiovani.substack.com/p/pqc-engineering-series-deep-dive-8f2
1•doomhammerhell•34m ago•0 comments

C++26: Standard Library Hardening

https://www.sandordargo.com/blog/2026/05/13/cpp26-library-hardening
1•ibobev•35m ago•0 comments

ASCII by Jason Scott

https://ascii.textfiles.com/
15•bookofjoe•37m ago•2 comments