Method Design - Clarity or Multifunctionality

I created a class in both Java and C # that allows me to execute SQL queries, as an example I have a method called "Delete" that takes several parameters;

public static int Delete(String table, String column, String operand, Object value) 

I have a value as an object type, since I can delete strings based on String, Integer or booleans, this means that the method is flexible enough to support different types. Then I add additional " " "characters to the request depending on whether it is a string or not using the instanceof test in Java (or .GetType in C #)

Example:

 if (value instanceof String) { System.out.println("It a String"); } else { System.out.println("It not a String"); } 

In implementing the rest of the class, I started thinking about myself whether the previously mentioned method is ideal, or should I use additional methods for specific data types, one to place String, another to Integer, etc.

If I started to implement this, this would mean that there will be additional methods in the logic with a minimal difference, however, each method has only one goal, simplifying their passage and documentation. On the other hand, if I save it like this, that is, there is much less code to be created and maintained, it can control any type of Delete instruction (in terms of data types), and there should only be a few if statements to determine the type of object, which was passed through the parameter.

What is the best approach to follow in terms of object oriented / best code practices?

Thanks for the information!

+2
source share
2 answers

None.

You must use parameterized queries.

You're right; You must save it in one way.
In C #, it is sometimes useful to make such methods generalized.

+5
source

Throwing away the unanswered question of whether this architecture is a good idea ...

As a rule, object-oriented code gets better, removing as much as possible explicit type checking. This will mean that to the extent that it matters which type is provided as a parameter, a version of the code with type overloads might be better. This is only an improvement, however, if the type of the parameter is known at compile time, of course, since this happens when overload resolution is enabled!

In addition, in the C # version, method overloads will avoid boxing value types.

Another (in this case), possibly contradictory rule, is that duplicate code should be removed as much as possible. This means that the best way to do this may be with one method. In this case, I would recommend processing the type code for other methods ( string DelimitValueIfNecessary(object) ), since such things are not the main competence of the method that creates the delete statements.

I think this second rule is more important than the first, so I would choose one method.

Now, to move on to the incomprehensible question about this architect: this is a terrible idea for a number of reasons, not limited to the following: SQL injection attacks, close interaction of object types and data models, inefficiency, leaky abstractions, etc.

+2
source

Source: https://habr.com/ru/post/1345935/


All Articles