Problem reading c # code

int scalar = creature is SpecialCreature ? (creature.IsAwesome ? 700 : 500) : (creature is NotSoNormalCreature ? (creature.IsAwesome ? (creature is IGreatCreature ? 450 : 280) : 240) : (creature.IsAwesome ? (creature is IGreatCreature ? 300 : 200) : 160)); 

How should I write this code to make it more readable?

I thought about just building ifs, but then I thought about how to create a "ConditionFactory"? Will it make any sense, or is it too complicated for such a simple task?

 int scalar; if (creature is SpecialCreature) { scalar = creature.IsAwesome ? 700 : 500; } else if (creature is NotSoNormalCreature) { if (creature.IsAwesome) { scalar = creature is IGreatCreature ? 450 : 280; } else { scalar = 240; } } else { if (creature.IsAwesome) { scalar = creature is IGreatCreature ? 300 : 200; } else { scalar = 160; } } 
+4
source share
5 answers

Not sure what you are going to do, but since you are using the base type inheritance chain, you can choose something like

 interface ICreature { bool IsAwesome { get; set; } int GetScalar(); } abstract class Creature : ICreature { public bool IsAwesome { get; set; } public virtual int GetScalar() { return 160; } } class SpecialCreature : Creature { public override int GetScalar() { return this.IsAwesome ? 700 : 500; } } class NotSoNormalCreature : Creature { public override int GetScalar() { return this.IsAwesome ? 450 : 280; } } // more ICreatures... 

So that you can force a creature to implement its own logic to define a scalar, and your consumer code may lose the complexity of the care.

 ICreature creature = GetCreatureFromSomewhere(); int scalar = creature.GetScalar(); 
+8
source

This is not exactly what you need here, but I use the Extension method to implement this kind of chain method, when the condition can be resolved to Or or And's list.

Sort of

 if (true.IfOr(condition1 == a, condition2 == b) { something(); } 

The extension method is then quite simple:

 public static bool IfOr(this bool result, params bool[] tests) { foreach (bool test in tests) if (!test) return !result; return result; } 

Another way that may work, although it may not be very optimal, is to use the Predicate delegate in .net and define a list of methods that execute your individual units of logic. Then you can replace your nested tertiary operators with lambda. I have no sample code for this, though, sorry.

Finally, sometimes there is nothing better than the good old switch statement. I believe that .Net has a tendency to compile them as jump tables, so if you test the most divisible first, then you can really get some pretty efficient and readable code. And it is supported, rather than hiding logic or implementation with tricks.

+4
source

I think the real problem is that you are hard-coded "configuration data". If you say, you tear out these β€œsettings” and put them in the XML configuration file, and does this mess go away?

This, too, may seem redundant until you pick up your various configs to make the game more playable ... a separate FILE configuration allows you to easily play (and return).


EDIT:

By the way, I would format this nested ternary statement as shown below to make it more readable.

 int scalar = creature is SpecialCreature ? creature.IsAwesome ? 700 : 500 : creature is NotSoNormalCreature ? creature.IsAwesome ? creature is IGreatCreature ? 450 : 280 : 240 : creature.IsAwesome ? creature is IGreatCreature ? 300 : 200 : 160 ; 

Greetings. Whale.

0
source

This is how I redid the code and made it readable

 // Original code spread apart int scalar = creature is SpecialCreature ? ( creature.IsAwesome ? 700 : 500 ) : ( creature is NotSoNormalCreature ? ( creature.IsAwesome ? ( creature is IGreatCreature ? 450 : 280 ) : 240 ) : ( creature.IsAwesome ? ( creature is IGreatCreature ? 300 : 200 ) : 160 ) ); // Readable code with hybrid if() and ? : if (creature is SpecialCreature) { scalar = creature.IsAwesome ? 700 : 500; } else if (creature is NotSoNormalCreature) { if (creature.IsAwesome) { scalar = creature is IGreatCreature ? 450 : 280; } else { scalar = 240; } } else { if (creature.IsAwesome) { scalar = creature is IGreatCreature ? 300 : 200; } else { scalar = 160; } } 

I want to recommend moving this calculation within each class, if possible, with overrides for different branches.

0
source

How about the good old:

 if (creature is SpecialCreature) { scalar=getSpecialCreatureScalar(creature); } else if (creature is NotSoNormalCreature) { scalar=getNotSoNormalCreatureScalar(creature); } else { scalar=getScalar(creature); } 

.. And then

 int GetSpecialCreatureScalar(SpecialCreature creature) { return creature.IsAwesome ? 700 : 500; } int GetNotSoNormalCreatureScalar(NotSoNormalCreature creature) { if (creature.IsAwesome) { return creature is IGreatCreature ? 450 : 280; } else { return 240; } } int GetScalar(Creature creature) { if (creature.IsAwesome) { return creature is IGreatCreature ? 300 : 200; } else { return 160; } } 

.. Gives the value of if. Makes another IMO.

0
source

All Articles