Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated AV1570 #114

Merged
merged 2 commits into from
Oct 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Src/Cheatsheet/Cheatsheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ NOTE: Requires Markdown Extra. See http://michelf.ca/projects/php-markdown/extra
* Don’t allow methods and constructors with more than three parameters (AV1561)
* Don’t use `ref` or `out` parameters (AV1562)
* Avoid methods that take a `bool` flag (AV1564)
* Always check the result of an `as` operation (AV1570)
* Prefer `is` patterns over `as` operations (AV1570)
* Don’t comment-out code (AV1575)

<br/>
Expand Down
17 changes: 15 additions & 2 deletions Src/Guidelines/1500_MaintainabilityGuidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,21 @@ Often, a method taking such a flag is doing more than one thing and needs to be
### <a name="av1568"></a> Don't use parameters as temporary variables (AV1568) ![](images/3.png)
Never use a parameter as a convenient variable for storing temporary state. Even though the type of your temporary variable may be the same, the name usually does not reflect the purpose of the temporary variable.

### <a name="av1570"></a> Always check the result of an `as` operation (AV1570) ![](images/1.png)
If you use `as` to obtain a certain interface reference from an object, always ensure that this operation does not return `null`. Failure to do so may cause a `NullReferenceException` at a much later stage if the object did not implement that interface.
### <a name="av1570"></a> Prefer `is` patterns over `as` operations (AV1570) ![](images/1.png)

If you use 'as' to safely upcast an interface reference to a certain type, always verify that the operation does not return `null`. Failure to do so may cause a `NullReferenceException` at a later stage if the object did not implement that interface.
Pattern matching syntax prevents this and improves readability. For example, instead of:

var remoteUser = user as RemoteUser;
if (remoteUser != null)
{
}

write:

if (user is RemoteUser remoteUser)
{
}

### <a name="av1575"></a> Don't comment out code (AV1575) ![](images/1.png)
Never check in code that is commented out. Instead, use a work item tracking system to keep track of some work to be done. Nobody knows what to do when they encounter a block of commented-out code. Was it temporarily disabled for testing purposes? Was it copied as an example? Should I delete it?