How can I refactor a set of ugly if statements?

Funky

I have this set of If statements which work fine but just looks a little ugly. Is there any way I can tart it up instead of using big ole' ugly if statements? The problem is that it uses string.contains which makes it a bit difficult to implement a dictionary, which I tried and failed :(

Here it is:

foreach (var Item in Stuff)
            {
                var loweredQuoteItem = quoteItem.Name.ToLower();

                if (loweredQuoteItem.Contains("one"))
                    Item.InsurerNameShort = "Item One";

                if (loweredQuoteItem.Contains("two"))
                    Item.InsurerNameShort = "Item Two";

                if (loweredQuoteItem.Contains("chaucer"))
                    Item.InsurerNameShort = "Chaucer";

                if (loweredQuoteItem.Contains("three"))
                    Item.InsurerNameShort = "Item Three";

                if (loweredQuoteItem.Contains("four"))
                    Item.InsurerNameShort = "Item Four";

                if (loweredQuoteItem.Contains("five"))
                    Item.InsurerNameShort = "Item Five";

               }
Simon Forsberg

The problem is that it uses string.contains which makes it a bit difficult to implement a dictionary, which I tried and failed :(

Then I guess you implemented it incorrectly. Here's what you should do:

  • Construct a Dictionary.
  • Put "Item One" as value for key "one"
  • Put "Item Two" as value for key "two"
  • Put "Item Two" as value for key "chaucer"
  • etc...
  • Inside your current loop, once you have the loweredQuoteItem, loop over your Dictionary.
    • If dictionary contains innerLoopKey, set Item.InsurerNameShort to innerLoopValue (and optionally break)

Make sure to construct this Dictionary outside of your foreach (var Item in Stuff) loop, for better performance.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I efficiently refactor SQLite access in Java?

From Dev

How can I refactor these two lines to one statement?

From Dev

How can I tell PHPStorm to refactor namespaces and class names?

From Dev

How can I refactor this code that finds the end of Unix time?

From Dev

How can I refactor these simple functions and make them more DRY?

From Dev

How to refactor long if/return statements in Ruby?

From Dev

How can I remove/refactor a «friend» dependency declaration properly?

From Dev

How do I refactor out GOTO statements in GWBASIC code?

From Dev

How can I use ESLint to refactor/restyle code in webstorm

From Dev

How can I refactor a simple class member to a more complex type?

From Dev

How can I refactor this code to be more concise?

From Dev

Ugly fonts in Netbeans - How can i make it use the system font?

From Dev

How can I refactor my kotlin code and make it more clear

From Dev

Ugly fonts in Netbeans - How can i make it use the system font?

From Dev

How to refactor these if else statements to a function

From Dev

How can I fix the ugly fonts in java applications?

From Dev

How can I refactor / make this code more pythonic?

From Dev

How can I efficiently refactor SQLite access in Java?

From Dev

How can I tell PHPStorm to refactor namespaces and class names?

From Dev

How can I refactor these simple functions and make them more DRY?

From Dev

How to refactor long if/return statements in Ruby?

From Dev

Refactor an ugly loop into LINQ

From Dev

How can I use the Factory pattern to refactor my Java code?

From Dev

How can I refactor this method using anonymous projection to be more generic?

From Dev

How can I remove the ugly applet icon background? [Lubuntu]

From Dev

How can i refactor these lines of code?

From Dev

How to refactor consecutive, independent, if statements in this algorithm?

From Dev

How Can I Refactor To Avoid Duplication?

From Dev

How to refactor logging statements in sibling classes?

Related Related

  1. 1

    How can I efficiently refactor SQLite access in Java?

  2. 2

    How can I refactor these two lines to one statement?

  3. 3

    How can I tell PHPStorm to refactor namespaces and class names?

  4. 4

    How can I refactor this code that finds the end of Unix time?

  5. 5

    How can I refactor these simple functions and make them more DRY?

  6. 6

    How to refactor long if/return statements in Ruby?

  7. 7

    How can I remove/refactor a «friend» dependency declaration properly?

  8. 8

    How do I refactor out GOTO statements in GWBASIC code?

  9. 9

    How can I use ESLint to refactor/restyle code in webstorm

  10. 10

    How can I refactor a simple class member to a more complex type?

  11. 11

    How can I refactor this code to be more concise?

  12. 12

    Ugly fonts in Netbeans - How can i make it use the system font?

  13. 13

    How can I refactor my kotlin code and make it more clear

  14. 14

    Ugly fonts in Netbeans - How can i make it use the system font?

  15. 15

    How to refactor these if else statements to a function

  16. 16

    How can I fix the ugly fonts in java applications?

  17. 17

    How can I refactor / make this code more pythonic?

  18. 18

    How can I efficiently refactor SQLite access in Java?

  19. 19

    How can I tell PHPStorm to refactor namespaces and class names?

  20. 20

    How can I refactor these simple functions and make them more DRY?

  21. 21

    How to refactor long if/return statements in Ruby?

  22. 22

    Refactor an ugly loop into LINQ

  23. 23

    How can I use the Factory pattern to refactor my Java code?

  24. 24

    How can I refactor this method using anonymous projection to be more generic?

  25. 25

    How can I remove the ugly applet icon background? [Lubuntu]

  26. 26

    How can i refactor these lines of code?

  27. 27

    How to refactor consecutive, independent, if statements in this algorithm?

  28. 28

    How Can I Refactor To Avoid Duplication?

  29. 29

    How to refactor logging statements in sibling classes?

HotTag

Archive