[Scilab-users] linking (jumping) from line of code to an other

Tim Wescott tim at wescottdesign.com
Fri May 12 18:50:40 CEST 2017


Jumps are generally bad style; I don't know if Scilab even supports
them.  If it does I advise you not to use them for something this
trivial (error handling is the only place I've seen them seriously
recommended, and even there they can be very problematical -- it's why
people invented exceptions).

For something that small the cost in making your code into spaghetti is
larger than the cost of just doing it.

The two ways that software professionals would deal with this are to
either get rid of 100 as a "magic number" and assign its value to a
variable (i.e., before the "for" statement assign PowerIncrement =
100), or rearrange the conditional so that the P = P + PowerIncrement
line is only encountered once.

Or do both.

Certainly if you use the existing structure and if the two lines that
increment P are supposed to do the same thing always, then using a
named variable is a Good Thing -- that means that at some later date,
when some poor ignorant fool reads your code they don't have to wonder
if the "P+100" in one spot means the same thing as the "P+100" in
another, AND they don't run the risk of changing the (hopefully proper)
increment in one place but not another.

And note, based on my experience writing software for the last 40
years, that often the poor ignorant fool who has to maintain your
software is you, months or years down the road -- so it pays to make
your code readable.  Nothing makes you feel dumber than not being able
to understand your own code, because no matter which way the blame
flies, it always lands squarely on you.

On Fri, 2017-05-12 at 11:15 +0200, Frieder Nikolaisen wrote:
> Hello,
> another question to solve the locomotive stuff, giving you a example
> with the not implented code. 
> I don't want to code the same stuff twice. Thats why I want to jump
> between lines of code. Is this possible and how? 
> P = 200;
> DM = 1;
> 
> for n = 1:10
>     if DM == 1 then 
>         if P > 100 then     DM = 1
>                             P = P - 60
>                             disp('P bigger 100. DM = ' + string(DM))
>         else    DM = 0
>                             P = P + 100 //instead of writing P = P +
> 100, I would like ...
>                             disp('P smaller 100. DM = ' + string(DM))
>         end 
>     else // D == 0 
>         if P > 100 then DM = 1          
>                                         disp('P bigger 100. DM = ' +
> string(DM))
>                         P = P - 30
>         else        DM = 0              // ... to continue here 
>                                         disp('P smaller 100. DM = ' +
> string(DM))
>                     P = P + 100       // to use this P = P + 100
>         end
>     end 
>     disp(string(n) + ' ' + string(P))
> end
>  
> _______________________________________________
> users mailing list
> users at lists.scilab.org
> http://lists.scilab.org/mailman/listinfo/users
-- 

Tim Wescott
www.wescottdesign.com
Control & Communications systems, circuit & software design.
Phone: 503.631.7815
Cell:  503.349.8432






More information about the users mailing list