Skip to content

Coding Refactorings #358

@JasonBock

Description

@JasonBock

Describe the solution you'd like
I did a review of the code to see what kind of improvements I could make. Here's a random list of all of them I found. Some of them are very minor, some may help with performance.

DONE - TypeReferenceModel Caching

TypeReferenceModel can be cached for a generator run. For example, these two methods will generate two instances of a TypeReferenceModel for the string, even though they're exactly the same:

public interface IOne
{
    void Work(string value);
}

public interface ITwo
{
    void Work(string value);
}

Therefore, make the ctor private, and have a static Create() method that will use a Dictionary<?, TypeReferenceModel>. Hopefully that will reduce the amount of TRMs made. Probably use ITypeSymbol for the dictionary key.

DONE - TypeReferenceModel IsPointer() calls

Within TypeReferenceModel, type.IsPointer() is called twice. For the second time, just use the IsPointer property.

Naming Clean-Up

Naming is hard. I have multiple ways to get "names":

  • ITypeSymbolExtensions.GetFullyQualifiedName()
  • ITypeSymbolExtensions.GetName()
  • TypeReferenceModel.BuildName()

Not sure what I can do here, but it would be nice if I could do some consolidation.

DONE - Removing Dead Code

Looks like a number of types and type members are no longer used, and can (probably) be removed.

  • ConstructorModel
    • MockType
  • EventModel
    • MockType
  • ParameterModel
    • MockType
  • PropertyModel
    • AllowNull
  • TypeReferenceModel
    • Kind
    • PointerArgParameterType
    • PointerArgProjectedEvaluationDelegateName
    • PointerArgProjectedName
  • IDictionaryOfTKeyTValueExtensions
  • INamespaceSymbolExtensions
  • SyntaxNodeExtensions
  • MockedType

DONE - Remove .ToImmutableArray()

Looks like I can get rid of that call and just use collection expressions with a spread.

Also, CreateBuilder and .Builder.

DONE - Code Suggestion Improvements

The following classes have minor code suggestion improvements:

  • MockableConstructorDiscovery
  • TypeArgumentsNamingContext
  • VariablesNamingContext
  • IndentedTextWriterExtensions
  • Expectations 

DONE - Miscellaneous Minor Changes

  • In TypeMockModel, setting this.ExpectationsIsSealed can be simplified.
  • Property exception code generation is off. It works, but the indentation is a little weird. Look at PropertyGeneratorTests.GenerateWithNewDefinitionAsync() for an example.
  • MockAdornmentsBuilder, line 20, raw string literal is a bit off.
  • IParameterSymbolExtensions.RequiresForcedNullableAnnotation(), minor indention change.

Describe alternatives you've considered
Keep the code as/is.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions