473,385 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

very long if statement

29
hi everybody

i've been searching a clean and correct way to use very long if statements.

multiple options:

Expand|Select|Wrap|Line Numbers
  1. if (cond1 && cond2 && (cond3 && cond4) && (cond5 || cond6) {
  2. // do this
  3. }
  4.  
Expand|Select|Wrap|Line Numbers
  1. if (cond1) {
  2.   if (cond2) {
  3.      if (cond3 && cond4) {
  4.          if (cond5 || cond6) {
  5.          // do this
  6.          }
  7.      }
  8. }
  9.  
Expand|Select|Wrap|Line Numbers
  1. if ( CheckCondition() ) {
  2. // do this
  3. }
  4.  
  5. public bool CheckCondition () {
  6.   return bool a = cond1;
  7.   bool b = cond2;
  8.   bool c = (cond3 && cond4);
  9.   bool d = (cond5 || cond6);
  10.  
  11. }
  12.  
a switch case statement is in my matter not very useable.
What would be the best way to use, what are the points I should pay attention to? Are there any other options to obtain a fast and clean statement?

Thank you very much for the tips
Dec 9 '08 #1
5 1836
acoder
16,027 Expert Mod 8TB
The first method would be OK in a one-off scenario, but the second one is more flexible if you want to later add else part conditions.

The third method wouldn't work because it's not JavaScript and the code is incorrect. If you do find yourself using this if statement multiple times, a function does make sense, but code it something like this:
Expand|Select|Wrap|Line Numbers
  1. function CheckCondition () {
  2.   return (cond1 && cond2 && (cond3 && cond4) && (cond5 || cond6));
  3.  }
Dec 9 '08 #2
gits
5,390 Expert Mod 4TB
thats true ... :) and just consider to use a function only when you really need the check more then once ... a function-call is much slower then a direct call ... even when the code would look much cleaner with the function it increases runtime especially in large loops ... so when this might be an issue ... then use a direct call ... we often avoid a 'script is running slow' message just by removing function calls in large loops - just a note because sometimes this might make problems.

kind regards
Dec 9 '08 #3
tomaz
29
thanks for the tips.

my case looks as follows:

i create an array with all input elements.
a for loop runs untill all input elements are checked.
in that loop I have conditions that looks like this:
- is the type 'text' or 'password': ok
- i don't have the attribute 'no_vkb'
- i don't have the 'fs_enabled' and 'df_enabled' attribute
- the element is not disabled or read-only
- the style is not null.

so this if statement is done about 10 times on each page.
some pages have 30 input fields...

So, I suppose in this case, a function seems the right thing to do...
Dec 9 '08 #4
gits
5,390 Expert Mod 4TB
yep ... in such cases a function is not 'evil' ... i was talking about loops of hundreds of records ... sometimes here we have 3000 or even more ;)

kind regards
Dec 9 '08 #5
tomaz
29
in fact, i have 2 functions with such a loop:

one for the text input elements and one for the textarea fields.
So If I could create a function with common conditions for both functions, that would be clean. Or at least it seems clean ;)
in the loop itself I can add more specific conditions for each function, next to the CheckCondition function.


I'll try it this way...
Dec 9 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Allcomp | last post by:
Hello, I have seen something really strange in VB6 If I do a Int ( (5 * 1.2)) , I receive the value 5, but I should receive 6? Is this a bug or something really "normal". I can see that if I...
7
by: Generic Usenet Account | last post by:
For a template class, all the inline functions are not checked by the compiler for syntax errors until and unless those inline functions are explicitly called by the user in the code. Don't...
11
by: DJJ | last post by:
I am using the MySQL ODBC 3.51 driver to link three relatively small MySQL tables to a Microsoft Access 2003 database. I am finding that the data from the MySQL tables takes a hell of a long time...
5
by: pembed2003 | last post by:
Hi all, I am reading the book "C How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
6
by: leonecla | last post by:
Hi everybody, I'm facing a very very strange problem with a very very simple C program... My goal should be to write to a binary file some numbers (integers), each one represented as a sequence...
6
by: B B | last post by:
Okay, here is what's happening: I have a reasonably fast laptop (1.4 GHz Mobile M, so comparable to 2.5GHz P4) doing .net development. Running Windows XP pro, SP2 IIS is installed and running...
2
by: Charles Wilt | last post by:
I have a IBM iSeries (aka AS-400) running v5r3 of OS/400 that I access via a linked server from SQL Server 2000. The following select works fine: select * from...
2
by: the.duckman | last post by:
G'Day, I have a simple peice of code behaving in a rather eratic manner. outputStreams = new MemoryStream(); //.... some code to add values to the stream ... bool ready = false; //.... some...
3
by: Eric Davidson | last post by:
DB2 9.5 I keep geting the message. SQL0101N The statement is too long or too complex. SQLSTATE=54001 When one of my sql statements takes over 60 seconds to compile the sql statement. Is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.