java - Do we really need to call flush() just before close() today? -


i read question using flush() before close() , , accepted answer means follow pattern.

just bufferedwriter#close() or filteroutputstream.#close() , if of buffered stream/writer call flush() when call close() , if (the dev , dev review code) know that, still need this? if yes, reason?

as javadoc says, don't need flush yourself. but, it's still do, considering readers, , common sense.

few experts know javadoc heart. wouldn't know sure if stream flushed or not without looking up, , i'm not alone. seeing explicit flush() call makes clear, , therefore makes code easier read.

furthermore, method name close() implies nothing flushing. it's closeable interface, , naturally, says nothing flushing. if don't flush buffered output stream before closing, despite wanting flush it, you'll relying on assumptions may or may not true. weaken implementation.

any assumptions make, should somehow pass on future maintainers. 1 way leaving comment:

// no need flush() manually, close() automatically 

if don't leave comment, future maintainers may have lookup javadoc too, if me don't have memorized. then, why write such comment when it's easier , better call , done it.

in short, flushing first before closing following logic. no need assumptions , second guesses, , no need make readers think.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -