-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c70640
commit 0b84a16
Showing
93 changed files
with
1,509 additions
and
659 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,50 @@ | ||
<h2>Why is this an issue?</h2> | ||
<p>Merging collapsible <code>if</code> statements increases the code’s readability.</p> | ||
<h3>Noncompliant code example</h3> | ||
<p>Nested code - blocks of code inside blocks of code - is eventually necessary, but increases complexity. This is why keeping the code as flat as | ||
possible, by avoiding unnecessary nesting, is considered a good practice.</p> | ||
<p>Merging <code>if</code> statements when possible will decrease the nesting of the code and improve its readability.</p> | ||
<p>Code like</p> | ||
<pre> | ||
if (condition1) | ||
{ | ||
if (condition2) | ||
if (condition2) // Noncompliant | ||
{ | ||
// ... | ||
} | ||
} | ||
</pre> | ||
<h3>Compliant solution</h3> | ||
<p>Will be more readable as</p> | ||
<pre> | ||
if (condition1 && condition2) | ||
if (condition1 && condition2) // Compliant | ||
{ | ||
// ... | ||
} | ||
</pre> | ||
<h2>How to fix it</h2> | ||
<p>If merging the conditions seems to result in a more complex code, extracting the condition or part of it in a named function or variable is a | ||
better approach to fix readability.</p> | ||
<h3>Code examples</h3> | ||
<h4>Noncompliant code example</h4> | ||
<pre> | ||
if (file != null) | ||
{ | ||
if (file.isFile() || file.isDirectory()) // Noncompliant | ||
{ | ||
/* ... */ | ||
} | ||
} | ||
</pre> | ||
<h4>Compliant solution</h4> | ||
<pre> | ||
bool isFileOrDirectory(File file) | ||
{ | ||
return file.isFile() || file.isDirectory(); | ||
} | ||
|
||
/* ... */ | ||
|
||
if (file != null && isFileOrDirectory(file)) // Compliant | ||
{ | ||
/* ... */ | ||
} | ||
</pre> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,43 @@ | ||
<p>A magic number is a hard-coded numerical value that may lack context or meaning. They should not be used because they can make the code less | ||
readable and maintainable.</p> | ||
<h2>Why is this an issue?</h2> | ||
<p>A magic number is a number that comes out of nowhere, and is directly used in a statement. Magic numbers are often used, for instance to limit the | ||
number of iterations of a loop, to test the value of a property, etc.</p> | ||
<p>Using magic numbers may seem obvious and straightforward when you’re writing a piece of code, but they are much less obvious and straightforward at | ||
debugging time.</p> | ||
<p>That is why magic numbers must be demystified by first being assigned to clearly named variables before being used.</p> | ||
<p>-1, 0 and 1 are not considered magic numbers.</p> | ||
<h3>Noncompliant code example</h3> | ||
<pre> | ||
public static void DoSomething() | ||
<p>Magic numbers make the code more complex to understand as it requires the reader to have knowledge about the global context to understand the | ||
number itself. Their usage may seem obvious when writing the code, but it may not be the case for another developer or later once the context faded | ||
away. -1, 0, and 1 are not considered magic numbers.</p> | ||
<h3>Exceptions</h3> | ||
<p>This rule doesn’t raise an issue when the magic number is used as part of:</p> | ||
<ul> | ||
<li> the <code>GetHashCode</code> method </li> | ||
<li> a variable/field declaration </li> | ||
<li> the single argument of an attribute </li> | ||
<li> a named argument for a method or attribute </li> | ||
<li> a constructor call </li> | ||
<li> a default value for a method argument </li> | ||
</ul> | ||
<h2>How to fix it</h2> | ||
<p>Replacing them with a constant allows us to provide a meaningful name associated with the value. Instead of adding complexity to the code, it | ||
brings clarity and helps to understand the context and the global meaning.</p> | ||
<h3>Code examples</h3> | ||
<h4>Noncompliant code example</h4> | ||
<pre data-diff-id="1" data-diff-type="noncompliant"> | ||
public void DoSomething() | ||
{ | ||
for(int i = 0; i < 4; i++) // Noncompliant, 4 is a magic number | ||
for (int i = 0; i < 4; i++) // Noncompliant, 4 is a magic number | ||
{ | ||
... | ||
} | ||
} | ||
</pre> | ||
<h3>Compliant solution</h3> | ||
<pre> | ||
<h4>Compliant solution</h4> | ||
<pre data-diff-id="1" data-diff-type="compliant"> | ||
private const int NUMBER_OF_CYCLES = 4; | ||
|
||
public static void DoSomething() | ||
public void DoSomething() | ||
{ | ||
for(int i = 0; i < NUMBER_OF_CYCLES ; i++) //Compliant | ||
for (int i = 0; i < NUMBER_OF_CYCLES; i++) // Compliant | ||
{ | ||
... | ||
} | ||
} | ||
</pre> | ||
<h3>Exceptions</h3> | ||
<p>This rule doesn’t raise an issue when the magic number is used as part of:</p> | ||
<ul> | ||
<li> the <code>GetHashCode</code> method </li> | ||
<li> a variable/field declaration </li> | ||
<li> the single argument of an attribute </li> | ||
<li> a named argument for a method or attribute </li> | ||
<li> a constructor call </li> | ||
</ul> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,24 @@ | ||
<h2>Why is this an issue?</h2> | ||
<p>The use of parentheses, even those not required to enforce a desired order of operations, can clarify the intent behind a piece of code. But | ||
redundant pairs of parentheses could be misleading, and should be removed.</p> | ||
<p>Parentheses can disambiguate the order of operations in complex expressions and make the code easier to understand.</p> | ||
<pre> | ||
a = (b * c) + (d * e); // Compliant: the intent is clear. | ||
</pre> | ||
<p>Redundant parentheses are parenthesis that do not change the behavior of the code, and do not clarify the intent. They can mislead and complexify | ||
the code. They should be removed.</p> | ||
<h3>Noncompliant code example</h3> | ||
<pre data-diff-id="1" data-diff-type="noncompliant"> | ||
if (a && ((x + y > 0))) // Noncompliant | ||
{ | ||
//... | ||
} | ||
int x = ((y / 2 + 1)); // Noncompliant | ||
|
||
return ((x + 1)); // Noncompliant | ||
if (a && ((x + y > 0))) { // Noncompliant | ||
return ((x + 1)); // Noncompliant | ||
} | ||
</pre> | ||
<h3>Compliant solution</h3> | ||
<pre data-diff-id="1" data-diff-type="compliant"> | ||
if (a && (x + y > 0)) | ||
{ | ||
//... | ||
} | ||
int x = (y / 2 + 1); | ||
|
||
return x + 1; | ||
if (a && (x + y > 0)) { | ||
return (x + 1); | ||
} | ||
</pre> | ||
|
Oops, something went wrong.