Skip to main content
deleted 67 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

description

I am trying to find out the best practice for writing a loop which does one check and at the same time has to increment a value so that the whilewhile loop will eventually fail. Performance is important that's why I thought of using the least amount of variables.

use case

Use case

In a decently low level method I do a binarySearch(array, key) which guarantees to find a key value in a sorted array if the key can be found. However it does not guarantees to find the first or last key first. But since I need the range of indexes in which the key excists (for other code later on) I try to find the last and first element in the array that matches. With this code I am trying to let lastSucceedingIndex hold the last index which holds the key:

while (lastSucceedingIndex < tailIndex && (array[++lastSucceedingIndex] == key)) {
}
lastSucceedingIndex--;

There is one situation in which a bug exists, namely when the lastSucceedingIndex is equal to the tailIndex: the while loop will fail and the ++lastSucceedingIndex will not happen. The lastSucceedingIndex-- is there in the end to fix the ++lastSucceedingIndex when it's not equal to the key, but since this does not happen in the last case it will decrement lastSucceedingIndex incorrectly. I wrote this loop for performance and I take this little bug for granted, it happens so rarely that it will effect less than 0.001% of all cases. This way it seems like I only need one counter and one variable, the loop could be executed as much as 17000 times before it finds its lastSucceedingIndex and the array is very large with possibly up to a million entries.

Alternatives for performance and readability?

Are there any alternatives for performance and readability?

I analysedanalysed my code by the build in code analysis of my IDE and I get the remark

while statement has empty body at line

Which seems to indicate that having a loop with no lines in them is considered a possible bug by the compiler or at least bad practice. Now Now my question is, is having an empty loop considered bad practice? I could move the lastSucceedingIndex to the body and make it bug-free but that would require one extra addition like so:

while (lastSucceedingIndex < tailIndex && array[lastSucceedingIndex + 1] == key) {
    lastSucceedingIndex++;
}

1 less bug for millions upon millions of extra assembly instructions seems a hard trade-off to make.

description

I am trying to find out the best practice for writing a loop which does one check and at the same time has to increment a value so that the while loop will eventually fail. Performance is important that's why I thought of using the least amount of variables.

use case

In a decently low level method I do a binarySearch(array, key) which guarantees to find a key value in a sorted array if the key can be found. However it does not guarantees to find the first or last key first. But since I need the range of indexes in which the key excists (for other code later on) I try to find the last and first element in the array that matches. With this code I am trying to let lastSucceedingIndex hold the last index which holds the key:

while (lastSucceedingIndex < tailIndex && (array[++lastSucceedingIndex] == key)) {
}
lastSucceedingIndex--;

There is one situation in which a bug exists, namely when the lastSucceedingIndex is equal to the tailIndex: the while loop will fail and the ++lastSucceedingIndex will not happen. The lastSucceedingIndex-- is there in the end to fix the ++lastSucceedingIndex when it's not equal to the key, but since this does not happen in the last case it will decrement lastSucceedingIndex incorrectly. I wrote this loop for performance and I take this little bug for granted, it happens so rarely that it will effect less than 0.001% of all cases. This way it seems like I only need one counter and one variable, the loop could be executed as much as 17000 times before it finds its lastSucceedingIndex and the array is very large with possibly up to a million entries.

Alternatives for performance and readability?

I analysed my code by the build in code analysis of my IDE and I get the remark

while statement has empty body at line

Which seems to indicate that having a loop with no lines in them is considered a possible bug by the compiler or at least bad practice. Now my question is, is having an empty loop considered bad practice? I could move the lastSucceedingIndex to the body and make it bug-free but that would require one extra addition like so:

while (lastSucceedingIndex < tailIndex && array[lastSucceedingIndex + 1] == key) {
    lastSucceedingIndex++;
}

1 less bug for millions upon millions of extra assembly instructions seems a hard trade-off to make.

I am trying to find out the best practice for writing a loop which does one check and at the same time has to increment a value so that the while loop will eventually fail. Performance is important that's why I thought of using the least amount of variables.

Use case

In a decently low level method I do a binarySearch(array, key) which guarantees to find a key value in a sorted array if the key can be found. However it does not guarantees to find the first or last key first. But since I need the range of indexes in which the key excists (for other code later on) I try to find the last and first element in the array that matches. With this code I am trying to let lastSucceedingIndex hold the last index which holds the key:

while (lastSucceedingIndex < tailIndex && (array[++lastSucceedingIndex] == key)) {
}
lastSucceedingIndex--;

There is one situation in which a bug exists, namely when the lastSucceedingIndex is equal to the tailIndex: the while loop will fail and the ++lastSucceedingIndex will not happen. The lastSucceedingIndex-- is there in the end to fix the ++lastSucceedingIndex when it's not equal to the key, but since this does not happen in the last case it will decrement lastSucceedingIndex incorrectly. I wrote this loop for performance and I take this little bug for granted, it happens so rarely that it will effect less than 0.001% of all cases. This way it seems like I only need one counter and one variable, the loop could be executed as much as 17000 times before it finds its lastSucceedingIndex and the array is very large with possibly up to a million entries.

Are there any alternatives for performance and readability?

I analysed my code by the build in code analysis of my IDE and I get the remark

while statement has empty body at line

Which seems to indicate that having a loop with no lines in them is considered a possible bug by the compiler or at least bad practice. Now my question is, is having an empty loop considered bad practice? I could move the lastSucceedingIndex to the body and make it bug-free but that would require one extra addition like so:

while (lastSucceedingIndex < tailIndex && array[lastSucceedingIndex + 1] == key) {
    lastSucceedingIndex++;
}

1 less bug for millions upon millions of extra assembly instructions seems a hard trade-off to make.

Post Reopened by user34073, 200_success
edited tags; edited title
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Is Epilogue to a while-loop with empty body considered good practice, what would be an alternative for this?binary search to find the range of matching indexes

added 1124 characters in body; edited title
Source Link
Joop
  • 227
  • 2
  • 6

While loop with header changing the variables Is a while-loop with empty body or are other ways preferred overconsidered good practice, what would be an alternative for this?

description

The reason I am asking this is because I am doing some analyses on my code bytrying to find out the build in code analysis of my IDEbest practice for writing a loop which does one check and at the same time has to increment a value so that the while loop will eventually fail. Performance is important that's why I getthought of using the remarkleast amount of variables.

while statement has empty body at line

use case

Which seemsIn a decently low level method I do a binarySearch(array, key) which guarantees to indicate that havingfind a loop with no lineskey value in them is not a preferred waysorted array if the key can be found. However it does not guarantees to do somethingfind the first or last key first. But since I got this code,need the range of indexes in which what I do a conditional check which also changes mythe key excists (for other code, later on) I try to find the last and it worksfirst element in the array that matches. With this code I am trying to let lastSucceedingIndex hold the last index which holds the key:

    while (lastSucceedingIndex < tailIndex && (array[++lastSucceedingIndex] == key)) {
    }
    lastSucceedingIndex--;

Information about what the loop does and why I decided to use it like this: I am using shortcircuit There is one situation in which a bug exists, namely when the lastSucceedingIndex is equal to quit the loop iftailIndex: the first part ofwhile loop will fail and the conditional check does++lastSucceedingIndex will not evaluate to truehappen. The second part increases first the lastSucceedingIndex-- and then checks if it matches someis there in the end to fix the ++lastSucceedingIndex when it's not equal to the key, more often than notbut since this isdoes not happen in the last case. Since I increase it will decrement lastSucceedingIndexwithin the header incorrectly. I leave the whilewrote this loop empty. To later correct for the off by oneperformance and I take this little bug for granted, it happens so rarely that it will effect less than 0.001% of all cases. This way it seems like I add the last line afteronly need one counter and one variable, the while loop could be executed as much as 17000 times before it finds its lastSucceedingIndex and the array is very large with possibly up to a million entries.

Alternatives for performance and readability?

NowI analysed my code by the build in code analysis of my IDE and I get the remark

while statement has empty body at line

Which seems to indicate that having a loop with no lines in them is considered a possible bug by the compiler or at least bad practice. Now my question is, is having an empty loop goodconsidered bad practice? Or should I could move the lastSucceedingIndexlastSucceedingIndex to the body and incrementmake it there? Or is there evenbug-free but that would require one extra addition like so:

while (lastSucceedingIndex < tailIndex && array[lastSucceedingIndex + 1] == key) {
    lastSucceedingIndex++;
}

1 less bug for millions upon millions of extra assembly instructions seems a third option I am missing?hard trade-off to make.

While loop with header changing the variables with empty body or are other ways preferred over this?

The reason I am asking this is because I am doing some analyses on my code by the build in code analysis of my IDE and I get the remark

while statement has empty body at line

Which seems to indicate that having a loop with no lines in them is not a preferred way to do something. I got this code, in which what I do a conditional check which also changes my code, and it works:

    while (lastSucceedingIndex < tailIndex && (array[++lastSucceedingIndex] == key)) {
    }
    lastSucceedingIndex--;

Information about what the loop does and why I decided to use it like this: I am using shortcircuit to quit the loop if the first part of the conditional check does not evaluate to true. The second part increases first the lastSucceedingIndex and then checks if it matches some key, more often than not this is the case. Since I increase lastSucceedingIndexwithin the header I leave the while loop empty. To later correct for the off by one bug I add the last line after the while loop.

Now my question is, is having an empty loop good practice? Or should I move the lastSucceedingIndex to the body and increment it there? Or is there even a third option I am missing?

Is a while-loop with empty body considered good practice, what would be an alternative for this?

description

I am trying to find out the best practice for writing a loop which does one check and at the same time has to increment a value so that the while loop will eventually fail. Performance is important that's why I thought of using the least amount of variables.

use case

In a decently low level method I do a binarySearch(array, key) which guarantees to find a key value in a sorted array if the key can be found. However it does not guarantees to find the first or last key first. But since I need the range of indexes in which the key excists (for other code later on) I try to find the last and first element in the array that matches. With this code I am trying to let lastSucceedingIndex hold the last index which holds the key:

while (lastSucceedingIndex < tailIndex && (array[++lastSucceedingIndex] == key)) {
}
lastSucceedingIndex--;

There is one situation in which a bug exists, namely when the lastSucceedingIndex is equal to the tailIndex: the while loop will fail and the ++lastSucceedingIndex will not happen. The lastSucceedingIndex-- is there in the end to fix the ++lastSucceedingIndex when it's not equal to the key, but since this does not happen in the last case it will decrement lastSucceedingIndex incorrectly. I wrote this loop for performance and I take this little bug for granted, it happens so rarely that it will effect less than 0.001% of all cases. This way it seems like I only need one counter and one variable, the loop could be executed as much as 17000 times before it finds its lastSucceedingIndex and the array is very large with possibly up to a million entries.

Alternatives for performance and readability?

I analysed my code by the build in code analysis of my IDE and I get the remark

while statement has empty body at line

Which seems to indicate that having a loop with no lines in them is considered a possible bug by the compiler or at least bad practice. Now my question is, is having an empty loop considered bad practice? I could move the lastSucceedingIndex to the body and make it bug-free but that would require one extra addition like so:

while (lastSucceedingIndex < tailIndex && array[lastSucceedingIndex + 1] == key) {
    lastSucceedingIndex++;
}

1 less bug for millions upon millions of extra assembly instructions seems a hard trade-off to make.

Post Closed as "Needs details or clarity" by 200_success
Post Reopened by RubberDuck, 200_success
added 105 characters in body; edited title
Source Link
Joop
  • 227
  • 2
  • 6
Loading
Post Closed as "Needs details or clarity" by 200_success
Source Link
Joop
  • 227
  • 2
  • 6
Loading