From c0acbe286e3db6c872c3aad37efb2160312a90df Mon Sep 17 00:00:00 2001 From: Haacked Date: Thu, 27 Aug 2015 10:59:03 -0700 Subject: [PATCH] :art: Remove rendundant null check In `GitClient.cs` we checked whether `repository.Head == null` twice in a row. I wanted to remove this redundancy and just ended up using the fun null propagation operator where we could. --- src/GitHub.App/Controllers/UIController.cs | 3 +-- src/GitHub.App/Models/RepositoryHost.cs | 2 +- src/GitHub.App/Services/AvatarProvider.cs | 2 +- src/GitHub.App/Services/GitClient.cs | 5 +---- src/GitHub.App/Services/Translation.cs | 9 +++------ src/GitHub.App/ViewModels/RepositoryPublishViewModel.cs | 2 +- 6 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/GitHub.App/Controllers/UIController.cs b/src/GitHub.App/Controllers/UIController.cs index fb7d53f053..67690a68cf 100644 --- a/src/GitHub.App/Controllers/UIController.cs +++ b/src/GitHub.App/Controllers/UIController.cs @@ -272,8 +272,7 @@ protected virtual void Dispose(bool disposing) Debug.WriteLine("Disposing ({0})", GetHashCode()); disposables.Dispose(); - if (transition != null) - transition.Dispose(); + transition?.Dispose(); disposed = true; } } diff --git a/src/GitHub.App/Models/RepositoryHost.cs b/src/GitHub.App/Models/RepositoryHost.cs index fa8dd30520..84d9a0c4ba 100644 --- a/src/GitHub.App/Models/RepositoryHost.cs +++ b/src/GitHub.App/Models/RepositoryHost.cs @@ -104,7 +104,7 @@ public IObservable LogIn(string usernameOrEmail, string pa // in multiple places in the chain below: var saveAuthorizationToken = new Func>(authorization => { - var token = authorization != null ? authorization.Token : null; + var token = authorization?.Token; if (string.IsNullOrWhiteSpace(token)) return Observable.Return(Unit.Default); diff --git a/src/GitHub.App/Services/AvatarProvider.cs b/src/GitHub.App/Services/AvatarProvider.cs index e7e7244b11..9b9901c47a 100644 --- a/src/GitHub.App/Services/AvatarProvider.cs +++ b/src/GitHub.App/Services/AvatarProvider.cs @@ -67,7 +67,7 @@ public IObservable GetAvatar(IAvatarContainer apiAccount) public IObservable InvalidateAvatar([AllowNull] IAvatarContainer apiAccount) { - return apiAccount == null || String.IsNullOrWhiteSpace(apiAccount.Login) + return String.IsNullOrWhiteSpace(apiAccount?.Login) ? Observable.Return(Unit.Default) : cache.Invalidate(apiAccount.Login); } diff --git a/src/GitHub.App/Services/GitClient.cs b/src/GitHub.App/Services/GitClient.cs index 1e072842b5..453560097c 100644 --- a/src/GitHub.App/Services/GitClient.cs +++ b/src/GitHub.App/Services/GitClient.cs @@ -18,10 +18,7 @@ public IObservable Push(IRepository repository, string branchName, string return Observable.Defer(() => { - if (repository.Head != null - && repository.Head != null - && repository.Head.Commits != null - && repository.Head.Commits.Any()) + if (repository.Head?.Commits != null && repository.Head.Commits.Any()) { var remote = repository.Network.Remotes[remoteName]; repository.Network.Push(remote, "HEAD", @"refs/heads/" + branchName); diff --git a/src/GitHub.App/Services/Translation.cs b/src/GitHub.App/Services/Translation.cs index 78ead6126a..9f08a6ec6b 100644 --- a/src/GitHub.App/Services/Translation.cs +++ b/src/GitHub.App/Services/Translation.cs @@ -67,12 +67,9 @@ protected virtual Tuple Match(Exception exception) string exceptionMessage = exception.Message; var apiException = exception as ApiValidationException; - if (apiException != null && apiException.ApiError != null && apiException.ApiError.Errors != null) - { - var error = apiException.ApiError.Errors.FirstOrDefault(); - if (error != null) - exceptionMessage = error.Message ?? exceptionMessage; - } + var error = apiException?.ApiError?.Errors?.FirstOrDefault(); + if (error != null) + exceptionMessage = error.Message ?? exceptionMessage; if (Original == exceptionMessage) return new Tuple(this, null); diff --git a/src/GitHub.App/ViewModels/RepositoryPublishViewModel.cs b/src/GitHub.App/ViewModels/RepositoryPublishViewModel.cs index 61263312b2..2c65d4ace1 100644 --- a/src/GitHub.App/ViewModels/RepositoryPublishViewModel.cs +++ b/src/GitHub.App/ViewModels/RepositoryPublishViewModel.cs @@ -191,7 +191,7 @@ void InitializeValidation() this.WhenAny(x => x.SafeRepositoryNameWarningValidator.ValidationResult, x => x.Value) .WhereNotNull() // When this is instantiated, it sends a null result. - .Select(result => result == null ? null : result.Message) + .Select(result => result?.Message) .Subscribe(message => { if (!string.IsNullOrEmpty(message))