Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

One, this may be true

In fact, it does not represent the summary of this month, a part of the fact is quite a long period of time, so record.

1. Why bugs

Just myself according to the years of development experience and experience and do the project, if once the program is a problem, whether from the point of view of responsibility or solve the problem, from design development > > test > d > > implementation until the user, the first is always worship the developer, because what is you really do, designers will say has been made clear to you, and operations Testers will say who knows what you write, you can’t detect bugs, and eventually even implement it and users will despise you for “what the fuck is junk software”.

First of all I have to clarify my attitude, this is not for who, because everyone is not easy, and very hard, some views may not be easy to be recognized or accepted, but it is the way it is, because this thing is see people, hu hu, if someone really care about, can treat as a fun to see. Although the reality is so helpless, but we must face it, because only by analyzing the problem, can we see the essence more clearly, so as to get real improvement. Here, as a development summary, there are the following major reasons for producing bugs:

1. Vague needs

Sometimes there is a demand, and did not carefully to read the demand, and did not carefully and put forward the demand of the demand and reverse disclosure, because in the general development of cognition, writing demand is not understand the demand of the people, plus in the countless “this is which XX demand?” “And” Why?” , “xx” true tm

After a long time of grinding, resulting in most of the reading demand is just a quick scan, scanning process only pick some sensitive words, and then fully take for granted, over time to develop a preconceived habit, and then make the demand is…… Well, I can’t imagine.

How to solve it?

Must read and check carefully objective demand by the user, and then reverse clarificaiton independent analysis, even if it is a very abstract figure, or a few lines of words, because of what you said may be made clear at the time you just think yourself, just not everyone can see what you mean, or you didn’t say clear, it doesn’t matter even if takes more time, Grinder does not mistakenly cut wood work, if possible pull on the test, as for why, that is to avoid you after finishing the follow-up, and then speak second, third, understand all understand.

2. You have extraordinary confidence

I think as development self recognition of the standard and write code that is not more than 6, how fast solution Bug do demand, but the confidence of fan, the SAO is not accept to retort, finish is measured, not the unit test, even basic over all have no, exactly that brought a lot of disadvantages, the ideal is the problem in the testing phase was found, and then a bunch of Bug, is not very ideal The situation is that the test did not detect, and then detected by the user…

How to solve it?

1. After writing, go through CodeReview as a whole, usually find some writing and design defects and modify them in time, and then test at least three times, three times, three times.

2. For large functional modules, write test cases for development versions so that you can better conduct unit tests.

3. Do not reply in time

Why is it necessary to resume in time? You can use tips from daily development, or typical bugs and solutions that have been solved, such as common ones

The object is not referenced to the instance. 2. The parameter length exceeds the range that the database can store. 3. The index is out of bounds. 4. A special service needs special processing.

Write it all down and review it periodically or irregularly, or after a new requirement is completed, use the record as a standard check to make sure that any mistakes made or problems resolved don’t reappear.

4. Don’t do things emotionally

Everyone has their own personality and temper, you can disapprove of everyone, you can think everyone is rubbish, but don’t show your emotions at work, it will have a big impact on those around you, or on yourself, remember “put up with it or leave it”!

5. Tests are unreliable

Why said test unreliable, not specially for contempt or provoking the violence and said, because the test in most of the small company is directed at the lines of business, plus level also levels not neat, met a relatively rich experience, can write Sql or familiar with most of the test tool or performance test of you stealing happy, because I met some application cases are not writing I don’t know whether I can’t write, or it’s too much trouble, so I don’t know where I’m pointing.

What if it cannot be changed and must be fixed?

1. Improve code robustness, reduce bugs and minimize problem risk.

2. A change of scenery might be better, but it might be worse…

2. Abnormal information

I made an exception notification requirement before, which simply means that the program executes an exception and sends an email and notification to the designated person. My approach is also very simple, and I send the caught exception in catch by using exception capture.

After the launch, some colleagues said that there was an anomaly and I didn’t send it. I also don’t know if it was true that there was an anomaly and I didn’t send it, because I passed the test for many times before, holding a skeptical attitude to analyze the code in combination with the log, and later I found it was indeed the case.

The code is as follows:

public Task Execute() { try { CalculationRawData(); } catch (Exception ex) { PushException? .Invoke(this, new ExceptionResponse(Company, nameof(CalculationJob),ex)); } return Task.FromResult(0); }Copy the code
public void CalculationRawData() { try { //dosomthing } catch (Exception ex) { Logger.Writelog("ex.InnerException.Message"); }}Copy the code

Why wasn't it sent?

According to the analysis of the above two codes, although the exception is captured in CalculationRawData method, it is not thrown, so Execute does not receive the exception and will not call the sending interface naturally. Therefore, only one unified entry is used to handle exceptions in daily development. In business, exceptions should not be handled as far as possible, because bad handling will lead to the real useful problem information being swallowed and increase the cost of investigation.

Implicit and explicit

The implicit keyword is used to declare implicit user-defined type conversion operators. You can use this keyword to implicitly convert between user-defined types and other types if you can ensure that the conversion process will not result in data loss.

The explicit keyword declaration must be converted by invoking the explicit user-defined conversion operator.

1.implicit
Public struct TimeRange {public DateTime StartTime {get; set; } public DateTime EndTime { get; set; } public TimeRange(DateTime startTime, DateTime endTime) { StartTime = startTime; EndTime = endTime; } public static implicit operator double(TimeRange timeRange) { return (timeRange.EndTime - timeRange.StartTime).TotalHours; ; }}Copy the code
static void Main(string[] args) { DateTime star = DateTime.Now; DateTime end = DateTime.Now.AddDays(1); Double result = new TimeRange(star, end); double result = new TimeRange(star, end); }Copy the code
2.explicit
static void Main(string[] args) { DateTime star = DateTime.Now; DateTime end = DateTime.Now.AddDays(1); Double result = (double)new TimeRange(star, end); }Copy the code

Four, fast conversion

1. Quickly convert an Int to a String
Int [] arr = new int[] {4,2, 4,6}; string[] result = Array.ConvertAll(arr, Convert.ToString);Copy the code
List<string> = List<int>
 List<string> list = new List<string>() { "1","2","3","1"};
 var result = list.ConvertAll(int.Parse);
Copy the code