Repository: microsoft/garnet
File: libs/server/Resp/BasicCommands.cs (C#)

Code before:
            while (!RespWriteUtils.TryWriteDirect(CmdStrings.RESP_OK, ref dcurr, dend))
                SendAndReset();

            return true;
        }

        /// <summary>
        /// SETNX
        /// </summary>
        private bool NetworkSETNX<TGarnetApi>(bool highPrecision, ref TGarnetApi storageApi)
            where TGarnetApi : IGarnetApi
        {
            Debug.Assert(parseState.Count == 2);
            var key = parseState.GetArgSliceByRef(0);
            var value = parseState.GetArgSliceByRef(1);
            var getOption = ArgSlice.FromPinnedSpan(CmdStrings.NX);
            parseState.InitializeWithArguments(key, value, getOption);

            var nkey = parseState.GetArgSliceByRef(0);
            var sbKey = nkey.SpanByte;

            var input = new RawStringInput(RespCommand.SETEXNX, ref parseState, startIdx: 1, arg1: 0);
            var status = storageApi.SET_Conditional(ref sbKey, ref input);

            // The status returned for SETNX as NOTFOUND is the expected status in the happy path
            if (status == GarnetStatus.NOTFOUND)
            {
                while (!RespWriteUtils.TryWriteInt32(1, ref dcurr, dend))
                    SendAndReset();
            }
            else
            {
                while (!RespWriteUtils.TryWriteInt32(0, ref dcurr, dend))
                    SendAndReset();
            }

            return true;
        }

        /// <summary>
        /// SET EX NX [WITHETAG]
        /// </summary>
        private bool NetworkSETEXNX<TGarnetApi>(ref TGarnetApi storageApi)
            where TGarnetApi : IGarnetApi
        {
            var key = parseState.GetArgSliceByRef(0);
            var sbKey = key.SpanByte;

            var val = parseState.GetArgSliceByRef(1);
            var sbVal = val.SpanByte;


Review comment (refactor):
nit: slightly cleaner so you don't have to duplicate the write code:
```cs
var retVal = status == GarnetStatus.NOTFOUND ? 1 : 0;
while (!RespWriteUtils.TryWriteInt32(retVal, ref dcurr, dend))
    SendAndReset();
```

Code after:
            while (!RespWriteUtils.TryWriteDirect(CmdStrings.RESP_OK, ref dcurr, dend))
                SendAndReset();

            return true;
        }

        /// <summary>
        /// SETNX
        /// </summary>
        private bool NetworkSETNX<TGarnetApi>(bool highPrecision, ref TGarnetApi storageApi)
            where TGarnetApi : IGarnetApi
        {
            Debug.Assert(parseState.Count == 2);
            var key = parseState.GetArgSliceByRef(0);
            var sbKey = key.SpanByte;

            var input = new RawStringInput(RespCommand.SETEXNX, ref parseState, startIdx: 1);
            var status = storageApi.SET_Conditional(ref sbKey, ref input);

            // The status returned for SETNX as NOTFOUND is the expected status in the happy path
            var retVal = status == GarnetStatus.NOTFOUND ? 1 : 0;
            while (!RespWriteUtils.TryWriteInt32(retVal, ref dcurr, dend))
                SendAndReset();

            return true;
        }

        /// <summary>
        /// SET EX NX [WITHETAG]
        /// </summary>
        private bool NetworkSETEXNX<TGarnetApi>(ref TGarnetApi storageApi)
            where TGarnetApi : IGarnetApi
        {
            var key = parseState.GetArgSliceByRef(0);
            var sbKey = key.SpanByte;

            var val = parseState.GetArgSliceByRef(1);
            var sbVal = val.SpanByte;

            var expiry = 0;
            ReadOnlySpan<byte> errorMessage = default;
            var existOptions = ExistOptions.None;
            var expOption = ExpirationOption.None;
            var etagOption = EtagOption.None;
            var getValue = false;

            var tokenIdx = 2;
            Span<byte> nextOpt = default;
            var optUpperCased = false;
            while (tokenIdx < parseState.Count || optUpperCased)
            {