From 351cf721bba1dad0771c797248774810731a113d Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Mon, 22 Sep 2025 11:57:07 +0000 Subject: [PATCH] refactor: rewrite if statements using ternary operator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR refactors multiple trivial if-else blocks by replacing them with concise ternary operator expressions, reducing code verbosity and improving readability. - if statement can be rewritten using the ternary operator: Several code segments that conditionally assigned values—such as creating string and byte arrays, setting up request body streams, selecting container paths, exposing Docker ports, instantiating generic types and subscription clients, and resolving property lookups—have been replaced with single-line ternary expressions. This change preserves existing behavior while streamlining the code and making it more maintainable. > This Autofix was generated by AI. Please review the change before merging. --- ClientProxyBase/ClientProxyBase.cs | 12 ++----- .../EventSourcing/DomainEventHelper.cs | 7 +--- .../SubscriptionWorker/SubscriptionWorker.cs | 15 ++------- Shared.IntegrationTesting/BaseDockerHelper.cs | 7 +--- Shared.IntegrationTesting/Extensions.cs | 7 +--- Shared.Tests/GuardTests.cs | 33 ++----------------- Shared.Tests/MiddlewareTests.cs | 7 +--- 7 files changed, 13 insertions(+), 75 deletions(-) diff --git a/ClientProxyBase/ClientProxyBase.cs b/ClientProxyBase/ClientProxyBase.cs index 7a2193ce..b36d62e3 100644 --- a/ClientProxyBase/ClientProxyBase.cs +++ b/ClientProxyBase/ClientProxyBase.cs @@ -93,15 +93,9 @@ protected virtual ResponseData HandleResponseContent(String content) { if (String.IsNullOrEmpty(content)) { - T data = default(T); - if (typeof(IEnumerable).IsAssignableFrom(typeof(T))) - { - data = (T)Activator.CreateInstance(typeof(List<>).MakeGenericType(typeof(T).GetGenericArguments())); - } - else - { - data = (T)Activator.CreateInstance(typeof(T)); - } + T data = typeof(IEnumerable).IsAssignableFrom(typeof(T)) + ? (T)Activator.CreateInstance(typeof(List<>).MakeGenericType(typeof(T).GetGenericArguments())) + : (T)Activator.CreateInstance(typeof(T)); return new ResponseData { Data = data }; } diff --git a/Shared.DomainDrivenDesign/EventSourcing/DomainEventHelper.cs b/Shared.DomainDrivenDesign/EventSourcing/DomainEventHelper.cs index 356fbc86..cf07f509 100644 --- a/Shared.DomainDrivenDesign/EventSourcing/DomainEventHelper.cs +++ b/Shared.DomainDrivenDesign/EventSourcing/DomainEventHelper.cs @@ -18,12 +18,7 @@ public static T GetProperty(IDomainEvent domainEvent, String propertyName, Bo PropertyInfo propertyInfo = null; PropertyInfo[] properties = domainEvent.GetType() .GetProperties(); - if (ignoreCase){ - propertyInfo = properties.SingleOrDefault(p => String.Compare(p.Name, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0); - } - else{ - propertyInfo = properties.SingleOrDefault(p => p.Name == propertyName); - } + propertyInfo = ignoreCase ? properties.SingleOrDefault(p => String.Compare(p.Name, propertyName, StringComparison.CurrentCultureIgnoreCase) == 0) : properties.SingleOrDefault(p => p.Name == propertyName); if (propertyInfo != null){ return (T)propertyInfo.GetValue(domainEvent); diff --git a/Shared.EventStore/SubscriptionWorker/SubscriptionWorker.cs b/Shared.EventStore/SubscriptionWorker/SubscriptionWorker.cs index 41026625..a3ea0803 100644 --- a/Shared.EventStore/SubscriptionWorker/SubscriptionWorker.cs +++ b/Shared.EventStore/SubscriptionWorker/SubscriptionWorker.cs @@ -185,18 +185,9 @@ private async Task ExecuteAsync(CancellationToken stoppingToken) { InflightMessages = this.InflightMessages }; - IPersistentSubscriptionsClient persistentSubscriptionsClient; - - if (this.InMemory) - { - persistentSubscriptionsClient = new InMemoryPersistentSubscriptionsClient(); - } - else - { - persistentSubscriptionsClient = - new EventStorePersistentSubscriptionsClient( - this.EventStorePersistentSubscriptionsClient); - } + IPersistentSubscriptionsClient persistentSubscriptionsClient = this.InMemory + ? new InMemoryPersistentSubscriptionsClient() + : new EventStorePersistentSubscriptionsClient(this.EventStorePersistentSubscriptionsClient); PersistentSubscription subscription = PersistentSubscription.Create(persistentSubscriptionsClient, diff --git a/Shared.IntegrationTesting/BaseDockerHelper.cs b/Shared.IntegrationTesting/BaseDockerHelper.cs index 80aad80c..a875b933 100644 --- a/Shared.IntegrationTesting/BaseDockerHelper.cs +++ b/Shared.IntegrationTesting/BaseDockerHelper.cs @@ -493,12 +493,7 @@ public virtual ContainerBuilder SetupSecurityServiceContainer(){ .SetDockerCredentials(this.DockerCredentials); Int32? hostPort = this.GetHostPort(ContainerType.SecurityService); - if (hostPort == null){ - securityServiceContainer = securityServiceContainer.ExposePort(DockerPorts.SecurityServiceDockerPort); - } - else{ - securityServiceContainer = securityServiceContainer.ExposePort(hostPort.Value, DockerPorts.SecurityServiceDockerPort); - } + securityServiceContainer = hostPort == null ? securityServiceContainer.ExposePort(DockerPorts.SecurityServiceDockerPort) : securityServiceContainer.ExposePort(hostPort.Value, DockerPorts.SecurityServiceDockerPort); // Now build and return the container return securityServiceContainer; diff --git a/Shared.IntegrationTesting/Extensions.cs b/Shared.IntegrationTesting/Extensions.cs index 8b23ce54..c32ae564 100644 --- a/Shared.IntegrationTesting/Extensions.cs +++ b/Shared.IntegrationTesting/Extensions.cs @@ -12,12 +12,7 @@ public static ContainerBuilder MountHostFolder(this ContainerBuilder containerBu String containerPath = null) { if (containerPath == null) { - if (dockerEnginePlatform == DockerEnginePlatform.Windows) { - containerPath = "C:\\home\\txnproc\\trace"; - } - else { - containerPath = "/home/txnproc/trace"; - } + containerPath = dockerEnginePlatform == DockerEnginePlatform.Windows ? "C:\\home\\txnproc\\trace" : "/home/txnproc/trace"; } if (!String.IsNullOrEmpty(hostTraceFolder)) { diff --git a/Shared.Tests/GuardTests.cs b/Shared.Tests/GuardTests.cs index ec03c468..593ca9f4 100644 --- a/Shared.Tests/GuardTests.cs +++ b/Shared.Tests/GuardTests.cs @@ -78,17 +78,7 @@ public void Guard_ThrowIfNullOrEmpty_StringArrayNotNullOrEmpty_NoErrorThrown() [InlineData(null)] public void Guard_ThrowIfNullOrEmpty_StringArrayIsNullOrEmpty_ErrorThrown(String testString) { - String[] testStringArray; - - if (testString == String.Empty) - { - testStringArray = new String[]{}; - } - else - { - testStringArray = null; - } - + String[] testStringArray = testString == String.Empty ? new String[]{} : null; Should.Throw( () => Guard.ThrowIfNullOrEmpty(testStringArray, nameof(testStringArray))); } @@ -122,17 +112,7 @@ public void Guard_ThrowIfNullOrEmptyWithExceptionType_StringArrayNotNullOrEmpty_ [InlineData(null)] public void Guard_ThrowIfNullOrEmptyWithExceptionType_StringArrayIsNullOrEmpty_ErrorThrown(String testString) { - String[] testStringArray; - - if (testString == String.Empty) - { - testStringArray = new String[] { }; - } - else - { - testStringArray = null; - } - + String[] testStringArray = testString == String.Empty ? new String[] { } : null; Should.Throw(() => Guard.ThrowIfNullOrEmpty(testStringArray, typeof(ArgumentNullException), nameof(testStringArray))); } @@ -470,14 +450,7 @@ public void Guard_ThrowIfNullOrEmpty_ByteArrayIsNullOrEmpty_ErrorThrown(String t { Byte[] testByteArray; - if (testString == String.Empty) - { - testByteArray = new Byte[] { }; - } - else - { - testByteArray = null; - } + testByteArray = testString == String.Empty ? new Byte[] { } : null; Should.Throw(() => Guard.ThrowIfNullOrEmpty(testByteArray, nameof(testByteArray))); diff --git a/Shared.Tests/MiddlewareTests.cs b/Shared.Tests/MiddlewareTests.cs index 982d8137..d99871e1 100644 --- a/Shared.Tests/MiddlewareTests.cs +++ b/Shared.Tests/MiddlewareTests.cs @@ -63,12 +63,7 @@ public async Task RequestLoggingMiddleware_RequestIsLogged(String requestBody, B new(LogLevel.Warning, logRequests, true); DefaultHttpContext defaultContext = new(); - if (requestBody != null){ - defaultContext.Request.Body = new MemoryStream(Encoding.UTF8.GetBytes(requestBody)); - } - else{ - defaultContext.Request.Body = new MemoryStream(); - } + defaultContext.Request.Body = requestBody != null ? new MemoryStream(Encoding.UTF8.GetBytes(requestBody)) : new MemoryStream(); defaultContext.Request.Path = "/";