Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Doppelten Code vermeiden? (https://www.delphipraxis.net/33924-doppelten-code-vermeiden.html)

Pseudemys Nelsoni 14. Nov 2004 21:52


Doppelten Code vermeiden?
 
moin,

Delphi-Quellcode:
procedure blubb(b: boolean);
begin
  case b of
    true:
      if irgendwas then
      begin
        DoSomething();
        DoSomething();
        DoSomething();
        DoSomething();
        DoSomething();
        DoSomething();
        DoSomething();
        DoSomething();
      end;
    false:
      if sonstwas then
      begin
        DoSomething();
        DoSomething();
        DoSomething();
        DoSomething();
        DoSomething();
        DoSomething();
        DoSomething();
        DoSomething();
      end;
  end;
end;

die 2 case teile unterscheiden sich NUR von der if abfrage, kann ich den code nicht irgendwie kleiner machen? die prozeduren innerhalb der cases sind 100% gleich

StefanDP 14. Nov 2004 21:54

Re: Doppelten Code vermeiden?
 
das wird ja immer ausgeführt, da boolean ja nur true oder false sein kann?!

/edit: sry, hab das irgendwas/sonstwas überlesen!

dann pack halt den teil den du doppelt hast in eine eigene funktion!

oder mach das so:
Delphi-Quellcode:
if (b and irgndwas) or ((not b) and sontwas) then
...

Mystic 14. Nov 2004 21:55

Re: Doppelten Code vermeiden?
 
Delphi-Quellcode:
procedure blubb(b: boolean);
begin
  if ((b) and (irgendwas)) or ((not b) and (sonstwas)) then
  begin
    DoSomething();
    DoSomething();
    DoSomething();
    DoSomething();
    DoSomething();
    DoSomething();
    DoSomething();
    DoSomething();
  end;
end;

Pseudemys Nelsoni 14. Nov 2004 22:15

Re: Doppelten Code vermeiden?
 
ahhh, danke (habs), 8)

himitsu 15. Nov 2004 07:03

Re: Doppelten Code vermeiden?
 
hmm? ich glaub, ich hab in Info mal zuviel aufgepasst :roll:

Code:
[s]// (a and b) or (-a and b) ... (a and b) or (c and b)
if (b and irgndwas) or ((not b) and sontwas) then

// Regel: (a and b) or (c and b) = (a or c) and b

// (a or c) and b
if (b or (not b)) and sontwas then

// Regel: a or -a = 1

// 1 and b
if true and sontwas then

// Regel: 1 and b = b

// b
if sontwas then[/s]

fiasko 15. Nov 2004 07:54

Re: Doppelten Code vermeiden?
 
Hallo,

Zitat:

Zitat von himitsu
hmm? ich glaub, ich hab in Info mal zuviel aufgepasst :roll:

Delphi-Quellcode:
// Regel: (a and b) or (c and b) = (a or c) and b

das kannst du hier nicht anwenden, da kein Operand in beiden and's vorkommt!

himitsu 15. Nov 2004 09:17

Re: Doppelten Code vermeiden?
 
stümmt :wall:
's war wohl noch zu früh ... irgendwas <> sonstwas ... 's sah heute früh irgendwie noch gleich aus :oops:

dann hat der Stefan halt weiterhin die beste Lösung :angel2:

mh166 15. Nov 2004 18:38

Re: Doppelten Code vermeiden?
 
Also ich würde das wie Stefan machen:

Delphi-Quellcode:
procedure blubb(b: boolean);

  procedure EverytimeTheSameShit;
  begin
    DoSomething();
    DoSomething();
    DoSomething();
    DoSomething();
    DoSomething();
    DoSomething();
    DoSomething();
    DoSomething();  
  end;

begin
  case b of
    true:
      if irgendwas then
      begin
        EverytimeTheSameShit;
      end;
    false:
      if sonstwas then
      begin
        EverytimeTheSameShit;
      end;
  end;
end;
mfg, mh166


Alle Zeitangaben in WEZ +1. Es ist jetzt 17:35 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz