Invert if/else/error

From Octave
Jump to: navigation, search

Introduction[edit]

The C++ error handling mechanism in core Octave has been changed to use exceptions. Previously, calling error() in C++ would always return execution to the calling function. This made it necessary to explicitly use the else keyword to control program flow. With exceptions, the C++ code may now be written in a manner that closely resembles Octave's own m-file language.

The principal work for this topic is to convert if/else statements that have an error() call in the else branch to a new format. The goal is to move the error() call to be after the if statement, eliminate the else branch, and decrease the indent of the remaining code. This requires reversing the conditional test in the if. For a single condition, the condition should be reversed rather than just adding the negation operator '!' to the start of the conditional. For a test with multiple conditions, use DeMorgan's Law (Demorgan's Law). In brief, you will need to negate each individual conditional and then change all && to || or vice versa.

Example 1 : Single Conditional (rows() from data.cc)[edit]

Before After
  bool do_set (const octave_value& v)
  {
    if (v.is_scalar_type () && v.is_real_type ())
      {
        double new_val = v.double_value ();

        if (new_val != current_val)
          {
            current_val = new_val;
            return true;
          }
      }
    else
      error ("set: invalid value for double property \"%s\"",
             get_name ().c_str ());
    return false;
  }
  bool do_set (const octave_value& v)
  {
    if (! v.is_scalar_type () || ! v.is_real_type ())
      error ("set: invalid value for double property \"%s\"",
             get_name ().c_str ());

    double new_val = v.double_value ();
  
    if (new_val != current_val)
      {
        current_val = new_val;
        return true;
      }

    return false;
  }

Detailed Instructions[edit]

The list of files which contain an instance of else followed by error() is shown in the Files section of this page. The actual instances, including line numbers, are shown in the Instances section. To avoid duplication, sign up for a particular file by editing the Files section of this wiki page and replacing '???' with your name. When you have edited a file you should verify that everything is okay by executing

make all
./run-octave
test FILENAME
When that passes, upload your patch to the patch tracker https://savannah.gnu.org/patch/?group=octave. If you can, e-mail the Octave Maintainer's list at octave-maintainers@gnu.org and let us know that there is something to review. Also, add the wiki tags
<strike> ... </strike>
to the Files section to cross the file off the list. In addition, increment the number of files that were fixed by +1.

Files[edit]

As of now, all the files have been corrected in Octave core source. Contributors are, however, welcomed to make changes as described above in Octave's packages.