<p>After I was convinced that labeled breaks/continues are a total "nono" over <a href="https://stackoverflow.com/questions/15481/java-coding-standard-best-practices-labeled-brakecontinue#15501">here</a>, I need help to remove the label out of my code.</p>

<p>I have a square matrix and a vector that has the same length. The vector has already some values in it an depending on the values in the matrix the vector is changed in the loop.</p>

<p>I hope, the code-fragment is basically understandable… </p>

<pre><code>vectorLoop:
for( int idx = 0; idx &lt; vectorLength; idx++) {
    if( conditionAtVectorPosition( v, idx ) ) continue vectorLoop;

    matrixLoop:
    for( rowIdx = 0; rowIdx &lt; n; rowIdx++ ) {
        if( anotherConditionAtVector( v, rowIdx ) ) continue matrixLoop;
        if( conditionAtMatrixRowCol( m, rowIdx, idx ) ) continue vectorLoop;
    }
    setValueInVector( v, idx );
}     
</code></pre>

<p>Please convince me, that there is a more readable/better version without the labels.</p>

[{'answer_id': 15855, 'author': 'Patrick', 'author_id': 429, 'author_profile': 'https://Stackoverflow.com/users/429', 'pm_score': 1, 'selected': False, 'text': "<p>Easily, my good man.</p>\n\n<pre><code>for( int idx = 0; idx &lt; vectorLength; idx++) {\n  if( conditionAtVectorPosition( v, idx ) ) continue;\n\n  for( rowIdx = 0; rowIdx &lt; n; rowIdx++ ) {\n    if( anotherConditionAtVector( v, rowIdx ) ) continue;\n    if( conditionAtMatrixRowCol( m, rowIdx, idx ) ) break;\n  }\n  if( !conditionAtMatrixRowCol( m, rowIdx, idx ) )\n    setValueInVector( v, idx );\n}\n</code></pre>\n\n<p>EDIT: Quite correct you are Anders. I've edited my solution to take that into account as well.</p>\n"}, {'answer_id': 15869, 'author': 'Anders Sandvig', 'author_id': 1709, 'author_profile': 'https://Stackoverflow.com/users/1709', 'pm_score': 1, 'selected': False, 'text': '<p>@Patrick you are assuming calling setValueInVector( v, idx ); at the end of the second loop is OK. If the code is to be identical, logically, it must be rewritten to somethng like this:</p>\n\n<pre>for( int idx = 0; idx </pre>\n'}, {'answer_id': 15870, 'author': 'Gishu', 'author_id': 1695, 'author_profile': 'https://Stackoverflow.com/users/1695', 'pm_score': 0, 'selected': False, 'text': '<p>Does this work for you? I extracted the inner loop into a method CheckedEntireMatrix (you can name it better than me) - Also my java is a bit rusty.. but I think it gets the message across</p>\n\n<pre><code>for( int idx = 0; idx &lt; vectorLength; idx++) {\n    if( conditionAtVectorPosition( v, idx ) \n    || !CheckedEntireMatrix(v)) continue;\n\n    setValueInVector( v, idx );\n}\n\nprivate bool CheckedEntireMatrix(Vector v)\n{\n    for( rowIdx = 0; rowIdx &lt; n; rowIdx++ ) {\n        if( anotherConditionAtVector( v, rowIdx ) ) continue;\n        if( conditionAtMatrixRowCol( m, rowIdx, idx ) ) return false;\n    }   \n    return true;\n}\n</code></pre>\n'}, {'answer_id': 15901, 'author': 'Nicolas', 'author_id': 1730, 'author_profile': 'https://Stackoverflow.com/users/1730', 'pm_score': 0, 'selected': False, 'text': '<p>Gishu has the right idea :</p>\n\n<pre><code>for( int idx = 0; idx &lt; vectorLength; idx++) {\n    if (!conditionAtVectorPosition( v, idx ) \n        &amp;&amp; checkedRow(v, idx))\n         setValueInVector( v, idx );\n}\n\nprivate boolean checkedRow(Vector v, int idx) {\n    for( rowIdx = 0; rowIdx &lt; n; rowIdx++ ) {\n        if( anotherConditionAtVector( v, rowIdx ) ) continue;\n        if( conditionAtMatrixRowCol( m, rowIdx, idx ) ) return false;\n    }  \n    return true;\n}\n</code></pre>\n'}, {'answer_id': 15903, 'author': 'fulmicoton', 'author_id': 446497, 'author_profile': 'https://Stackoverflow.com/users/446497', 'pm_score': 0, 'selected': False, 'text': "<p>I'm not too sure to understand the first continue.\nI would copy Gishu and write something like ( sorry if there are some mistakes ) :</p>\n\n<pre><code>for( int idx = 0; idx &lt; vectorLength; idx++) {\n    if( !conditionAtVectorPosition( v, idx ) &amp;&amp; CheckedEntireMatrix(v))\n        setValueInVector( v, idx );\n}\n\ninline bool CheckedEntireMatrix(Vector v) {\n    for(rowIdx = 0; rowIdx &lt; n; rowIdx++)\n        if ( !anotherConditionAtVector(v,rowIdx) &amp;&amp; conditionAtMatrixRowCol(m,rowIdx,idx) ) \n            return false;\n    return true;\n}\n</code></pre>\n"}, {'answer_id': 15908, 'author': 'jfs', 'author_id': 718, 'author_profile': 'https://Stackoverflow.com/users/718', 'pm_score': 1, 'selected': False, 'text': '<p>From reading your code. </p>\n\n<ul>\n<li>I noticed your eliminating the invalid vector positions at conditionAtVectorPosition then you remove the invalid rows at anotherConditionAtVector. </li>\n<li>It seems that checking rows at anotherConditionAtVector is redundant since whatever the value of idx is, anotherConditionAtVector only depends on the row index (assuming anotherConditionAtVector has no side effects). </li>\n</ul>\n\n<p>So you can do this:</p>\n\n<ul>\n<li>Get the valid positions first using conditionAtVectorPosition (these are the valid columns).</li>\n<li>Then get the valid rows using anotherConditionAtVector.</li>\n<li>Finally, use conditionAtMatrixRowCol using the valid columns and rows.</li>\n</ul>\n\n<p>I hope this helps.</p>\n'}, {'answer_id': 15946, 'author': 'Marcus Downing', 'author_id': 1000, 'author_profile': 'https://Stackoverflow.com/users/1000', 'pm_score': 6, 'selected': True, 'text': "<p>Looking at the solutions presented so far:</p>\n\n<ul>\n<li><p>They all look less readable than the original, in that they involve spending more code on the mechanism of the code rather than on the algorithm itself</p></li>\n<li><p>Some of them are broken, or were before they were edited. Most damning is the fact that people are having to think quite hard about how to write the code without labels and not break anything.</p></li>\n<li><p>Some come with a performance penalty of running the same test twice, which may not always be trivial. The alternative to that is storing and passing round booleans, which gets ugly.</p></li>\n<li><p>Refactoring the relevant part of the code into a method is effectively a no-op: it rearranges how the code is laid out in the file, but has no effect on how it's executed.</p></li>\n</ul>\n\n<p>All of which makes me believe that, at least in the case of this question as phrased, the label is the correct solution and doesn't need to be refactored away. Certainly there are cases where labels are used incorrectly and should be refactored away. I just don't think it should be treated as some unbreakable rule.</p>\n"}, {'answer_id': 15981, 'author': 'Nicolas', 'author_id': 1730, 'author_profile': 'https://Stackoverflow.com/users/1730', 'pm_score': 1, 'selected': False, 'text': '<p>@<a href="https://stackoverflow.com/questions/15851/refactoring-away-labeled-loops#15946">Sadie</a>:</p>\n\n<blockquote>\n  <p>They all look less readable than the original, in that they involve spending more code on the mechanism of the code rather than on the algorithm itself</p>\n</blockquote>\n\n<p>Externalizing the second loop outside the algorithm is not necessarily less readable. If the method name is well chosen, it can improve readability.</p>\n\n<blockquote>\n  <p>Some of them are broken, or were before they were edited. Most damning is the fact that people are having to think quite hard about how to write the code without labels and not break anything.</p>\n</blockquote>\n\n<p>I have a different point of view: some of them are broken because it is hard to figure out the behavior of the original algorithm.</p>\n\n<blockquote>\n  <p>Some come with a performance penalty of running the same test twice, which may not always be trivial. The alternative to that is storing and passing round booleans, which gets ugly.</p>\n</blockquote>\n\n<p>The performance penalty is minor. However I agree that running a test twice is not a nice solution.</p>\n\n<blockquote>\n  <p>Refactoring the relevant part of the code into a method is effectively a no-op: it rearranges how the code is laid out in the file, but has no effect on how it\'s executed.</p>\n</blockquote>\n\n<p>I don\'t see the point. Yep, it doesn\'t change the behavior, like... refactoring?</p>\n\n<blockquote>\n  <p>Certainly there are cases where labels are used incorrectly and should be refactored away. I just don\'t think it should be treated as some unbreakable rule.</p>\n</blockquote>\n\n<p>I totally agree. But as you have pointed out, some of us have difficulties while refactoring this example. Even if the initial example is readable, it is hard to maintain.</p>\n'}, {'answer_id': 16014, 'author': 'Marcus Downing', 'author_id': 1000, 'author_profile': 'https://Stackoverflow.com/users/1000', 'pm_score': 1, 'selected': False, 'text': '<p>@<a href="https://stackoverflow.com/questions/15851?sort=newest#15981">Nicolas</a></p>\n\n<blockquote>\n  <blockquote>\n    <p>Some of them are broken, or were before they were edited. Most damning is the fact that \n    people are having to think quite hard about how to write the code without labels and not \n    break anything.</p>\n  </blockquote>\n  \n  <p>I have a different point of view: some of them are broken because it is hard to figure out \n  the behavior of the original algorithm.</p>\n</blockquote>\n\n<p>I realise that it\'s subjective, but I don\'t have any trouble reading the original algorithm. It\'s shorter and clearer than the proposed replacements.</p>\n\n<p>What all the refactorings in this thread do is emulate the behaviour of a label using other language features - as if you were porting the code to a language that didn\'t have labels.</p>\n'}, {'answer_id': 16048, 'author': 'Anders Sandvig', 'author_id': 1709, 'author_profile': 'https://Stackoverflow.com/users/1709', 'pm_score': 1, 'selected': False, 'text': '<blockquote><blockquote>Some come with a performance penalty of running the same test twice, which may not always be trivial. The alternative to that is storing and passing round booleans, which gets ugly.</blockquote>\n\nThe performance penalty is minor. However I agree that running a test twice is not a nice solution.</blockquote>\n\n<p>I believe the question was how to remove the labels, not how to optimize the algorithm. It appeared to me that the original poster was unaware of how to use \'continue\' and \'break\' keywords without labels, but of course, my assumptions may be wrong. </p>\n\n<p>When it comes to performance, the post does not give any information about the implementation of the other functions, so for all I know they might as well be downloading the results via FTP as consisting of simple calculations inlined by the compiler.</p>\n\n<p>That being said, doing the same test twice is not optimal&mdash;in theory.</p>\n\n<p>EDIT: On a second thought, the example is actually not a horrible use of labels. I agree that <a href="http://en.wikipedia.org/wiki/Goto#Criticism_of_goto_usage" rel="nofollow noreferrer">"goto is a no-no"</a>, but not because of code like this. The use of labels here does not actually affect the readability of the code in a significant way. Of course, they are not required and can easily be omitted, but not using them simply because "using labels is bad" is not a good argument in this case. After all, removing the labels does not make the code much easier to read, as others have already commented.</p>\n'}, {'answer_id': 16512, 'author': 'Mo.', 'author_id': 1870, 'author_profile': 'https://Stackoverflow.com/users/1870', 'pm_score': 1, 'selected': False, 'text': '<p>This question was not about optimizing the algorithm - but thanks anyway ;-) </p>\n\n<p>At the time I wrote it, I considered the labeled continue as a readable solution.</p>\n\n<p>I asked SO a <a href="https://stackoverflow.com/questions/15481/java-coding-standard-best-practices-labeled-brakecontinue#15501">question</a> about the convention (having the label in all caps or not) for labels in Java.</p>\n\n<p>Basically every answer told me "do not use them - there is always a better way! refactor!". So I posted this question to ask for a more readable (and therefore better?) solution.</p>\n\n<p>Until now, I am not completely convinced by the alternatives presented so far.</p>\n\n<p>Please don\'t get me wrong. Labels are evil most of the time. </p>\n\n<p>But in my case, the conditional tests are pretty simple and the algorithm is taken from a mathematical paper and therefore very likely to not change in the near future. So I prefer having all the relevant parts visible at once instead of having to scroll to another method named something like checkMatrixAtRow(x).</p>\n\n<p>Especially at more complex mathematical algorithms, I find it pretty hard to find "good" function-names - but I guess that is yet another question</p>\n'}, {'answer_id': 17408, 'author': 'RodeoClown', 'author_id': 943, 'author_profile': 'https://Stackoverflow.com/users/943', 'pm_score': 1, 'selected': False, 'text': "<p>I think that labelled loops are so uncommon that you can pick whatever method of labelling works for you - what you have there makes your intentions with the continues perfectly clear.</p>\n\n<hr>\n\n<p><em>After leading the charge to suggest refactoring the loops in the original question and now seeing the code in question, I think you've got a very readable loop there.</em></p>\n\n<p><em>What I had imagined was a very different chunk of code - putting the actual example up, I can see it is much cleaner than I had thought.</em></p>\n\n<p><em>My apologies for the misunderstanding.</em></p>\n"}]
['https://Stackoverflow.com/questions/15851', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1870/']