Skip to main content
deleted 1 characters in body
Source Link
9000
  • 24.4k
  • 4
  • 53
  • 80

Nested loops are fine as long as they describe the correct algorithm.

Nested loops have performance considerations (see @Travis-Presetto'sPesetto's answer), but sometimes it's exactly the correct algorithm, e.g. when you need to access every value in a matrix.

Labeling loops in Java allows to prematurely break out of several nested loops when other ways to do this would be cumbersome. E.g. some game might have a piece of code like this:

Player chosen_one = null; ... outer: // this is a label for (Player player : party.getPlayers()) { for (Cell cell : player.getVisibleMapCells()) { for (Item artefact : cell.getItemsOnTheFloor()) if (artefact == HOLY_GRAIL) { chosen_one = player; break outer; // everyone stop looking, we found it } } } 

While code like the example above may sometimes be the optimal way to express a certain algorithm, it is usually better to break this code into smaller functions, and probably use return instead of break. So a break with a label is a faint code smell; pay extra attention when you see it.

Nested loops are fine as long as they describe the correct algorithm.

Nested loops have performance considerations (see @Travis-Presetto's answer), but sometimes it's exactly the correct algorithm, e.g. when you need to access every value in a matrix.

Labeling loops in Java allows to prematurely break out of several nested loops when other ways to do this would be cumbersome. E.g. some game might have a piece of code like this:

Player chosen_one = null; ... outer: // this is a label for (Player player : party.getPlayers()) { for (Cell cell : player.getVisibleMapCells()) { for (Item artefact : cell.getItemsOnTheFloor()) if (artefact == HOLY_GRAIL) { chosen_one = player; break outer; // everyone stop looking, we found it } } } 

While code like the example above may sometimes be the optimal way to express a certain algorithm, it is usually better to break this code into smaller functions, and probably use return instead of break. So a break with a label is a faint code smell; pay extra attention when you see it.

Nested loops are fine as long as they describe the correct algorithm.

Nested loops have performance considerations (see @Travis-Pesetto's answer), but sometimes it's exactly the correct algorithm, e.g. when you need to access every value in a matrix.

Labeling loops in Java allows to prematurely break out of several nested loops when other ways to do this would be cumbersome. E.g. some game might have a piece of code like this:

Player chosen_one = null; ... outer: // this is a label for (Player player : party.getPlayers()) { for (Cell cell : player.getVisibleMapCells()) { for (Item artefact : cell.getItemsOnTheFloor()) if (artefact == HOLY_GRAIL) { chosen_one = player; break outer; // everyone stop looking, we found it } } } 

While code like the example above may sometimes be the optimal way to express a certain algorithm, it is usually better to break this code into smaller functions, and probably use return instead of break. So a break with a label is a faint code smell; pay extra attention when you see it.

added hauling a value from out of the loops.
Source Link
9000
  • 24.4k
  • 4
  • 53
  • 80

Nested loops are fine as long as they describe the correct algorithm.

Nested loops have performance considerations (see @Travis-Presetto's answer), but sometimes it's exactly the correct algorithm, e.g. when you need to access every value in a matrix.

Labeling loops in Java allows to prematurely break out of several nested loops when other ways to do this would be cumbersome. E.g. some game might have a piece of code like this:

Player chosen_one = null; ... outer: // this is a label for (Player player : party.getPlayers()) { for (Cell cell : player.getVisibleMapCells()) { for (Item artefact : cell.getItemsOnTheFloor()) if (artefact == HOLY_GRAIL) { chosen_one = player; break outer; // everyone stop looking, we found it } } } 

While code like the example above may sometimes be the optimal way to express a certain algorithm, it is usually better to break this code into smaller functions, and probably use return instead of break. So a break with a label is a faint code smell; pay extra attention when you see it.

Nested loops are fine as long as they describe the correct algorithm.

Nested loops have performance considerations (see @Travis-Presetto's answer), but sometimes it's exactly the correct algorithm, e.g. when you need to access every value in a matrix.

Labeling loops in Java allows to prematurely break out of several nested loops when other ways to do this would be cumbersome. E.g. some game might have a piece of code like this:

outer: // this is a label for (Player player : party.getPlayers()) { for (Cell cell : player.getVisibleMapCells()) { for (Item artefact : cell.getItemsOnTheFloor()) if (artefact == HOLY_GRAIL) { break outer; // everyone stop looking, we found it } } } 

While code like the example above may sometimes be the optimal way to express a certain algorithm, it is usually better to break this code into smaller functions, and probably use return instead of break. So a break with a label is a faint code smell; pay extra attention when you see it.

Nested loops are fine as long as they describe the correct algorithm.

Nested loops have performance considerations (see @Travis-Presetto's answer), but sometimes it's exactly the correct algorithm, e.g. when you need to access every value in a matrix.

Labeling loops in Java allows to prematurely break out of several nested loops when other ways to do this would be cumbersome. E.g. some game might have a piece of code like this:

Player chosen_one = null; ... outer: // this is a label for (Player player : party.getPlayers()) { for (Cell cell : player.getVisibleMapCells()) { for (Item artefact : cell.getItemsOnTheFloor()) if (artefact == HOLY_GRAIL) { chosen_one = player; break outer; // everyone stop looking, we found it } } } 

While code like the example above may sometimes be the optimal way to express a certain algorithm, it is usually better to break this code into smaller functions, and probably use return instead of break. So a break with a label is a faint code smell; pay extra attention when you see it.

Source Link
9000
  • 24.4k
  • 4
  • 53
  • 80

Nested loops are fine as long as they describe the correct algorithm.

Nested loops have performance considerations (see @Travis-Presetto's answer), but sometimes it's exactly the correct algorithm, e.g. when you need to access every value in a matrix.

Labeling loops in Java allows to prematurely break out of several nested loops when other ways to do this would be cumbersome. E.g. some game might have a piece of code like this:

outer: // this is a label for (Player player : party.getPlayers()) { for (Cell cell : player.getVisibleMapCells()) { for (Item artefact : cell.getItemsOnTheFloor()) if (artefact == HOLY_GRAIL) { break outer; // everyone stop looking, we found it } } } 

While code like the example above may sometimes be the optimal way to express a certain algorithm, it is usually better to break this code into smaller functions, and probably use return instead of break. So a break with a label is a faint code smell; pay extra attention when you see it.