Giter VIP home page Giter VIP logo

mvonballmo / csharphandbook Goto Github PK

View Code? Open in Web Editor NEW
27.0 8.0 5.0 5.92 MB

The focus of this document is on providing a reference for writing C#. It includes naming, structural and formatting conventions as well as best practices for writing clean, safe and maintainable code. Many of the best practices and conventions apply equally well to other languages.

csharp handbook best-practices language formatting naming-conventions conventions

csharphandbook's Issues

Update to include advice for C# 8

Relevant for the manual

  • Nullable reference types (default settings, how to use)
  • Default implementations (rework the extension methods section/advice)
  • Target-typed new expressions
  • Switch expressions (lightweight pattern-matching)
  • Note on Ranges/indices

Add FAQ / tl;dr at the beginning of the document

Observed

The document is quite long and doesn't provide very much of a summary of the rules most important to developers.

Expected

The document should include an FAQ with links into the rest of the document that complements the TOC with another way of getting into the material.

Analysis

Add sections for the following questions (to begin with):

  • Should I use global namespaces?
  • Should I use var?
  • Should I use records? When?
  • Should I use record structs? When?

Settings Files (StyleCop , ReSharper and EditorConfig.)

Hello,

We have been using the handbook quite a bit. but have not been able to find the settings files.

Settings files
This repository includes configuration files that set up the rules outlined in this handbook for StyleCop and ReSharper and EditorConfig.

if you can point us in the right direction it would be greatly appreciated.

Thank in advance.

Add advice on ternary operators and code coverage

Every code-coverage tool I've seen tracks coverage by lines. A ternary operator or coalescing operator places multiple branches on a single line.

For example, the following code looks quite verbose.

if (expression != null)
{
  if (otherBaseExpression == null)
  {
    return false;
  }

  return expression.Equals(otherBaseExpression);
}

if (otherBaseExpression != null)
{
  return false;
}

return Equals(BaseValue, otherDynamicType.BaseValue);

While we could rewrite the code as follows, tightening things up a bit and not losing too much legibility, we would lose the line-by-line coverage we had before. That is, we won't be able to tell from our coverage whether each branch was really tested.

if (expression != null)
{
  return otherBaseExpression != null && expression.Equals(otherBaseExpression);
}

return otherBaseExpression == null && Equals(BaseValue, otherDynamicType.BaseValue);

Add notes on using statements (without braces)

Notes

It's fine to use them, of course. Just be aware that the lifetime of each using is the entire method. If you stick to shorter methods without or with limited side-effects, then this won't be a problem. Just make sure that you're not allowing the lifetime of an IDisposable be extended unnecessarily because you didn't close it off with braces. If this is happening, then you probably have a method that's doing two things anyway and you can take this as a hint that you should split the method, possibly using local functions to determine the disposable's lifetime.

Add section on global usings

Notes

Library authors should be aware that package users will generally only see the source files downloaded via SourceLink. If too many of the usings are in a solution-specific "global usings" file, then it won't be clear from the sources where the types came from. The compiler will know, but the developer will not. This applies also to direct developers of the project.

It almost goes without saying that you should not include global usings that might cause ambiguities in too many source files.

Add warning about records vs. struct records

Notes

A struct record has mutable properties, whereas a record has immutable properties. A readonly struct record has immutable properties. Be aware that simply changing record to struct record will also change formerly immutable properties to mutable ones.

Include section on how to create transient objects

C# offers support for object-initialization, including init and required properties that allow a user to create immutable objects with a very convenient syntax.

Avoid using factories to create objects that are better created with new().

Drawbacks:

  • You can't use object-initialization syntax
  • It's difficult to find references to where these objects are created

For example, if you have a database layer that lets you create and save objects, an API that returns a new object is not ideal.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.