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: MSB/MIB – A mobility layer on top of GDP and UBI (GitHub)

https://github.com/mroblesg88/msb-mib-gross-mobility
1•juanmrobles•43s ago•0 comments

Why Most Redesigns Fail: Lessons from Sweden's Traffic Switch

https://mikajovicic.com/writing/the-myth-of-big-redesign/
2•violinar•6m ago•0 comments

TailAdmin Laravel is now available: a Tailwind CSS dashboard kit for Laravel

https://github.com/TailAdmin/tailadmin-laravel
1•vinishbhaskar•6m ago•1 comments

Show HN: OgBlocks – UI Library for Non-CSS Developers

https://ogblocks.dev/
1•karanzkk•7m ago•0 comments

How do salespeople use AI?

https://twitter.com/bayka/status/1998650248051523846
1•Bayram•10m ago•1 comments

The Unwritten Laws of Systems Engineering (1994)

https://evansopticalengineering.com/Page00/sysenlaw.htm
3•o4c•13m ago•0 comments

Quilter's AI designed a Linux computer that booted on the first try

https://venturebeat.com/ai/quilters-ai-just-designed-an-843-part-linux-computer-that-booted-on-the
2•guiambros•14m ago•0 comments

German government US authorities have access to EU cloud data (heise.de)

https://www.heise.de/news/Gutachten-US-Behoerden-haben-weitreichenden-Zugriff-auf-europaeische-Cl...
2•m-akcan•17m ago•1 comments

Trump Admin Is Preparing to Revoke Visas of Critics of Elon Musk's Twitter

https://zeteo.com/p/trump-revoke-visas-breton-ahmed-twitter-musk
2•mdhb•17m ago•0 comments

How HTML Changes in ePub

https://htmhell.dev/adventcalendar/2025/11/
2•robin_reala•20m ago•0 comments

Show HN: Video Frame Extractor – Extract frames in the browser

https://www.blurimageonline.com/video-frame-extractor
1•teroquyiqwu•20m ago•1 comments

Dedicated GPU Server from Hetzner

https://www.hetzner.com/dedicated-rootserver/matrix-gpu/
3•Scotrix•25m ago•1 comments

The ivory tower of Platform Engineering

https://platformengineering.org/blog/the-ivory-tower-of-platform-engineering
2•gpi•26m ago•0 comments

On-Prem Backup Still Makes Sense

https://medium.com/@justinwang001/why-on-prem-backup-still-makes-sense-5723504c2d7b
3•jerrywjh•29m ago•0 comments

Exploration of basic human values in 38 million obituaries over 30 years [pdf]

https://moralitylab.bc.edu/wp-content/uploads/sites/192/2025/09/markowitz-et-al-an-exploration-of...
3•sipofwater•35m ago•1 comments

LLM-Powered Relevance Assessment for Pinterest Search

https://medium.com/pinterest-engineering/llm-powered-relevance-assessment-for-pinterest-search-b8...
1•tsenturk•40m ago•0 comments

The Cost of a Closure in C

https://thephd.dev/the-cost-of-a-closure-in-c-c2y
4•ingve•41m ago•0 comments

We are in the era of Science Slop (and it's exciting)

https://superposer.substack.com/p/we-are-in-the-era-of-science-slop
2•stared•43m ago•0 comments

Show HN: DailyMe–I built an RPG task tracker for ADHD son build self-disclpline

https://dailyme.app/
3•nananono•44m ago•1 comments

Beyond the FGC-9: How the Urutau Redefines the Global 3D-Printed Firearms

https://gnet-research.org/2025/01/08/beyond-the-fgc-9-how-the-urutau-redefines-the-global-3d-prin...
1•f1shy•49m ago•0 comments

Project Chrono an Open Source Multi-Physics Simulation Engine

https://projectchrono.org/
3•lorenzohess•54m ago•0 comments

What I Look for in AI-Assisted PRs

https://benjamincongdon.me/blog/2025/12/10/What-I-Look-For-in-AI-Assisted-PRs/
1•ingve•55m ago•0 comments

Show HN: Browser-based encryption tools with zero server-side processing

https://ikrypt.com/
1•digi_wares•55m ago•0 comments

Show HN: RequestHunt – Find real feature requests from Reddit, X, and HN

https://www.requesthunt.com/
2•Zephyr0x•57m ago•0 comments

You Cannot Fix Rotten Soil

https://alifeengineered.substack.com/p/you-cannot-fix-rotten-soil
3•gpi•1h ago•1 comments

Pick a door. I'll judge you

https://nathanpmyoung.substack.com/p/pick-a-door-ill-judge-you
4•mparramon•1h ago•0 comments

Jetbrains Fixes 20 Year Old Feature Request

https://youtrack.jetbrains.com/issue/CPP-4141/Make-CLion-available-as-IntelliJ-plugin
3•krisgenre•1h ago•0 comments

A new way to see and control your algorithm

https://about.instagram.com/blog/announcements/reels-algorithm-control
1•ChrisArchitect•1h ago•0 comments

Handling Email in Emacs

https://stuff.sigvaldason.com/email.html
1•harryday•1h ago•1 comments

Sea urchin species on brink of extinction after marine pandemic

https://www.theguardian.com/environment/2025/dec/11/sea-urchin-species-on-brink-of-extinction-aft...
2•mykowebhn•1h ago•0 comments