TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Danger: Overloaded Constructors Ahead

13 pointsby karagenitover 3 years ago

5 comments

kelnosover 3 years ago
Oof, the lesson that should have been learned here is that this is an inherently unsafe way to design the API of this class. While Java&#x27;s type system isn&#x27;t the most expressive in the world, certainly you could design something that makes it impossible to construct in a way that you&#x27;d ever see this failure.<p>Hell, you could even use a builder pattern (and make the constructor private), which is pretty common in Java-land:<p><pre><code> final ErrorResponse errorResponse = ErrorResponse.newBuilder() .withSuccesses(1, &quot;Great!&quot;) .withWarnings(3, &quot;Hmm...&quot;) .withErrors(1, &quot;Uh oh&quot;) .build(); </code></pre> If you have no warnings (for example), you don&#x27;t call `.withWarnings()`, and then you have zero warnings and no message to go with it. If you have warnings, you call the `.withWarnings()` method and you <i>must</i> provide both bits of information.<p>Yes, it&#x27;s more verbose. But that&#x27;s Java for you, and the key takeaway is that it&#x27;s impossible to construct an `ErrorMessage` with invalid internal state (assume that the `.with*()` methods have proper preconditions, etc.).<p>In addition, `.getSuccessMsg()` etc. should return `Optional&lt;String&gt;`. Alternatively, you could just have `.getSuccesses()`, which returns something like `Optional&lt;ResponseData&gt;`, which is just a poor-man&#x27;s 2-tuple of (numSuccesses, successMsg). (And if you&#x27;re using a recent Java version [which I suspect Salesforce is not], you can even use a record to save you the boilerplate.)
评论 #28629076 未加载
评论 #28628294 未加载
trav4225over 3 years ago
&gt; Just because one field tells you something should be there doesn’t mean an extra null-check would hurt.<p>IMO, it would. NPE exists for a reason: to instantly alert you to a bug and&#x2F;or contract violation. Maybe I misunderstood the author&#x27;s intent here, but sweeping the contract violation under the rug with a superfluous null check doesn&#x27;t seem to help anything here. If successMsg is null, what are you going to do, display &quot;&quot; instead? Often, immediate termination with NPE seems to be the best approach in cases like this, IMHO.
评论 #28624799 未加载
评论 #28625335 未加载
评论 #28629165 未加载
jamesfinlaysonover 3 years ago
Overloaded constructors where the parameter order is different in overloads doesn&#x27;t seem like a great idea.<p>Surely<p>&gt; public ErrorResponse(int successCount, int errorCount, String errorMsg, String warningMsg);<p>&gt; public ErrorResponse(int successCount, int errorCount, String errorMsg, String warningMsg, String successMsg);<p>would cause fewer problems than<p>&gt; public ErrorResponse(int successCount, int errorCount, String warningMsg, String errorMsg);<p>&gt; public ErrorResponse(int successCount, int errorCount, String successMsg, String warningMsg, String errorMsg);
评论 #28625079 未加载
评论 #28629215 未加载
TheDudeManover 3 years ago
Once you get past some threshold of number of params, switch it to a single param of a type that can be constructed using the builder pattern.
评论 #28629225 未加载
selcukaover 3 years ago
I am more surprised to see that Java didn&#x27;t raise a compile time error for trying to read the value of an argument that doesn&#x27;t exist in the method signature (successMsg).
评论 #28629245 未加载