frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Show HN: PyTorch-World – Modular Library to Build and Train World Models

https://github.com/ParamThakkar123/pytorch-world
1•paramthakkar•3m ago•1 comments

Show HN: Env files aren't meant for storing secrets

https://github.com/BinSquare/envmap
1•binsquare•7m ago•0 comments

The Strange World of David Lynch

https://thequietus.com/interviews/strange-world-of/david-lynch-best-films/
1•thinkingemote•9m ago•0 comments

News publishers embrace vertical video with in-app "watch" tabs

https://www.niemanlab.org/2025/11/news-publishers-embrace-vertical-video-with-in-app-watch-tabs/
1•giuliomagnifico•22m ago•0 comments

Show HN: BugMagnet for Claude Code and Cursor – automated exploratory testing

https://github.com/gojko/bugmagnet-ai-assistant
1•adzicg•23m ago•0 comments

Becoming Unblockable

https://www.seangoedecke.com/unblockable/
1•cyprien_g•24m ago•0 comments

Parametric 3D model generator powered by build123d and Blender pipeline

https://www.iteration3d.fr/en/pages/how-are-3d-files-generated.php
1•sylvainFR•25m ago•2 comments

Fuck Sheets – Prompt2Sheets – Edit Google Sheets in Plain English (No Formulas)

https://prompt2sheets.com
1•PEGHIN•25m ago•1 comments

Encyclopedia of Triangle Centers

https://faculty.evansville.edu/ck6/encyclopedia/etc.html
1•jjgreen•29m ago•0 comments

The New York Neighborhoods That Best Protect Against Extreme Heat

https://www.nytimes.com/2025/11/27/realestate/nyc-neighborhoods-climate-change.html
2•fleahunter•37m ago•0 comments

Tao: Intelligence is too high-dimensional for accurate low-dimensional narrative

https://mathstodon.xyz/@tao/115620261936846090
2•bertman•41m ago•0 comments

eth-explorer a minimal cli tool for visualizing blockchain events

https://github.com/AlbertoDino/eth_explorer
1•donbia•46m ago•1 comments

Necroprinting – Dead mosquito proboscis used for high-resolution 3D printing

https://www.tomshardware.com/3d-printing/dead-mosquito-proboscis-used-for-high-resolution-3d-prin...
6•indy•47m ago•0 comments

Case Examples of Coherence Therapy for Anxiety and Panic [pdf]

https://coherencetherapy.org/files/CaseEx-AnxPanic.pdf
1•plucafs•48m ago•0 comments

ZeroLu/awesome-nanobanana-pro: list of curated Nano Banana pro prompts

https://github.com/ZeroLu/awesome-nanobanana-pro
2•emreb•48m ago•0 comments

Building an AI Agent? Don't let it burn your budget

2•Saurabh_Kumar_•54m ago•1 comments

ShipSafer Is Hyped Up

https://shipsafer.app/waitlist
1•usegrand•55m ago•1 comments

Engineering Multiplier Archetypes – By Gregor Ojstersek

https://newsletter.eng-leadership.com/p/engineering-multiplier-archetypes
2•rbanffy•59m ago•0 comments

Ferrari's Formula One pit-stops and handovers from surgery to intensive care [pdf]

https://gwern.net/doc/technology/2008-sower.pdf
1•fanf2•59m ago•0 comments

Lazy Linearity for a Core Functional Language (POPL 2026)

https://alt-romes.github.io/posts/2025-11-26-lazy-linearity-popl26.html
3•todsacerdoti•1h ago•0 comments

Testing image editing models through 100 recursive edits

https://www.willienotwilly.com/thats-not-the-rock
1•falloon•1h ago•0 comments

Show HN: Tampo – One app for personal tasks and team projects

https://tampoapp.com
3•AbdullahSheraz•1h ago•0 comments

We found CRISPR targets in genomic 'dead zones' using semiprime factorization"

https://github.com/Joedaddy66/integer-resonance-crispr
2•JOEDADDY66•1h ago•0 comments

How to Clean Up Your Rails Logs: Ignoring Benign SQL Warnings

https://blog.saeloun.com/2025/11/27/sql-warnings-ignored-error-codes-rails/
2•amalinovic•1h ago•0 comments

Show HN: I got tired of doing SEO work so I automated it

https://www.blogseo.io
3•vincejos•1h ago•2 comments

How to Deal with AI Restrictions When Upscaling Images

https://ivanca.github.io/programming/2025/11/26/bypass-ai-upscalint-restrictions/
2•AmbroseBierce•1h ago•0 comments

Major LLMs fabricate evidence when presented with governance critiques

https://zenodo.org/records/17728992
6•mikeup91•1h ago•1 comments

New research highlights a shortage of male mentors for boys and young men

https://www.psypost.org/new-research-highlights-a-shortage-of-male-mentors-for-boys-and-young-men/
27•ashishgupta2209•1h ago•32 comments

Using Claude to create a bootable Forth OS

https://isene.org/2025/11/SimplicityOS.html
3•veltas•1h ago•0 comments

My Rebuttal to Simon Cowell's Rolling Stone Statement

https://twitter.com/katiewaissel24/status/1993828722500055299
3•mellosouls•1h ago•0 comments