frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Never let a good domain go to waste

https://www.whitehouse.gov/lab-leak-true-origins-of-covid-19/
1•blackdogie•30s ago•0 comments

Fighting Context Drift

https://github.com/dsadsadsadsadas/Trepan
1•slx44i•1m ago•0 comments

Aristotle Reasoning Agent

https://aristotle.harmonic.fun/
1•bananaflag•2m ago•1 comments

Microsoft weighs legal action over $50B Amazon-OpenAI cloud deal

https://www.ft.com/content/e814f4c3-4fb5-4e2e-90a6-470044436b39
2•markjgx•6m ago•0 comments

PondDB – Self-hosted agent memory database built on DuckDB

https://github.com/pond-db/pond-db
2•houtianlu•7m ago•0 comments

ORYN – local-first autonomous cybersecurity console, built in 7 days

https://github.com/alihassanassi/ORYN
1•alihassanassi•9m ago•0 comments

Ways to protect your money from potentially higher inflation and unemployment

https://www.cnn.com/2026/03/18/business/consumer-interest-rates-inflation-unemployment-your-money
1•mooreds•12m ago•0 comments

Ask HN: Why is everyone on HN obsessed with Rust?

3•goldkey•12m ago•1 comments

2026, the Year of the Horse

https://www.carlyhasredhair.com/p/2026-the-year-of-the-horse
1•mooreds•13m ago•0 comments

Show HN: WheresMyPermitSF – Self-Hosted SF Housing Permit Bottleneck Analyzer

https://wheresmypermitsf.candace.cloud/
1•kaashmonee•14m ago•0 comments

Why AI Tools Fail – and Where the Real Lever Is

https://dekodiert.de/en/articles/maschinenlesbarer-kontext
1•sdoering•15m ago•0 comments

CISA Urges Hardening After Cyberattack Against Stryker

https://www.cisa.gov/news-events/alerts/2026/03/18/cisa-urges-endpoint-management-system-hardenin...
2•dboreham•15m ago•0 comments

French bulldog frenzy may be cooling in US, but dachshunds are riding high

https://apnews.com/article/popular-dog-breeds-dachshund-french-bulldog-d94ee9db8d56bcb29ccf39e855...
2•mooreds•16m ago•1 comments

Our new Windsurf pricing plans

https://windsurf.com/blog/windsurf-pricing-plans
1•qainsights•19m ago•0 comments

Fruity Loops

https://en.wikipedia.org/wiki/FL_Studio
1•bigwheels•19m ago•0 comments

Shooting-Free Days Decline in Major U.S. Cities

https://www.publichealth.columbia.edu/news/shooting-free-days-decline-major-u-s-cities-new-metric...
3•littlexsparkee•26m ago•0 comments

Ask HN: Which router makers do you trust most?

4•general_reveal•26m ago•3 comments

Show HN: A Genetic algorithm that red-teams your copy with 100 LLM personas

https://crashtestcopy.com/
3•vignesh_warar•27m ago•0 comments

ServerGlance iOS app for real-time SSH server monitoring

https://apps.apple.com/us/app/serverglance-ssh-monitor/id6758614736
3•fatihkurt•27m ago•0 comments

Website to post Claude fuck ups

https://claudedidwhat.wtf
20•bcgsh•28m ago•5 comments

The fiery, deadly crashes involving the Tesla Cybertruck

https://www.theguardian.com/technology/2026/mar/18/tesla-cybertruck-crashes-battery-fires
2•abawany•30m ago•0 comments

Vineland

https://en.wikipedia.org/wiki/Vineland
3•doener•31m ago•0 comments

WebGL Considered Harmful (2011)

https://www.microsoft.com/en-us/msrc/blog/2011/06/webgl-considered-harmful
2•leonidasv•34m ago•0 comments

How to Audit a Blog Post for SEO Without Using 5 Different Tools

https://kitful.ai/blog/how-to-audit-a-blog-post-for-seo-without-using-5-different-tools
2•eashish93•37m ago•0 comments

Thinking about objects in 4D space-time is efficient and consistent (ontology)

https://rubber-duck-typing.com/posts/2026-03-16-system-engineering-perdurantism.html
2•sayon•37m ago•0 comments

Naturalistic Driving Study on Cannabis Use in Washington and Virginia

https://vtechworks.lib.vt.edu/server/api/core/bitstreams/db02223d-1a8b-48be-a432-450cb39c3d41/con...
2•PaulHoule•39m ago•0 comments

State of the Docs by Gitbook

https://www.stateofdocs.com/2026
3•fazkan•40m ago•0 comments

Show HN: Blobsearch – Object storage and DuckDB based Elasticsearch alternative

https://github.com/amr8t/blobsearch
2•conqrr•40m ago•0 comments

Anthropic dispatch: more OpenClaw competiton

https://old.reddit.com/r/Anthropic/comments/1rx1z5c/anthropic_launched_a_new_cowork_feature_called/
3•gigatexal•42m ago•0 comments

Userdb: Add birthDate field to JSON user records

https://github.com/systemd/systemd/pull/40954
1•airhangerf15•46m ago•1 comments