Allow new conditions to be added without modifying code

5

I'm manipulating a .txt and I have to change values of type M31 to T90 for example.

My code is like this:

 //Change machine tools for Sodick
                        if (_strLinesFinal.Contains("M50"))
                        {
                            _OS = _OS.Replace("M50", "T90");
                        }

                        if (_strLinesFinal.Contains("M60"))
                        {
                            _OS = _OS.Replace("M60", "T91");
                        }

                        if (_strLinesFinal.Contains("S555"))
                        {
                            _OS = _OS.Replace("S555", "C100");
                        }

The problem is that I'll have to always add a if to each code I want to change. I wanted something more practical, maybe put in a app.config , for example. How can I do this without having to always add a condition for code exchange?

    
asked by anonymous 11.10.2016 / 14:08

2 answers

7

This is not a problem, but you're right that it can be tricky to maintain.

Some people like to do something more "object oriented". So I would have a method for each case, but I think it's an exaggeration.

You have to analyze the specific case and I do not have the information at all. I do not know who can add something. Can you let the user do this? Even if you are a more privileged user. Or do you need to pass the programmer's approval before putting this in?

Is there no exception if the action to be taken is different from the one in the example?

If it is really common to add new items and this is something that the user can do, the correct thing is to make a registration of equivalence codes. It would have a table in the database with the code found and the equivalent to be replaced. There it makes a screen for someone to update that. Maybe just for someone privileged. Below I show you how to do the action shown in the question.

If the programmer has to be aware of this new code, I see no problem doing this in the code.

If you want to avoid both if you can make a list or dictionary with the codes and make a loop. It would look something like this:

foreach (var item in codigos) {
    if (_strLinesFinal.Contains(item.Key)) {
        _OS = _OS.Replace(item.Key, item.Value);
    }
}

To initialize the dictionary would look something like this:

var codigos = new Dictionary {
    ["M50"] = "T90",
    ["M60"] = "T91",
    ["S555"] = "C100", };

If it is taken from a database instead of these equivalent codes it is in the application code, it will be created by reading the database. You can even use the database directly and create the dictionary. It would have to change something, but it works. Depends on the case. I would probably do the cache in the dictionary. It depends on the database technology you're using, but it would look something like this:

var codigos = new Dictionary<string, string>();
using (var cmd = new SqlCommand("select codigo, equivalencia from codigos", dbConn)) {
    using (var reader = cmd.ExecuteReader()) {
        while (reader.Read()) {
            codigos[(string)reader["codigo"]] = (string)reader["equivalencia"];
        }
    }
}

But if you have actions other than just giving Replace() then if will be necessary.

    
11.10.2016 / 14:43
-2

Is there a standard for replacements? From what I've seen there may be a default in the case of MXX for TXX. In this case, the TXX has increment of 1 for every ten added. If this is the case I recommend creating a pattern mapping function, it will leave the code leaner and with fewer entries in the dictionary.

    
13.10.2016 / 03:01