Click here to Skip to main content
15,894,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all, I have this code with ternary operator (not easy to maintenance).
C#
d_reg_f = (entity.d_fot == DateTime.MinValue ? (xreg_f == null ? DateTime.MinValue : reg_f.d_registrazione) : entity.d_fot != DateTime.MinValue && reg_f.d_cre > entity.d_fot ? DateTime.MinValue :  reg_f.d_cre <= entity.d_fot ? reg_f.d_registrazione : DateTime.MinValue);


What I have tried:

I converted the code like this

C#
if (entity.d_fot == DateTime.MinValue)
      if (xreg_f == null)
                { d_reg_f = DateTime.MinValue; }
                else
                { d_reg_f = reg_f.d_registrazione; }
            else if (entity.d_fot != DateTime.MinValue && reg_f.d_cre > entity.d_fot)
            { d_reg_f = DateTime.MinValue; }
            else if (reg_f.d_cre <= entity.d_fot)
            { d_reg_f = reg_f.d_registrazione; }
            else
            { d_reg_f = DateTime.MinValue; }

It seems to work fine but I'm not sure.
Did I forget something in the If? What do you think?
Thanks to those who will answer me.
Posted
Updated 19-Jun-22 0:48am
v2
Comments
0x01AA 19-Jun-22 6:46am    
I found this: Ternary to If-else online converter[^]
Maybe it helps you to verify your code.

Using this tool: Ternary to If-else online converter[^]
I get this:
if (entity.d_fot == DateTime.MinValue) {
	if (xreg_f == null) {
		d_reg_f= DateTime.MinValue;
	} else {
		d_reg_f= reg_f.d_registrazione;
	}
} else {
	if (entity.d_fot != DateTime.MinValue && reg_f.d_cre > entity.d_fot) {
		d_reg_f= DateTime.MinValue;
	} else {
		if (reg_f.d_cre <= entity.d_fot) {
			d_reg_f= reg_f.d_registrazione;
		} else {
			d_reg_f= DateTime.MinValue;
		}
	}
}
 
Share this answer
 
v4
Comments
Graeme_Grant 19-Jun-22 6:58am    
check the lack of value assignment
0x01AA 19-Jun-22 7:02am    
Yes I'm aware of that. But it should show only the logic. OP seems to have enough knowledge to complete it ;)
0x01AA 19-Jun-22 7:15am    
there you go ... cleaner solution
Graeme_Grant 19-Jun-22 7:20am    
5'd ... but you missed one... :P
0x01AA 19-Jun-22 7:26am    
Thanks :-)
Readability comes down to formatting:

1. Ternary:
C#
d_reg_f = (entity.d_fot == DateTime.MinValue
    ? (xreg_f == null
        ? DateTime.MinValue
        : reg_f.d_registrazione)
    : entity.d_fot != DateTime.MinValue && reg_f.d_cre > entity.d_fot
       ? DateTime.MinValue
       : reg_f.d_cre <= entity.d_fot
           ? reg_f.d_registrazione
           : DateTime.MinValue
    );

2. if ... else if ... else with Ternary
C#
if (entity.d_fot == DateTime.MinValue)
{
    d_reg_f = xreg_f == null
        ? DateTime.MinValue
        : reg_f.d_registrazione;
}
else if (entity.d_fot != DateTime.MinValue && reg_f.d_cre > entity.d_fot)
{
    d_reg_f = reg_f.d_cre <= entity.d_fot
        ? reg_f.d_registrazione
        : DateTime.MinValue;
}
else
{
    d_reg_f = DateTime.MinValue;
}
 
Share this answer
 
v3
Comments
0x01AA 19-Jun-22 6:56am    
Check if ... else ... else if ;)
Graeme_Grant 19-Jun-22 6:58am    
:P
0x01AA 19-Jun-22 7:01am    
?I mean, I know no language which can handle:

if
else
else if
else if
else
Graeme_Grant 19-Jun-22 7:03am    
there you go ... cleaner solution
0x01AA 19-Jun-22 7:03am    
5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900