frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

How to Fix Circular Imports in a React/TypeScript Application

https://jsdev.space/howto/circular-imports-ts/
1•javatuts•23s ago•0 comments

Show HN: VaultAI – 42 AI models on a portable SSD, works offline ($399)

https://vaultai.us/
1•laramie_co•1m ago•1 comments

A 97M-parameter model outperforms 900M for warehouse robot coordination

https://rovnou.com/en/blog/deepfleet
1•ekusiadadus•4m ago•1 comments

Show HN: HushWork – A calm focus environment for focus in one tab

https://hushwork.app
1•pgebski•5m ago•1 comments

The path to ubiquitous AI (17k tokens/sec)

https://taalas.com/the-path-to-ubiquitous-ai/
1•sidnarsipur•9m ago•0 comments

SwiftUI Agent Skill: Build Better Views with AI

https://www.avanderlee.com/ai-development/swiftui-agent-skill-build-better-views-with-ai/
2•skreep•11m ago•0 comments

Chess engine, pt. 3: Elo, and rigorous SPRT testing

https://www.dogeystamp.com/chess3/
1•luu•12m ago•0 comments

Show HN: R2Stat – Monitor your Cloudflare R2 free Class B quota

https://r2stat.com/
1•jacoka•15m ago•1 comments

Agentic Engineering Best Practices

1•kingJulio•16m ago•0 comments

I tripled my SaaS prices after 2 weeks and signups didn't drop

https://web-production-71423.up.railway.app/
1•Shmungus•16m ago•1 comments

Show HN: One async PHP process serving web, REST API, and MCP for AI agents

https://pascualmg.dev/blog/pascual/one-async-php-process-web-server-rest-api-and-mcp-for-ai-agents
1•passh•17m ago•0 comments

Improving Interactive In-Context Learning from Natural Lang Feedback – DeepMind

https://arxiv.org/abs/2602.16066
2•zerop•21m ago•0 comments

Show HN: Behavr – Run realistic user simulations on your prototypes in minutes

1•Behavrai•26m ago•1 comments

Microsoft: Anti-phishing rules mistakenly blocked emails, Teams messages

https://www.bleepingcomputer.com/news/microsoft/microsoft-anti-phishing-rules-mistakenly-blocked-...
1•exploraz•28m ago•0 comments

Arpa.net

http://www.arpa.net/
1•TigerUniversity•30m ago•0 comments

Cloudflare uses ClickHouse to scale analytics at quadrillion-row scale

https://clickhouse.com/blog/cloudflare
2•samaysharma•30m ago•0 comments

Laurie Spiegel's pioneering '80s music software, Music Mouse, returns updated

https://djmag.com/news/laurie-spiegels-pioneering-80s-music-making-software-music-mouse-returns-m...
3•coffeeyesplease•31m ago•0 comments

A Constructive Look at TempleOS

http://www.codersnotes.com/notes/a-constructive-look-at-templeos/
2•TigerUniversity•33m ago•0 comments

Accenture combats AI refuseniks by linking promotions to log-ins

https://www.ft.com/content/ac672f97-a603-4c56-afa3-4a5273d45674
2•cianmm•40m ago•1 comments

Show HN: I Emulated My Childhood

https://sklivvz.com/posts/i-finally-emulated-my-childhood
2•sklivvz1971•44m ago•0 comments

Show HN: 17MB pronunciation scorer beats human experts at phoneme level

2•fabiosuizu•45m ago•0 comments

The Great Locomotive Chase

https://en.wikipedia.org/wiki/Great_Locomotive_Chase
3•keiferski•45m ago•0 comments

State of Generative Media

https://fal.ai/gen-media-report-volume-1
1•mdrzn•45m ago•0 comments

Brave Iranians gather in central Iran to honour those killed in uprising

https://twitter.com/IranIntl_En/status/2024767317893075330
8•ukblewis•46m ago•1 comments

Trump to order declassification of UFO/UAP related files

https://twitter.com/TrumpDailyPosts/status/2024661955479556382
1•lucasRW•47m ago•0 comments

Show HN: SaveTheTrade – a simple trade journal and performance tracker

https://survivethetrade.com/
1•daniellax•47m ago•0 comments

Wikipedia has deprecated and will blacklist archive.today

https://en.wikipedia.org/wiki/Wikipedia:Archive.today_guidance
7•gyrovague-com•51m ago•3 comments

TamboUI: A Modern Terminal UI Framework for Java (GraalVM Native)

https://github.com/tamboui/tamboui
1•mikepapadim•52m ago•0 comments

Optimism Plunges 28% as Base Drifts from OP Stack: What's Next?

https://timescrypto.com/cryptonews/altcoins/optimism-plunges-28-as-base-drifts-from-op-stack-what...
1•Alan_Writer•53m ago•0 comments

Show HN: Aismond – attack-surface monitoring for MSP client fleets

https://www.aismond.com/
1•mirceamitu•53m ago•0 comments