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

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

Expand Messages
  • Ron Jeffries
    ... 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
    Message 1 of 139 , Dec 1, 2002
    • 0 Attachment
      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
    • J. B. Rainsberger
      So said ericheikkila on 12/4/2002 -------------------- ... Often, i means something. Say what you mean; mean what you say. :) J. B. Rainsberger,
      Message 139 of 139 , Dec 7, 2002
      • 0 Attachment
        So said ericheikkila on 12/4/2002 --------------------

        >Single letter variables drive me nuts. ;)
        >I use 'index' instead of 'i' (or loop, or maybe count, depending on
        >the context).
        >As far as abbreviations go...if the entire team agrees, fine.
        >If someone on the team doesn't know that itr is the same as iterator,
        >just change it to iterator.
        >
        >Usually, I'll not abbrev ;)

        Often, "i" means something. Say what you mean; mean what you say. :)

        J. B. Rainsberger,
        President, Diaspar Software Services
        Let's write software that people understand.
        http://www.diasparsoftware.com/
        telephone: +1 416 791-8603
        All correspondence (c) 2002 Diaspar Software Services.
        If you want to use it, just ask; don't steal.
      Your message has been successfully submitted and would be delivered to recipients shortly.