Loading ...
Sorry, an error occurred while loading the content.

66596Re: [XP] Writing simple software - A Challenge!

Expand Messages
  • Ron Jeffries
    Dec 1, 2002
      On Saturday, November 30, 2002, at 9:52:39 PM, Kay A. Pentecost wrote:

      >> > Oh. I thought that he meant exceptions like in "throwing them..."
      >>
      >> I did, but I also meant what Kevin meant.

      > Well, would you explain more about not throwing them?

      Um.

      Don't throw exceptions,
      Find any alternative,
      To do the right thing.

      Nearly anything we need to do can be defined in such a way as to work
      over the full range of its arguments. Where this is possible, do so.

      Write code not to throw new exceptions, but allow it to throw existing
      ones if you are passed unworkable inputs. If all the code is your own,
      use and trust your tests to be sure that methods are used "properly"
      rather than throwing exceptions when they are not.

      It is true, as was stated above, that sometimes an exception might
      finally occur, such as when passed a null, way down in the guts. In
      this case, one alternative is to break the rule "Don't Throw
      Exceptions". Another alternative is to use the possibly null argument
      sooner so that the error isn't way down.

      The best alternative may be to make the null argument work, or to
      write the code in such a way as to make null arguments impossible.
      (Some languages allow one to declare that arguments are not nullable.)

      If the code you're writing will be used by "outsiders", it may need to
      protect itself against misuse. Even here, appropriate definition of
      what it does will help.

      For Example:

      public string ConcatenateTextFiles(string[] textfilenames) returns a
      string containing all the lines of text from all the files. If any
      of the files contains characters that are not text, the method will
      throw a NonTextCharacterFound exception. If textfilenames is null or
      empty, the method will throw a NoFilesToProcess exception. If the
      resulting string would exceed 32767 characters, the method will
      throw a TooMuchDataForMe exception. If any of the file names does
      not refer to a file that can be read, either a CannotFindFile, or
      CannotOpenFileForRead exception will be thrown.

      or:

      public string ConcatenateTextFiles(string[] textfilenames) returns a
      string containing all the lines of text from all the files listed,
      up to a maximum of 32767 characters. If no text is found, the method
      returns an empty string. Non-text characters are ignored. File names
      that cannot be opened or read are ignored.

      The second method is probably easier to write and certainly easier
      to use.

      Exceptions - avoid having any.

      Ron Jeffries
      www.XProgramming.com
      He who will not apply new remedies must expect old evils. -- Francis Bacon
    • Show all 139 messages in this topic