Home > Enterprise >  Why is there Unreachable code detected in C# - Nested ternary operation into if-else statement
Why is there Unreachable code detected in C# - Nested ternary operation into if-else statement

Time:12-08

before that, Im a beginner in c#. I had change nested ternary operations into independent statement but when I change it to if-else statement, it says that it is unreachable code

here are the lines that I had problem with

FlowLineSize = sumOfAllWells,
NoOfWell = _brainConceptDCDetailsInput.HydrocacbornType == "Gas"
    ? _brainConceptDCDetailsInput.GasFlowlineSize
    : _brainConceptDCDetailsInput.HydrocacbornType == "Oil"
        ? _brainConceptDCDetailsInput.OilFlowlineSize
        : 0

what is the syntax format to put if else statement after the sumOfAllWells,, I already tried putting if statement after it and it say unreachable code

Edited:

here is the full code,

public BrainSubseaJumperInputDTO FillInputDTO(ProjectInfoDTO projectInfoInput = null, BrainConceptsInputDTO conceptInput = null)
        {
            var sumOfAllWells = conceptInput?.ConceptDCDetailsInputDTO.Sum(x => x.OilProducerWell   x.GasProducerWell   x.WaterInjectorWell   x.GasInjectorWell) ?? 0;
            return new BrainSubseaJumperInputDTO()
            {
                FlowLineSize = sumOfAllWells,
                NoOfWell = _brainConceptDCDetailsInput.HydrocacbornType == "Gas" ? _brainConceptDCDetailsInput.GasFlowlineSize :
                            _brainConceptDCDetailsInput.HydrocacbornType == "Oil" ? _brainConceptDCDetailsInput.OilFlowlineSize : 0
            };
        

            

        }

CodePudding user response:

If this was valid code: After the edit, it makes more sense ...

FlowLineSize = sumOfAllWells, // <== Typo here? No, it's part of something bigger
NoOfWell = _brainConceptDCDetailsInput.HydrocacbornType == "Gas" ?
                            _brainConceptDCDetailsInput.GasFlowlineSize :
                            _brainConceptDCDetailsInput.HydrocacbornType == "Oil" ? 
                                 _brainConceptDCDetailsInput.OilFlowlineSize 
                                 :0

then that ternary abomination would roughly translate to

if( _brainConceptDCDetailsInput.HydrocacbornType == "Gas")
{
    NoOfWell = _brainConceptDCDetailsInput.GasFlowlineSize;
}
else
{
    if(_brainConceptDCDetailsInput.HydrocacbornType == "Oil")
    { 
        NoOfWell = _brainConceptDCDetailsInput.OilFlowlineSize;
    }
    else
    {
        NoOfWell = 0;
    }
}

assuming there is a NoOfWell declared.

But as pointed out in comments: There are better (i.e. cleaner, more readable) ways to express this.

For example switch expressions:

NoOfWell = _brainConceptDCDetailsInput.HydrocarbonType switch 
{ 
    "Gas" => _brainConceptDCDetailsInput.GasFlowlineSize, 
    "Oil" => _brainConceptDCDetailsInput.OilFlowlineSize, 
    _ => 0 
}

If you can make HydroCarbonType a class then this maybe could be a property of that.

If you could make it an Enum, you could use Dictionary and ExtentionMethods ...


After seeing the full code, you could also make the logic part of a DTO Builder / Factory ...

static BrainSubseaJumperInputDTO FromBrainConceptDetailsInput( 
                                     int sum, 
                                     WhateverTypeThatIs input )
{
    return new BrainSubseaJumperInputDTO(){
        FlowLineSize = sum,
        NoOfWell = input.HydrocarbonType switch 
                   { 
                       "Gas" => input.GasFlowlineSize, 
                       "Oil" => input.OilFlowlineSize, 
                       _ => 0 
                   }
    };
}

That would boil down your code to

public BrainSubseaJumperInputDTO FillInputDTO(ProjectInfoDTO projectInfoInput = null, BrainConceptsInputDTO conceptInput = null)
{
    var sumOfAllWells = conceptInput?.ConceptDCDetailsInputDTO
                                     .Sum(x => x.OilProducerWell   
                                               x.GasProducerWell   
                                               x.WaterInjectorWell   
                                               x.GasInjectorWell) 
                                     ?? 0;
    return FromBrainConceptDetailsInput( sumOfAllWells, 
                                         _brainConceptDCDetailsInput);
}
  • Related