Click here to Skip to main content
15,886,137 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Cosmetic vs More Efficient Pin
Gary Wheeler5-May-21 1:58
Gary Wheeler5-May-21 1:58 
GeneralRe: Cosmetic vs More Efficient Pin
W Balboos, GHB5-May-21 2:37
W Balboos, GHB5-May-21 2:37 
GeneralRe: Cosmetic vs More Efficient Pin
KateAshman5-May-21 2:27
KateAshman5-May-21 2:27 
GeneralRe: Cosmetic vs More Efficient Pin
W Balboos, GHB5-May-21 2:34
W Balboos, GHB5-May-21 2:34 
GeneralRe: Cosmetic vs More Efficient Pin
KateAshman7-May-21 4:25
KateAshman7-May-21 4:25 
GeneralRe: Cosmetic vs More Efficient Pin
Steve Naidamast5-May-21 4:19
professionalSteve Naidamast5-May-21 4:19 
GeneralRe: Cosmetic vs More Efficient Pin
W Balboos, GHB5-May-21 5:21
W Balboos, GHB5-May-21 5:21 
GeneralRe: Cosmetic vs More Efficient Pin
Al Gonzalez5-May-21 5:32
Al Gonzalez5-May-21 5:32 
I don't think it matters much as the compilers are pretty efficient at this.
In C#, I usually use the "null coalescing assignment operator (??=)"; so "inVal ??= internalVal;".

Here are some functions to test the various ways to assign a default value:
C#
void FixNullArg_SimpleIf(string arg = null) {
    if (arg == null)
        arg = "Fix null via simple if";
    Console.WriteLine(arg);
}
void FixNullArg_TernaryOp(string arg = null)
{
    arg = (arg == null) ? "Fix null via ternary operator" : arg;
    Console.WriteLine(arg);
}
void FixNullArg_NullCoalescingOp(string arg = null)
{
    arg = arg ?? "Fix null via ?? operator";
    Console.WriteLine(arg);
}
void FixNullArg_NullCoalescingAssignmentOp(string arg = null)
{
    arg ??= "Fix null via ??= operator";
    Console.WriteLine(arg);
}
void FixNullArg_IsNullOrWhiteSpace(string arg = null)
{
    arg = string.IsNullOrWhiteSpace(arg) ? "Fix null via IsNullOrWhiteSpace" : arg;
    Console.WriteLine(arg);
}
And here is the decompiled Intermediate Language (IL):
MSIL
FixNullArg_SimpleIf:
IL_0000:  nop         
IL_0001:  ldarg.1     
IL_0002:  ldnull      
IL_0003:  ceq         
IL_0005:  stloc.0     
IL_0006:  ldloc.0     
IL_0007:  brfalse.s   IL_0010
IL_0009:  ldstr       "Fix null via simple if"
IL_000E:  starg.s     01 
IL_0010:  ldarg.1     
IL_0011:  call        System.Console.WriteLine
IL_0016:  nop         
IL_0017:  ret         

FixNullArg_TernaryOp:
IL_0000:  nop         
IL_0001:  ldarg.1     
IL_0002:  brfalse.s   IL_0007
IL_0004:  ldarg.1     
IL_0005:  br.s        IL_000C
IL_0007:  ldstr       "Fix null via ternary operator"
IL_000C:  starg.s     01 
IL_000E:  ldarg.1     
IL_000F:  call        System.Console.WriteLine
IL_0014:  nop         
IL_0015:  ret         

FixNullArg_NullCoalescingOp:
IL_0000:  nop         
IL_0001:  ldarg.1     
IL_0002:  dup         
IL_0003:  brtrue.s    IL_000B
IL_0005:  pop         
IL_0006:  ldstr       "Fix null via ?? operator"
IL_000B:  starg.s     01 
IL_000D:  ldarg.1     
IL_000E:  call        System.Console.WriteLine
IL_0013:  nop         
IL_0014:  ret         

FixNullArg_NullCoalescingAssignmentOp:
IL_0000:  nop         
IL_0001:  ldarg.1     
IL_0002:  brtrue.s    IL_000B
IL_0004:  ldstr       "Fix null via ??= operator"
IL_0009:  starg.s     01 
IL_000B:  ldarg.1     
IL_000C:  call        System.Console.WriteLine
IL_0011:  nop         
IL_0012:  ret         

FixNullArg_IsNullOrWhiteSpace:
IL_0000:  nop         
IL_0001:  ldarg.1     
IL_0002:  call        System.String.IsNullOrWhiteSpace
IL_0007:  brtrue.s    IL_000C
IL_0009:  ldarg.1     
IL_000A:  br.s        IL_0011
IL_000C:  ldstr       "Fix null via IsNullOrWhiteSpace"
IL_0011:  starg.s     01 
IL_0013:  ldarg.1     
IL_0014:  call        System.Console.WriteLine
IL_0019:  nop         
IL_001A:  ret         
I'm sticking with the ??= operator for simple null tests and ternary when more involved checks are needed (e.g. see if a string is null, empty or just whitespace).

modified 5-May-21 13:19pm.

GeneralRe: Cosmetic vs More Efficient Pin
SeattleC++5-May-21 5:57
SeattleC++5-May-21 5:57 
GeneralRe: Cosmetic vs More Efficient Pin
JP Reyes5-May-21 6:35
JP Reyes5-May-21 6:35 
GeneralRe: Cosmetic vs More Efficient Pin
Zuoliu Ding5-May-21 8:08
Zuoliu Ding5-May-21 8:08 
GeneralRe: Cosmetic vs More Efficient Pin
W Balboos, GHB5-May-21 8:33
W Balboos, GHB5-May-21 8:33 
GeneralRe: Cosmetic vs More Efficient Pin
Zuoliu Ding5-May-21 8:44
Zuoliu Ding5-May-21 8:44 
GeneralRe: Cosmetic vs More Efficient Pin
Myron Dombrowski5-May-21 19:43
Myron Dombrowski5-May-21 19:43 
GeneralRe: Cosmetic vs More Efficient Pin
W Balboos, GHB6-May-21 1:44
W Balboos, GHB6-May-21 1:44 
GeneralRe: Cosmetic vs More Efficient Pin
Eddy Vluggen6-May-21 9:10
professionalEddy Vluggen6-May-21 9:10 
GeneralRe: Cosmetic vs More Efficient Pin
Gerry Schmitz7-May-21 14:01
mveGerry Schmitz7-May-21 14:01 
GeneralMay the 4th be with you. Pin
AnotherKen4-May-21 5:38
professionalAnotherKen4-May-21 5:38 
GeneralRe: May the 4th be with you. Pin
OriginalGriff4-May-21 6:04
mveOriginalGriff4-May-21 6:04 
GeneralRe: May the 4th be with you. Pin
The Other John Ingram4-May-21 19:41
The Other John Ingram4-May-21 19:41 
GeneralRe: May the 4th be with you. Pin
jeron15-May-21 7:40
jeron15-May-21 7:40 
GeneralThought of the Day Pin
OriginalGriff4-May-21 4:31
mveOriginalGriff4-May-21 4:31 
GeneralRe: Thought of the Day Pin
W Balboos, GHB4-May-21 6:32
W Balboos, GHB4-May-21 6:32 
GeneralRe: Thought of the Day Pin
Daniel Pfeffer4-May-21 7:37
professionalDaniel Pfeffer4-May-21 7:37 
GeneralRe: Thought of the Day Pin
AnotherKen4-May-21 8:02
professionalAnotherKen4-May-21 8:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.