Skip to content

Conversation

@iSazonov
Copy link
Collaborator

@iSazonov iSazonov commented Oct 3, 2017

Add links on Microsoft best practice guidelines.
Add a recommendation to avoid initializing by 0 or null.

as it obscures the functional changes of the PR.
A separate PR should be submitted for such refactoring without any functional changes.

* Avoid initializing variables, fields and auto-implemented properties to zero or null because C# guarantees its are initialized to zero.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel this one is not important to be put here. For example, some developers might always make sure all local variables are initialized before use, which is fine and won't actually cause any overhead -- IL code will have the assignment but JIT will be smart enough to not do the assignment.

Instead, I think we should mention the Interlocked type here, which I saw in Managed Threading Best Practices:
Consider using the Interlocked class instead of the lock statement to atomically change simple states. The Interlocked class provides better performance for updates that must be atomic.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will add Interlocked.

We should have a consensus about initializations - it seems your positions is different from the @PaulHigin position.

Copy link
Member

@daxian-dbw daxian-dbw Oct 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think null/zero initializations for local variables doesn't matter perf-wise. Personally, I prefer to always initialize local variables before use, but that's not something should be enforced to everyone.
For type fields, I saw some are initialized to 0, but rarely see people initialize them to null, but either way, I won't pick it in code review.
For auto properties, I never saw people initialize it to null or zero (maybe there are but rare).

Overall, I think we should just remove this item.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.


* Avoid initializing variables, fields and auto-implemented properties to zero or null because C# guarantees its are initialized to zero.

* Follow Microsoft best practise guidances:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not say follow because some of those guidelines are contradicting with our coding convention. For example, the naming guideline in the first link below says "do not use "g_" or "s_" to indicate static fields", while we require static fields to start with s_ or t_.

I think it's better to say: "Here are some useful links for your reference:"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

* [Collections](https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/guidelines-for-collections)
* [Exceptions](https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/exceptions)
* [Best Practices for Developing World-Ready Applications](https://docs.microsoft.com/en-us/dotnet/standard/globalization-localization/best-practices-for-developing-world-ready-apps) - Unicode, Culture, Encoding and Localization.
* [Best Practices for System.Net Classes](https://docs.microsoft.com/en-us/dotnet/framework/network-programming/best-practices-for-system-net-classes)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one seems to focus on full .NET instead of .NET Core, so it mainly talks about things related to WebRequest and WebResponse.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added just for WebRequest and WebResponse.
Remove?

Copy link
Member

@daxian-dbw daxian-dbw Oct 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WebRequest and WebResponse are mostly deprecated, and HttpClient is the way to go. So I think we can remove this link.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

* [Exceptions](https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/exceptions)
* [Best Practices for Developing World-Ready Applications](https://docs.microsoft.com/en-us/dotnet/standard/globalization-localization/best-practices-for-developing-world-ready-apps) - Unicode, Culture, Encoding and Localization.
* [Best Practices for System.Net Classes](https://docs.microsoft.com/en-us/dotnet/framework/network-programming/best-practices-for-system-net-classes)
* [Best practices for exceptions](https://docs.microsoft.com/en-us/dotnet/standard/exceptions/best-practices-for-exceptions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use capital P for practices and capital E for exceptions to be consistent with others.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

* [Best Practices for Developing World-Ready Applications](https://docs.microsoft.com/en-us/dotnet/standard/globalization-localization/best-practices-for-developing-world-ready-apps) - Unicode, Culture, Encoding and Localization.
* [Best Practices for System.Net Classes](https://docs.microsoft.com/en-us/dotnet/framework/network-programming/best-practices-for-system-net-classes)
* [Best practices for exceptions](https://docs.microsoft.com/en-us/dotnet/standard/exceptions/best-practices-for-exceptions)
* [Best Practices for Using Strings in .NET](https://docs.microsoft.com/en-us/dotnet/standard/base-types/best-practices-strings)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have been very confused about when to use InvariantCulture vs. Ordinal, and I believe there are many other developers like me :) I think this one is very useful.

* [Best practices for exceptions](https://docs.microsoft.com/en-us/dotnet/standard/exceptions/best-practices-for-exceptions)
* [Best Practices for Using Strings in .NET](https://docs.microsoft.com/en-us/dotnet/standard/base-types/best-practices-strings)
* [Best Practices for Regular Expressions in .NET](https://docs.microsoft.com/en-us/dotnet/standard/base-types/best-practices)
* [Serialization guidelines](https://docs.microsoft.com/en-us/dotnet/standard/serialization/serialization-guidelines)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use capital G for guidelines.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

as it obscures the functional changes of the PR.
A separate PR should be submitted for such refactoring without any functional changes.

* Avoid initializing variables, fields and auto-implemented properties to zero or null because C# guarantees its are initialized to zero.
Copy link
Member

@daxian-dbw daxian-dbw Oct 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think null/zero initializations for local variables doesn't matter perf-wise. Personally, I prefer to always initialize local variables before use, but that's not something should be enforced to everyone.
For type fields, I saw some are initialized to 0, but rarely see people initialize them to null, but either way, I won't pick it in code review.
For auto properties, I never saw people initialize it to null or zero (maybe there are but rare).

Overall, I think we should just remove this item.

* [Collections](https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/guidelines-for-collections)
* [Exceptions](https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/exceptions)
* [Best Practices for Developing World-Ready Applications](https://docs.microsoft.com/en-us/dotnet/standard/globalization-localization/best-practices-for-developing-world-ready-apps) - Unicode, Culture, Encoding and Localization.
* [Best Practices for System.Net Classes](https://docs.microsoft.com/en-us/dotnet/framework/network-programming/best-practices-for-system-net-classes)
Copy link
Member

@daxian-dbw daxian-dbw Oct 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WebRequest and WebResponse are mostly deprecated, and HttpClient is the way to go. So I think we can remove this link.

@daxian-dbw daxian-dbw merged commit ec1106a into PowerShell:master Oct 5, 2017
@iSazonov iSazonov deleted the coding-guidence branch October 6, 2017 03:45
@iSazonov iSazonov mentioned this pull request May 3, 2018
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants