Skip to content

Commit

Permalink
Prefer is for type checking, where possible
Browse files Browse the repository at this point in the history
Prefer `is` over either `Equals(typeof())` or `IsAssignableFrom()`, assuming a constant type and a positive check.
  • Loading branch information
JeremyCaney committed Dec 8, 2021
1 parent bf469db commit e6bd2b7
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
14 changes: 6 additions & 8 deletions OnTopic/Attributes/AttributeValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,24 +128,22 @@ internal static class AttributeValueConverter {
/*------------------------------------------------------------------------------------------------------------------------
| Handle type-specific rules
\-----------------------------------------------------------------------------------------------------------------------*/
var type = value.GetType();

if (type.Equals(typeof(string))) {
if (value is string) {
return (string)value;
}
else if (type.Equals(typeof(bool)) || type.Equals(typeof(bool?))) {
else if (value is bool) {
return ((bool)value) ? "1" : "0";
}
else if (type.Equals(typeof(int)) || type.Equals(typeof(int?))) {
else if (value is int) {
return ((int)value).ToString(CultureInfo.InvariantCulture);
}
else if (type.Equals(typeof(double)) || type.Equals(typeof(double?))) {
else if (value is double) {
return ((double)value).ToString(CultureInfo.InvariantCulture);
}
else if (type.Equals(typeof(DateTime)) || type.Equals(typeof(DateTime?))) {
else if (value is DateTime) {
return ((DateTime)value).ToString(CultureInfo.InvariantCulture);
}
else if (type.Equals(typeof(Uri))) {
else if (value is Uri) {
return ((Uri)value).ToString();
}

Expand Down
4 changes: 2 additions & 2 deletions OnTopic/Mapping/TopicMappingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public class TopicMappingService : ITopicMappingService {
/*------------------------------------------------------------------------------------------------------------------------
| Handle topics
\-----------------------------------------------------------------------------------------------------------------------*/
if (typeof(Topic).IsAssignableFrom(target.GetType())) {
if (target is Topic) {
return topic;
}

Expand Down Expand Up @@ -856,7 +856,7 @@ MappedTopicCache cache
if (
sourceProperty?.GetValue(source) is IList sourcePropertyValue &&
sourcePropertyValue.Count > 0 &&
typeof(Topic).IsAssignableFrom(sourcePropertyValue[0]?.GetType())
sourcePropertyValue[0] is Topic
) {
listSource = getCollection(
CollectionType.MappedCollection,
Expand Down
2 changes: 1 addition & 1 deletion OnTopic/Metadata/ContentTypeDescriptorCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class ContentTypeDescriptorCollection : KeyedTopicCollection<ContentTypeD
| Add all ContentTypeDescriptors to collection
\-----------------------------------------------------------------------------------------------------------------------*/
var contentTypeDescriptors = rootContentType
.FindAll(t => typeof(ContentTypeDescriptor).IsAssignableFrom(t.GetType()))
.FindAll(t => t is ContentTypeDescriptor)
.Cast<ContentTypeDescriptor>();
foreach (var contentType in contentTypeDescriptors) {
Add((ContentTypeDescriptor)contentType);
Expand Down
2 changes: 1 addition & 1 deletion OnTopic/Querying/TopicExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public static class TopicExtensions {
\-----------------------------------------------------------------------------------------------------------------------*/
var contentTypeDescriptor = rootContentType?.FindFirst(t =>
t.Key.Equals(topic.ContentType, StringComparison.OrdinalIgnoreCase) &&
t.GetType().IsAssignableFrom(typeof(ContentTypeDescriptor))
t is ContentTypeDescriptor
) as ContentTypeDescriptor;

/*------------------------------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit e6bd2b7

Please sign in to comment.