在每次循环通过时重新分配while循环的布尔表达式时,如何避免违反DRY原理? [英] How to avoid violating the DRY principle when the boolean expression of a while loop is reassigned every loop pass?

查看:119
本文介绍了在每次循环通过时重新分配while循环的布尔表达式时,如何避免违反DRY原理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两者

while MyFunction(1, 2, 3) > 0 do
  begin
    Temp := MyFunction(1, 2, 3);
    ShowMessage(IntToStr(Temp));
  end;

Temp := MyFunction(1, 2, 3);
while Temp > 0 do
  begin
    ShowMessage(IntToStr(Temp));
    Temp := MyFunction(1, 2, 3);
  end;

违反了 DRY原则,因为只有两个调用MyFunction一个是必要的.

violate the DRY principle because there are two calls to MyFunction when only one is necessary.

推荐答案

简单

  function dotemp(a,b,c:integer;var value : integer):boolean;
  begin
    value:a*b+c;
    result:=value>0; 
  end;

  begin
    while dotemp(a,b,c,temp) do
        begin
           Operateontemp(temp);
         end;
  end;

Pascal没有左值分配,但是有var参数.

Pascal has no lvalue assignments, but it DOES have var parameters.

这篇关于在每次循环通过时重新分配while循环的布尔表达式时,如何避免违反DRY原理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆