Replace CloseShield* constructors with factory methods#173
Conversation
|
These failed tests are unrelated to this PR. They are caused by test |
|
Hi @robtimus |
|
Hi All: I'm not a fan of the factory method name The following reads better to me: or: WDYT? |
|
I've come to like code that almost reads as if it's English. I don't get a feeling about how to interpret the first one: "a CloseShieldInputStream on a new FileInputStream". The How do you feel about simply calling the methods "wrap" or "wrapping"? If you don't like those options (and can't think of a better one), I think "with" is the best of the other two alternatives: "a CloseShieldInputStream with a new FileInputStream" does say what it means. |
|
I thought of another alternative that I think works both with and without static imports: |
+1 to wrap, it really captures what is going on here. |
|
I've renamed the methods to |
| * the underlying input stream is never closed. | ||
| * Use {@link #wrap(InputStream)} instead. | ||
| */ | ||
| @Deprecated |
There was a problem hiding this comment.
I don't agree with deprecating these APIs, different versions of different IDEs raise different warnings depending on your preferred configuration of said IDE. Let's keep this PR simpler and lighter, we can always add deprecation later if it is truly warranted.
There was a problem hiding this comment.
I feel that without deprecating the constructors, this PR adds no real value. People that currently use the constructors will keep doing so because they see no reason to change their code, and new users of the classes may only get confused if there are two ways to do the same thing. They'll probably end up using the constructors too because that's a character shorter (new vs .wrap).
My thought was that deprecating the constructors will make current users think about whether or not they have closed the underlying streams, even if their IDEs don't warn them. New users will chose the wrapper method because the alternative is deprecated.
The only downside to deprecating the constructors that I can see is that current users that don't have their IDEs setup to warn about unclosed streams will get warnings where there were none first, but as I said, perhaps they will think about whether or not their streams are properly closed somewhere else.
|
I will review all of this tomorrow (Thursday). |
CloseShieldOutputStream, CloseShieldWriter, #173. Sort methods: static methods come first. Fix some Javadocs. Some formatting.
|
@robtimus Merged to git master. Thank you for the PR :-) |
|
You're welcome :) |
Using
CloseShieldInputStream,CloseShieldOutputStream,CloseShieldReaderorCloseShieldWritercurrently makes it easier to let resource leaks go undetected. That's because most IDEs are smart when it comes to wrapping anInputStreaminto anotherInputStream, and likewise forOutputStream,ReaderandWriter- it will assume that theclosemethod delegates and therefore assumes that closing the wrapper is safe. For instance, the following will not produce an IDE warning that theFileInputStreamis never closed:This PR should make it easier for IDEs to detect such resource leaks by two simple changes:
Objects.requireNonNull, my IDE would complain about that too.)Using a static import for the wrapper method, the above code snippet becomes the following, and there is an IDE warning that the
FileInputStreamis never closed:I've tried to import and use all of the separate
dontClosemethods in one class, and the compiler has no problems with that, so there will be no ambiguity errors if you statically import more than onedontClosemethod.