Is it right to use string when integer value?

0

Today I came across a certain situation, which left me with doubts:

In my application I have some ID's coming from a table, these ID's are integer values (1,2,3, and so on) and all I treat them as string .

One of the reasons I do this is to not use the method: Convert.Toint and I end up using the method: .ToString()

I will not perform any math operation, it is only for primary key.

These ID's are obtained from a GridView populated by a DataTable which in turn has the data coming from the DB, these ID's are generated by the bank by a column with Identity so we will never have values duplicates.

I also do not care if the value is first, last, which is next, it's just to identify the delete action of the registry.

This treatment is correct? what is the form indicated in these cases?

    
asked by anonymous 14.10.2016 / 16:46

2 answers

4

There is no right or wrong right in this case. There is the indicated and the most efficient. If the fields are integers, treat them as such. It may seem that there is nothing stopping you from using the integers converted to string in this situation, but there is a "rule" in system development that I follow very successfully: Do the less surprising thing. Always.

Amazingly, in this case, it should be read as "something that begets less surprise".

In this case, if the field is integer (or long ) in the database, treat it as such in your code as well. This will generate less surprise to some other programmer, or even yourself when you come to fix a bug in the code in a few years. In addition, using Convert.ToInt32() instead of ToString() will not make any difference, but just the fact that you wonder if you treat the integer as string in your code is right, already shows that the ideal would not even need to ask that question and treat integer as the integer that is.

I also guarantee that in%% of% this field is integer. If it compares the ID of the GridView with the id in the dataset / datatable, it may have to convert back to integer from any

There are even future features that might be affected by this unnecessary conversion made today. For example, you may need to put the Ids inside a DataTable and if they are strings the operations will be slower (not much, since Hashset uses a hash to be found, but there is a loss of performance). You may want to sort the Ids to know what was created before in the database (since it is identity) and the sorting of strings, in addition to much slower, does not follow the numeric order: Hashset while "10" < "2" .

In short: while there are reasons to use integer as an integer, with advantages, there are no advantages or reasons (in your case) to treat them as string.

Do not preach parts yourself (and others who use your code). Do what you generate the least surprise. Ask yourself: What is the most anticipated? Is an integer field in the database treated as a string in your code, or is it treated as an integer?

    
14.10.2016 / 17:38
2

Although you do not use IDs for mathematical operations, you certainly make comparisons, especially when searching for data in your bank. Thinking about performance, the best practice is still to use numbers, because comparisons can be made in one cycle, whereas comparing strings requires that each character be compared (unless you convert to numbers). With respect to your concern of calling ToString repeatedly, you can rest easy, as this method is very fast and optimized at CLR level, so it will never be the bottleneck in any application. What could be a problem and boxing and unboxing of values, ie letting C # do the work for you.

For example, the code

for(int i = 0; i < 50; i++) {
    sb.Append(i.ToString()); 
    sb.Append(",");
}

is faster than

StringBuilder sb = new StringBuilder();
for(int i = 0; i < 50; i++) 
    sb.Append (i + ",");

Just because the second requires extra boxing and unboxing operations.

In this way, it makes more sense to worry about the efficiency of the search in your bank and other comparisons between the IDs than with the ToString, which means that int would usually be a better choice.

    
14.10.2016 / 17:45