Skip to content

Commit

Permalink
Don't put non-trivial code inside pre-processor logic.
Browse files Browse the repository at this point in the history
It makes maintenance unreasonably hard as you get different compile time errors/warnings depending on the build type.
  • Loading branch information
sarahelsaig committed Oct 18, 2023
1 parent 87b31aa commit 221050f
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/Hastlayer/Hast.Transformer/SimpleMemory/SimpleMemory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Hast.Synthesis.Models;
using Lombiq.HelpfulLibraries.Common.Utilities;
using Microsoft.Extensions.Logging;
using System;
using System.Runtime.InteropServices;
Expand All @@ -21,6 +22,13 @@ public class SimpleMemory
{
public const int MemoryCellSizeBytes = sizeof(int);

private const bool IsDebug =
#if DEBUG
true;
#else
false;
#endif

/// <summary>
/// Gets the span of memory at the cellIndex, the length is <see cref="MemoryCellSizeBytes"/>.
/// </summary>
Expand Down Expand Up @@ -94,10 +102,9 @@ internal SimpleMemory(Memory<byte> memory, int prefixCellCount, int alignment)
{
memory = memory.Slice(alignmentOffset, memory.Length - alignment);
}
else
else if (IsDebug)
{
// This should never happen in production.
#if DEBUG
Console.Error.WriteLine("Alignment failed!");

Check failure on line 108 in src/Hastlayer/Hast.Transformer/SimpleMemory/SimpleMemory.cs

View workflow job for this annotation

GitHub Actions / Publish to NuGet / publish-nuget

CS0162: Unreachable code detected [/home/runner/work/Hastlayer-SDK/Hastlayer-SDK/src/Hastlayer/Hast.Transformer/Hast.Transformer.csproj]
Console.Error.WriteLine(" 64-bit: {0}", Environment.Is64BitProcess);
Console.Error.WriteLine(" address: {0}", address);
Expand All @@ -106,7 +113,9 @@ internal SimpleMemory(Memory<byte> memory, int prefixCellCount, int alignment)
Console.Error.WriteLine(" alignment: {0}", alignment);
Console.Error.WriteLine(" alignmentOffset: {0}", alignmentOffset);
Console.Error.WriteLine(" expectedLength: {0}", expectedLength);
#else
}
else
{
throw new InvalidOperationException(
"Alignment failed! (" +
StringHelper.CreateInvariant($"64-bit: {Environment.Is64BitProcess}; ") +
Expand All @@ -116,7 +125,6 @@ internal SimpleMemory(Memory<byte> memory, int prefixCellCount, int alignment)
StringHelper.CreateInvariant($"alignment: {alignment}; ") +
StringHelper.CreateInvariant($"alignmentOffset: {alignmentOffset}; ") +
StringHelper.CreateInvariant($"expectedLength: {expectedLength})"));
#endif
}
}

Expand Down

0 comments on commit 221050f

Please sign in to comment.