Skip to main content
added 1670 characters in body
Source Link
Arseni Mourzenko
  • 135.8k
  • 31
  • 350
  • 522

Different blocks have different meanings: by using the ones which are commonly used for a specific purpose, you make your code easier to read and maintain. By using the ones which are not commonly used for the purpose, you force readers of your code to stop and think why you've used this construct instead of the commonly used oneyou force readers of your code to stop and think why you've used this construct instead of the commonly used one.

While semantically correct, for (x = 0; !done; x++) is not something usual. while or do...while are used in situations where you need to break from a loop when a condition is met:

x = 0;
do {
    ... something involving 'x', that might end early.
    x++;
}
while (x <= max)

(assuming that max can change within the loop; otherwise, you can simply do for (x = 0; x >=<= max; x++))

Being concise is important too. Less code means less risk of errors. For example, your while-based code is unnecessarily complicated. For example, (!(x < max)) takes time to understand and may be source of an error. The if block within the while adds complexity too. You also add a variable which transcends the while block and which can be simply removed.


I prefer the first one, mainly because I dislike breaking out of loops

That is your personal opinion which is valuable when you're working on a personal project which will never be shared with anybody. As soon as you think that the code may be open sourced (and actually read and used by others) or your work in a team, what you prefer may go against:

  • Your team habits. If in a team of 6 persons, 5 are using a construct and you are using a different construct, for the sake of consistency, do use the one your teammates use, including if yours looks clearly superior (unless of course you can convince your teammates at the beginning of the project that your's is better).

  • Community usages. Different communities use different styles, which leads to discrepancies between languages. For example:

     if (something) {
     }
    

    in JavaScript should be written:

     if (something)
     {
     }
    

    in C#. Sticking to community usages makes it easier for the people outside your project to read your code (for example if you post a piece of code on Stack Overflow) or for you to read code from other projects.

  • Official style and style checkers. You may disagree with the choice the authors of the style guide made, but the fact is, consistency is more important.

    For example, I find a few rules of PSR-2 quite weird. For example:

     function hello()
     {                      // Newline here.
         if (something) {   // No newline here.
         }
     }
    

    Still, I'll rather use this style than create my own and find myself inconsistent with thousands of PHP programmers.

Different blocks have different meanings: by using the ones which are commonly used for a specific purpose, you make your code easier to read and maintain. By using the ones which are not commonly used for the purpose, you force readers of your code to stop and think why you've used this construct instead of the commonly used one.

While semantically correct, for (x = 0; !done; x++) is not something usual. while or do...while are used in situations where you need to break from a loop when a condition is met:

x = 0;
do {
    ... something involving 'x', that might end early.
    x++;
}
while (x <= max)

(assuming that max can change within the loop; otherwise, you can simply do for (x = 0; x >= max; x++))

I prefer the first one, mainly because I dislike breaking out of loops

That is your personal opinion which is valuable when you're working on a personal project which will never be shared with anybody. As soon as you think that the code may be open sourced (and actually read and used by others) or your work in a team, what you prefer may go against:

  • Your team habits. If in a team of 6 persons, 5 are using a construct and you are using a different construct, for the sake of consistency, do use the one your teammates use, including if yours looks clearly superior (unless of course you can convince your teammates at the beginning of the project that your's is better).

  • Community usages. Different communities use different styles, which leads to discrepancies between languages. For example:

     if (something) {
     }
    

    in JavaScript should be written:

     if (something)
     {
     }
    

    in C#. Sticking to community usages makes it easier for the people outside your project to read your code (for example if you post a piece of code on Stack Overflow) or for you to read code from other projects.

  • Official style and style checkers. You may disagree with the choice the authors of the style guide made, but the fact is, consistency is more important.

    For example, I find a few rules of PSR-2 quite weird. For example:

     function hello()
     {                      // Newline here.
         if (something) {   // No newline here.
         }
     }
    

    Still, I'll rather use this style than create my own and find myself inconsistent with thousands of PHP programmers.

Different blocks have different meanings: by using the ones which are commonly used for a specific purpose, you make your code easier to read and maintain. By using the ones which are not commonly used for the purpose, you force readers of your code to stop and think why you've used this construct instead of the commonly used one.

While semantically correct, for (x = 0; !done; x++) is not something usual. while or do...while are used in situations where you need to break from a loop when a condition is met:

x = 0;
do {
    ... something involving 'x', that might end early.
    x++;
}
while (x <= max)

(assuming that max can change within the loop; otherwise, you can simply do for (x = 0; x <= max; x++))

Being concise is important too. Less code means less risk of errors. For example, your while-based code is unnecessarily complicated. For example, (!(x < max)) takes time to understand and may be source of an error. The if block within the while adds complexity too. You also add a variable which transcends the while block and which can be simply removed.


I prefer the first one, mainly because I dislike breaking out of loops

That is your personal opinion which is valuable when you're working on a personal project which will never be shared with anybody. As soon as you think that the code may be open sourced (and actually read and used by others) or your work in a team, what you prefer may go against:

  • Your team habits. If in a team of 6 persons, 5 are using a construct and you are using a different construct, for the sake of consistency, do use the one your teammates use, including if yours looks clearly superior (unless of course you can convince your teammates at the beginning of the project that your's is better).

  • Community usages. Different communities use different styles, which leads to discrepancies between languages. For example:

     if (something) {
     }
    

    in JavaScript should be written:

     if (something)
     {
     }
    

    in C#. Sticking to community usages makes it easier for the people outside your project to read your code (for example if you post a piece of code on Stack Overflow) or for you to read code from other projects.

  • Official style and style checkers. You may disagree with the choice the authors of the style guide made, but the fact is, consistency is more important.

    For example, I find a few rules of PSR-2 quite weird. For example:

     function hello()
     {                      // Newline here.
         if (something) {   // No newline here.
         }
     }
    

    Still, I'll rather use this style than create my own and find myself inconsistent with thousands of PHP programmers.

added 1670 characters in body
Source Link
Arseni Mourzenko
  • 135.8k
  • 31
  • 350
  • 522

Different blocks have different meanings: by using the ones which are commonly used for a specific purpose, you make your code easier to read and maintain. By using the ones which are not commonly used for the purpose, you force readers of your code to stop and think why you've used this construct instead of the commonly used one.

While semantically correct, for (x = 0; !done; x++) is not something usual. while or do...while are used in situations where you need to break from a loop when a condition is met:

x = 0;
do {
    ... something involving 'x', that might end early.
    x++;
}
while (x >=<= max)

(assuming that max can change within the loop; otherwise, you can simply do for (x = 0; x >= max; x++))

I prefer the first one, mainly because I dislike breaking out of loops

That is your personal opinion which is valuable when you're working on a personal project which will never be shared with anybody. As soon as you think that the code may be open sourced (and actually read and used by others) or your work in a team, what you prefer may go against:

  • Your team habits. If in a team of 6 persons, 5 are using a construct and you are using a different construct, for the sake of consistency, do use the one your teammates use, including if yours looks clearly superior (unless of course you can convince your teammates at the beginning of the project that your's is better).

  • Community usages. Different communities use different styles, which leads to discrepancies between languages. For example:

     if (something) {
     }
    

    in JavaScript should be written:

     if (something)
     {
     }
    

    in C#. Sticking to community usages makes it easier for the people outside your project to read your code (for example if you post a piece of code on Stack Overflow) or for you to read code from other projects.

  • Official style and style checkers. You may disagree with the choice the authors of the style guide made, but the fact is, consistency is more important.

    For example, I find a few rules of PSR-2 quite weird. For example:

     function hello()
     {                      // Newline here.
         if (something) {   // No newline here.
         }
     }
    

    Still, I'll rather use this style than create my own and find myself inconsistent with thousands of PHP programmers.

Different blocks have different meanings: by using the ones which are commonly used for a specific purpose, you make your code easier to read and maintain. By using the ones which are not commonly used for the purpose, you force readers of your code to stop and think why you've used this construct instead of the commonly used one.

While semantically correct, for (x = 0; !done; x++) is not something usual. while or do...while are used in situations where you need to break from a loop when a condition is met:

x = 0;
do {
    ... something involving 'x', that might end early.
    x++;
}
while (x >= max)

(assuming that max can change within the loop; otherwise, you can simply do for (x = 0; x >= max; x++))

Different blocks have different meanings: by using the ones which are commonly used for a specific purpose, you make your code easier to read and maintain. By using the ones which are not commonly used for the purpose, you force readers of your code to stop and think why you've used this construct instead of the commonly used one.

While semantically correct, for (x = 0; !done; x++) is not something usual. while or do...while are used in situations where you need to break from a loop when a condition is met:

x = 0;
do {
    ... something involving 'x', that might end early.
    x++;
}
while (x <= max)

(assuming that max can change within the loop; otherwise, you can simply do for (x = 0; x >= max; x++))

I prefer the first one, mainly because I dislike breaking out of loops

That is your personal opinion which is valuable when you're working on a personal project which will never be shared with anybody. As soon as you think that the code may be open sourced (and actually read and used by others) or your work in a team, what you prefer may go against:

  • Your team habits. If in a team of 6 persons, 5 are using a construct and you are using a different construct, for the sake of consistency, do use the one your teammates use, including if yours looks clearly superior (unless of course you can convince your teammates at the beginning of the project that your's is better).

  • Community usages. Different communities use different styles, which leads to discrepancies between languages. For example:

     if (something) {
     }
    

    in JavaScript should be written:

     if (something)
     {
     }
    

    in C#. Sticking to community usages makes it easier for the people outside your project to read your code (for example if you post a piece of code on Stack Overflow) or for you to read code from other projects.

  • Official style and style checkers. You may disagree with the choice the authors of the style guide made, but the fact is, consistency is more important.

    For example, I find a few rules of PSR-2 quite weird. For example:

     function hello()
     {                      // Newline here.
         if (something) {   // No newline here.
         }
     }
    

    Still, I'll rather use this style than create my own and find myself inconsistent with thousands of PHP programmers.

Source Link
Arseni Mourzenko
  • 135.8k
  • 31
  • 350
  • 522

Different blocks have different meanings: by using the ones which are commonly used for a specific purpose, you make your code easier to read and maintain. By using the ones which are not commonly used for the purpose, you force readers of your code to stop and think why you've used this construct instead of the commonly used one.

While semantically correct, for (x = 0; !done; x++) is not something usual. while or do...while are used in situations where you need to break from a loop when a condition is met:

x = 0;
do {
    ... something involving 'x', that might end early.
    x++;
}
while (x >= max)

(assuming that max can change within the loop; otherwise, you can simply do for (x = 0; x >= max; x++))