Re: Some same exceptions used in a given file
Patricia Shanahan <pats@acm.org> writes:
If neither of those ideas answers the actual question, please post an
example.
I came to recognize four grades of code (ignoring shades of
grey between them):
- q&d code: Get a job done as fast as possible, then
throw away the code, assume all operations will work
as intended, i.e., when opening a file, assume it is
there. Assume, the programer is present when the
program is running, so that he can fix any problems
as they are observed. In case of run-time errors,
the behavior is undefined.
- q&d+ code: In case of run-time errors, just terminate.
Think of Perl code like:
open FILE, "/etc/passwd" or die "Can't open /etc/passed";
- hq code: Observe all the rules, document everything,
write exception-safe code, clean up resources as
early as possible, refactor, write tests, ...
- library-grade code: is hq code written so that it can be
put into a library, its methods communicate only via
their interface, not with the environment, and always
return to the caller.
Sometimes q&d code is appropriate, and for such code, one
might event write utilities, such as
java.io.BufferedReader qndFopen( String path )
{ java.io.BufferedReader result = null;
try
{ result = new java.io.BufferedReader
( new java.io.InputStreamReader
( new java.io.FileInputStream( path ))); }
catch( final Exception e ){}
return result; }
Now, if that exception is not just ignored, but the program
is terminated with an error report, this already is on the
q&d+ level, i.e., one step higher on the quality ladder:
java.io.BufferedReader qndpFopen( String path )
{ java.io.BufferedReader result = null;
try
{ result = new java.io.BufferedReader
( new java.io.InputStreamReader
( new java.io.FileInputStream( path ))); }
catch( final Exception e )
{ java.lang.System.err.println( e );
System.exit( 1 ); }
return result; }
It is still not library-grade, of course, but sometimes
approriate. It also might be another idea for what the OP
was looking for.