Your comments

Если вы про паттерны, то работа в процессе, всё будет.

Thanks for reporting this. It's rather a bug in example. We'll upload the fix later this week.

Добрый день!


Спасибо за ваш отзыв. Не могли бы вы уточнить какое именно тестирование имеете в виду?

Добрый день!


Проблема исправлена, извините за неудобства. Почистите кеш браузера, если не заметите изменений.

Here's analogy: you work on the 20th floor, but the dining room is on 3rd floor. It's fine to take a 2-minute elevator ride once a day on a lunch break. However, if the only coffee machine is also there, you will have to spend hours riding back and forth each time you commit your code.


Jokes aside, here are 2 stackoverflow threads, where you can get in-depth answer:

http://stackoverflow.com/questions/224232/what-is-the-cost-of-net-reflection

http://stackoverflow.com/questions/25458/how-costly-is-net-reflection

As with all things in programming you have to balance performance cost with with any benefit gained. But I'd say if you're not running this code in a loop, don't worry about the performance.

Sorry, C# is not my "native" programming language, but as far as I know, C# has almost the same reflection capabilities as Java. It should be easy to convert the code above to C# by looking at examples at MSDN:

https://msdn.microsoft.com/en-us/library/system.reflection.fieldinfo.getvalue(v=vs.110).aspx

for instance:

PropertyName.class.getField(fieldName) will become typeof(PropertyName).GetField(fieldName). And attributeField.get(PropertyName).toString() will probably become attribute.getValue(PropertyName). Check the MSDN article for the list of exceptions you need to handle while doing this.


By the way, you're right if you think that we created more code than we saved. Sometimes it's better to just leave things the way the are.


However, that is the solution for growing number of sub-ifs. You can easily put more fields to compare, but the nesting level won't change.

I would try to extract a new private method or local function for comparing fields by names.


Assuming your example was in Java, I would aim for something like this:


    public void someBusinessLogicMethod() {
        // Define and intialize obj, objToFind, PropertyName
        // ...
        String[] fieldsToCompare = ["Name", "Role", "Value", "Class", "Top", "Left", "Height", "Width"];
        if (this.compareGivenFields(obj, objToFind, PropertyName, fieldsToCompare)) {
           // Some code.
        }
    }
    private Boolean compareGivenFields(Object obj, Object objToFind, Object PropertyName, String[] fieldNames) {
        Boolean result = false;
        for (String fieldName : fieldNames) {
            result &= compareField(obj, objToFind, PropertyName, fieldName);
        }
        return result;
    }
    private Boolean compareField(Object obj, Object objToFind, Object PropertyName, String fieldName) {
        try {
            Field attributeField = PropertyName.class.getField(fieldName);
            try {
                String attribute = attributeField.get(PropertyName).toString();
                return obj.Attributes[attribute].Equals(objToFind.Attributes[attribute]);
            } catch (IllegalAccessException e) {
                System.out.println("Error accessing field value.");
            }
        } catch(NoSuchFieldException e) {
            System.out.println("There's no such field.");
        }
        return false;
    }


In other languages which support direct field access by name (PHP, Javascript) solution might be even simpler.