Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
Keep your IF ... ELSE conditions reduced to a minimum Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
23-Jun-03
Category
Algorithm
Language
Delphi 2.x
Views
133
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Fernando Martins

Make simple conditions easier to read and mantain

Answer:

Sometimes you have conditions like 
1   
2   if condition then
3     Result := what_ever
4   else
5     Result := something;
6   
7   //Or, even worst, sometimes you have several conditions, like 
8   
9   if condition1 then
10    Result1 := what_ever1
11  else
12    Result1 := something1;
13  
14  if condition2 then
15    Result2 := what_ever2
16  else
17    Result2 := something2;
18  
19  if conditionN then
20    ResultN := what_everN
21  else
22    ResultN := somethingN;
23  
24  //Woundt it be much easy to write something like 
25  
26  Result := IFF(condition, result_when_condition_is_true,
27    result_when_condition_is_false);


What I propose here is a simple function that will reduce simple conditions into a 
single line, by receiving a condition and the values  to return when the condition 
is true or false. 

28  function IFF(C: Boolean; T, F: Variant): Variant;
29  begin
30    if C then
31      Result := T
32    else
33      Result := F;
34  end;
35  
36  Since the variables are variant type, you can pass any data type you want, like in 
37  these examples: 
38  
39  // will return 'Correct', since the condition is true
40  MyStr = IFF(TRUE, 'Correct', 'Incorrect');
41  
42  // will return 'Incorrect', since the condition is false
43  MyStr = IFF(TALSE, 'Correct', 'Incorrect');
44  
45  // will return X if X > Y, otherwise returns Y
46  MyInt = IFF(X > Y, X, Y);
47  
48  // will return TRUE, since TRUE or FALSE is TRUE
49  MyBool = IFF((TRUE or FALSE), TRUE, FALSE);
50  
51  // will return 0, since TRUE and FALSE is FALSE
52  MyInt = IFF((TRUE and FALSE), 1, 0);
53  
54  // is MyStr has a lenght grater then 0, returns its lenght, otherwise returns 0
55  MyInt = IFF(Lenght(MyStr) > 0, Lenght(MyStr), 0);
56  
57  // if 'Address:' is present on MyStr, it will return the lenght of the string, 
58  otherwise will return the string 'Not Found!'
59  MyVar = IFF(Pos('Address:', MyStr) > 0, Length(MyStr), 'Not found!');
60  
61  // if x is smaller or equal to 1, it returns X, otherwise it returns the 
62  multiplication of X by its predecessor
63  MyInt = IFF(X <= 1, X, X * (X - 1));


I've been using this funtion for a while and noticed that the code is easier to read and maintain. 

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC