Code review according to Jeffrey Richter

> The pattern is to take the basest class or interface possible for your arguments and return the most specific class or interface possible for your return types. This gives your callers the most flexibility in passing in types to your methods and the most opportunities to cast/reuse the return values.
This commit is contained in:
JustArchi
2017-11-16 22:10:11 +01:00
parent 216bf4b486
commit 8019cdcbb4
14 changed files with 93 additions and 56 deletions

View File

@@ -23,6 +23,7 @@
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
@@ -38,6 +39,16 @@ namespace ArchiSteamFarm {
internal static class Utilities {
private static readonly Random Random = new Random();
internal static IReadOnlyCollection<T> AsReadOnlyCollection<T>(this ICollection<T> source) {
if (source == null) {
ASF.ArchiLogger.LogNullError(nameof(source));
return null;
}
IReadOnlyCollection<T> result = source as IReadOnlyCollection<T> ?? new ReadOnlyCollectionAdapter<T>(source);
return result;
}
[SuppressMessage("ReSharper", "UnusedParameter.Global")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void Forget(this object obj) { }
@@ -152,10 +163,6 @@ namespace ArchiSteamFarm {
Task.Factory.StartNew(function, options).Forget();
}
internal static IEnumerable<T> ToEnumerable<T>(this T item) where T : struct {
yield return item;
}
internal static string ToHumanReadable(this TimeSpan timeSpan) => timeSpan.Humanize(3, maxUnit: TimeUnit.Year);
private static string[] GetArgs(string[] args, byte argsToSkip = 1) {
@@ -173,5 +180,21 @@ namespace ArchiSteamFarm {
return result;
}
internal static IEnumerable<T> ToEnumerable<T>(this T item) {
yield return item;
}
private sealed class ReadOnlyCollectionAdapter<T> : IReadOnlyCollection<T> {
public int Count => Source.Count;
private readonly ICollection<T> Source;
internal ReadOnlyCollectionAdapter(ICollection<T> source) => Source = source ?? throw new ArgumentNullException(nameof(source));
public IEnumerator<T> GetEnumerator() => Source.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}
}