That file example looks familiar.
First off, sometimes we can read the file into a string, and process it that way:
f = fileToString("filename.bar")
process(f)
The function opens and closes it on its own, so we don't have to manage closing. However, if we cannot do it that way, then the code can be simplified:
f = openFile("filename")
process_file(f)
f.close
If you consider the total code that is likely involved, then it would probably be less than 5 percent difference. I don't remember any programs I have ever done that had more than a few file open commands. If there are many, then likely it can be put into a loop or some other pattern that can be factored to fewer spots. Thus, it may add about 3 more lines to a total of something like 2000, which is not going to come close to 5 percent.
I will agree the closure may better ensure that the file gets closed, but if the language supports destructors, we can do something similar without closures. As soon as the variable/object/array goes out of scope, its destructor is called. (Different languages will support the timing and syntax differently, I would note).
If you are reading one character at a time, then the getNextChar() function could also automatically close the file at the end of the stream:
f = openFile("filename", autoClose=true)
while (getNextChar(f)) {
. . . processChar(f.char)
}
Here, the API takes care of closing for us. We could also leave it open if we want to do something else with it after the first loop.
A "killer" closure example would perhaps be one where a similar operation needs to be done many times per routine, not just once or twice, and in lots of routines. I have yet to see such. Someone suggested HTML tags as an example, but one can use wrappers for these:
s = "make me bold"
s = boldifier(s)
print(s)
function boldifier(s) {
. . . return ("[b]" . s . "[/b]")
}
(I used square brackets instead of angle brackets here, and dots to force indentation so that I don't have to mess around with HTML.)