E
E
Eugene Chefranov2020-05-26 21:35:35
C++ / C#
Eugene Chefranov, 2020-05-26 21:35:35

How to shorten a condition?

There are several conditions where one of several names is checked for a match + the current temperature

if ((name == "SiO2 (β-кварц)" || name == "SiO2 (α-кварц)" || name == "SiO2 (α-тридимит)" || name == "SiO2 (β-кристобалит)" || name == "SiO2(к) кварц" || name == "SiO2(к) тридимит" || name == "SiO2(к) кристобалит" || name == "SiO2 (полиморф.)") && curTemp <= 846.15)
    {
        h = -217.75F; s = 10F; a = 11.22F; b = 8.2F; c = -2.7F;
    } 
    else if((name == "SiO2 (β-кварц)" || name == "SiO2 (α-кварц)" || name == "SiO2 (α-тридимит)" || name == "SiO2 (β-кристобалит)" || name == "SiO2(к) кварц" || name == "SiO2(к) тридимит" || name == "SiO2(к) кристобалит" || name == "SiO2 (полиморф.)") && curTemp >= 846.15)
    {
        h = -217.6F; s = 0F; a = 14.41F; b = 1.94F; c = 0F;
    }
    else if ((name == "SiO2 (β-кварц)" || name == "SiO2 (α-кварц)" || name == "SiO2 (α-тридимит)" || name == "SiO2 (β-кристобалит)" || name == "SiO2(к) кварц" || name == "SiO2(к) тридимит" || name == "SiO2(к) кристобалит" || name == "SiO2 (полиморф.)") && curTemp >= 1143.15)
    {
        h = -216.5F; s = 10.4F; a = 13.64F; b = 2.64F; c = 0F;
    }
    else if ((name == "SiO2 (β-кварц)" || name == "SiO2 (α-кварц)" || name == "SiO2 (α-тридимит)" || name == "SiO2 (β-кристобалит)" || name == "SiO2(к) кварц" || name == "SiO2(к) тридимит" || name == "SiO2(к) кристобалит" || name == "SiO2 (полиморф.)") && curTemp >= 1743.15)
    {
        h = -215.95F; s = 10.19F; a = 4.28F; b = 21.06F; c = 0F;
    }

Is it possible to somehow enter a list of names, for example, into an array (or something else) and from there take one at a time and compare? And now it looks very cumbersome and not readable.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
ayazer, 2020-05-26
@Chefranov

private static string[] _elementNames = new[] { "SiO2 (β-кварц)",  "and", "more"};

....

if (_elementNames.Contains(name))
{
    if (curTemp <= 846.15)
    {
        h = -217.75F; 
        s = 10F;
        a = 11.22F; 
        b = 8.2F; 
        c = -2.7F;
    }
    else if (....) { ....}
    ....
}

if you need to make this particular piece more readable, you can do something like this

D
d-stream, 2020-05-26
@d-stream

It suggests Dictionary https://docs.microsoft.com/ru-ru/dotnet/api/system...
where the key will be name, and the value will be a primitive class/structure of 4 fields
, and then the initialization of the dictionary will be placed in a separate block (and for example it will be possible to load it from storage)
and search:
h=Dic[""SiO2 (β-quartz)""].h;
s=Dic[""SiO2 (β-quartz)""].s;
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question