Home > Enterprise >  if condition is true add flag EnableDebugging without the else
if condition is true add flag EnableDebugging without the else

Time:12-09

I need to check if odcSettings.EnableDebugging is true and add a script flag (V8ScriptEngineFlags.EnableDebugging) but if it is false don't add the flag. I want to do this without the else part or rather in a single line.

 if (odcSettings.EnableDebugging == "true")
            {
                if (string.Equals(scriptLanguage, "javascript.v8", StringComparison.OrdinalIgnoreCase))
                {
                    return new V8ScriptEngine(scriptName, V8ScriptEngineFlags.EnableStringifyEnhancements | V8ScriptEngineFlags.EnableDebugging | V8ScriptEngineFlags.UseCaseInsensitiveMemberBinding | V8ScriptEngineFlags.EnableDateTimeConversion);
                }

                if (string.Equals(scriptLanguage, "javascript", StringComparison.OrdinalIgnoreCase))
                {
                    return new JScriptEngine(scriptName, WindowsScriptEngineFlags.MarshalDateTimeAsDate | WindowsScriptEngineFlags.EnableDebugging);
                }

                return new VBScriptEngine(scriptName, WindowsScriptEngineFlags.EnableDebugging);
            }
            else
            {
                if (string.Equals(scriptLanguage, "javascript.v8", StringComparison.OrdinalIgnoreCase))
                {
                    return new V8ScriptEngine(scriptName, V8ScriptEngineFlags.EnableStringifyEnhancements | V8ScriptEngineFlags.UseCaseInsensitiveMemberBinding | V8ScriptEngineFlags.EnableDateTimeConversion);
                }

                if (string.Equals(scriptLanguage, "javascript", StringComparison.OrdinalIgnoreCase))
                {
                    return new JScriptEngine(scriptName, WindowsScriptEngineFlags.MarshalDateTimeAsDate);
                }
            }  //need it to be a single line change instead of an if and else 

CodePudding user response:

This is not exactly what you are asking for, but I will share the thought nonetheless.

You could consider inverting the logic a little bit by handling the "javascript.v8" scenario, the "javascript" scenario and the remaining scenarios separately. The appropriate measure regarding 'enable debugging' will then be made separately for each scenario, seeing as the conditional flag you want to apply varies between the scenarios (i.e. V8ScriptEngineFlags.EnableDebugging vs. WindowsScriptEngineFlags.EnableDebugging).

With such an approach, you set the flags only once, apply the relevant 'enable debugging' flag if needed, and then do the appropriate engine call.

Here is a handwritten implementation to illustrate the goal:

var enableDebugging = odcSettings.EnableDebugging == "true";

var isJavascriptV8 =
    string.Equals(scriptLanguage, "javascript.v8", StringComparison.OrdinalIgnoreCase);

var isJavascript =
    string.Equals(scriptLanguage, "javascript", StringComparison.OrdinalIgnoreCase);

if (isJavascriptV8)
{
    var flag = V8ScriptEngineFlags.EnableStringifyEnhancements |
               V8ScriptEngineFlags.UseCaseInsensitiveMemberBinding |
               V8ScriptEngineFlags.EnableDateTimeConversion;
    
    if (enableDebugging)
    {
        flag |= V8ScriptEngineFlags.EnableDebugging;
    }
    
    return new V8ScriptEngine(scriptName, flag);
}

if (isJavascript)
{
    var flag = WindowsScriptEngineFlags.MarshalDateTimeAsDate;
    
    if (enableDebugging)
    {
        flag |= WindowsScriptEngineFlags.EnableDebugging;
    }
    
    return new JScriptEngine(scriptName, flag);
}

if (enableDebugging)
{
    return new VBScriptEngine(scriptName, WindowsScriptEngineFlags.EnableDebugging);
}
  • Related