Skip to main content
added 545 characters in body
Source Link
palacsint
  • 30.4k
  • 9
  • 82
  • 157

I'm not too familiar with C#, so just atwo general note: Instead of structures likenotes.

  1. Instead of structures like this:

     if (idsToTypes.ContainsKey(id))
     {
         return Activator.CreateInstance(idsToTypes[id], args);
     }
     else
     {
         return null;
     }
    

you could write this:

    if (idsToTypes.ContainsKey(id))
{
    return Activator.CreateInstance(idsToTypes[id], args);
}
else
{
    return null;
}

You could write this:

if (idsToTypes.ContainsKey(id))
{
    return Activator.CreateInstance(idsToTypes[id], args);
    }
    return null;

The else keyword is unnecessary in these cases.

  1. Using guard clauses makes the code flatten and more readable.

     if (!type.IsAssignableFrom(_type)) {
         throw new ArgumentException(String.Format("Registered type, {0}, was not assignable from {1}", _type, type));
     }
     idsToTypes.Add(id, _type);
    

References: Replace Nested Conditional with Guard Clauses in Refactoring: Improving the Design of Existing Code; Flattening Arrow Code

I'm not too familiar with C#, so just a general note: Instead of structures like this:

if (idsToTypes.ContainsKey(id))
{
    return Activator.CreateInstance(idsToTypes[id], args);
}
else
{
    return null;
}

You could write this:

if (idsToTypes.ContainsKey(id))
{
    return Activator.CreateInstance(idsToTypes[id], args);
}
return null;

The else keyword is unnecessary in these cases.

I'm not too familiar with C#, so just two general notes.

  1. Instead of structures like this:

     if (idsToTypes.ContainsKey(id))
     {
         return Activator.CreateInstance(idsToTypes[id], args);
     }
     else
     {
         return null;
     }
    

you could write this:

    if (idsToTypes.ContainsKey(id))
    {
        return Activator.CreateInstance(idsToTypes[id], args);
    }
    return null;

The else keyword is unnecessary in these cases.

  1. Using guard clauses makes the code flatten and more readable.

     if (!type.IsAssignableFrom(_type)) {
         throw new ArgumentException(String.Format("Registered type, {0}, was not assignable from {1}", _type, type));
     }
     idsToTypes.Add(id, _type);
    

References: Replace Nested Conditional with Guard Clauses in Refactoring: Improving the Design of Existing Code; Flattening Arrow Code

fixed example
Source Link
palacsint
  • 30.4k
  • 9
  • 82
  • 157

I'm not too familiar with C#, so just a general note: Instead of structures like this:

if (typeidsToTypes.IsAssignableFromContainsKey(_typeid))
{
    idsToTypesreturn Activator.AddCreateInstance(ididsToTypes[id], _typeargs);
}
else
{
    throw new ArgumentException(String.Format("Registered type, {0}, was not assignable fromreturn {1}",_type,type));null;
}

You could write this:

if (typeidsToTypes.IsAssignableFromContainsKey(_typeid))
{
    idsToTypesreturn Activator.AddCreateInstance(ididsToTypes[id], _typeargs);
}
throw new ArgumentException(String.Format("Registered type, {0}, was not assignable from {1}", _type,return type));null;

The else keyword is unnecessary in these cases.

I'm not too familiar with C#, so just a general note: Instead of structures like this:

if (type.IsAssignableFrom(_type))
{
    idsToTypes.Add(id, _type);
}
else
{
    throw new ArgumentException(String.Format("Registered type, {0}, was not assignable from {1}",_type,type));
}

You could write this:

if (type.IsAssignableFrom(_type))
{
    idsToTypes.Add(id, _type);
}
throw new ArgumentException(String.Format("Registered type, {0}, was not assignable from {1}", _type, type));

The else keyword is unnecessary in these cases.

I'm not too familiar with C#, so just a general note: Instead of structures like this:

if (idsToTypes.ContainsKey(id))
{
    return Activator.CreateInstance(idsToTypes[id], args);
}
else
{
    return null;
}

You could write this:

if (idsToTypes.ContainsKey(id))
{
    return Activator.CreateInstance(idsToTypes[id], args);
}
return null;

The else keyword is unnecessary in these cases.

Source Link
palacsint
  • 30.4k
  • 9
  • 82
  • 157

I'm not too familiar with C#, so just a general note: Instead of structures like this:

if (type.IsAssignableFrom(_type))
{
    idsToTypes.Add(id, _type);
}
else
{
    throw new ArgumentException(String.Format("Registered type, {0}, was not assignable from {1}",_type,type));
}

You could write this:

if (type.IsAssignableFrom(_type))
{
    idsToTypes.Add(id, _type);
}
throw new ArgumentException(String.Format("Registered type, {0}, was not assignable from {1}", _type, type));

The else keyword is unnecessary in these cases.