in reply to Optimization of a piece of code(Is there a better way than this?)

Well... most obvious save is...
if($Max_Questions > $Total_Questions){ croak"Number of questions in $FileName exceeded"; }elsif($Max_Questions < 1){ croak"Must have at least one question in the test."; }else{ &_set_Max_Questions($self,(defined $Max_Questions ? $Max_Questions : + $Total_Questions)); }
you can also do $self->_set_Max_Questions(...) to make your code look more object oriented :) Update err, the $self-> thing is assuming you are using OO, based on the $self...

Update This is just plain wrong, see post below.

                - Ant
                - Some of my best work - (1 2 3)

  • Comment on Re: Optimization of a piece of code(Is there a better way than this?)
  • Download Code

Replies are listed 'Best First'.
Re: Optimization of a piece of code(Is there a better way than this?)
by Abigail-II (Bishop) on Jul 04, 2002 at 11:42 UTC
    I don't find this obvious at all, as it behaves differently.

    In the original code, if $Max_Questions is undefined, it gets a default value. In your code, if $Max_Questions isn't defined, you'll get two warnings, and it will croak with "Must have at least one question in the test.". The check on definedness of $Max_Question will not be reached if $Max_Question isn't defined.

    I don't think there's much wrong with the original code, and I suggest leaving as it is.

    Abigail

      Ack... good point, my bad... I missed the obvious...

                      - Ant
                      - Some of my best work - (1 2 3)