Optimized bug: use static with caution

Source: Internet
Author: User

These days I have been dizzy and dizzy, my lunch break has been canceled, and my smoking time has been busy. I even have to go to the bathroom and think about my work. I can't help IT, when there are many things, 24 hours a day is not enough, it must be 25 hours a day. But the days don't follow our ideas. We can only go through the days!

 

It is easy to make mistakes when you are busy. This is not a cainiao-level mistake, so that colleagues can have a good time. This is the case. An algorithm in the project needs to be optimized. During Code review, it is found that many databases are requested during initialization, however, some of the data is configuration-type data (usually less frequently changed data is called configuration-type data), and the quantity is not large, so I thought of using static structures to save this part of data, to reduce the number of database requests. Thus, during the optimization, the original local variables were changed to Global static variables and constructed once in the static constructor. As a result, the problem came out, which also caused me to be too eager for immediate success, because there are many things, I don't have to worry about it. As a result, I suffered a big loss.

Speaking of this, the old birds who read this article must have understood the reason (I believe there are also a lot of people who can guess a hundred birds without a dozen ). Don't talk about it, "Cuihua, code !"

Public class StaticBug
{
Static Dictionary <int, Bug> BugDictionary = new Dictionary <int, Bug> ();
Static StaticBug ()
{
InitBugDictionary ();
}

Private Bug ErrorGetBug (int bugId)
{
// Incorrect method.
Bug bug = null;
If (BugDictionary. ContainsKey (bugId ))
{
Bug = BugDictionary [bugId];
If (bug! = Null)
{
Bug. BugCount ++; // you do not want to change the BugCount attribute value of the Bug object in BugDictionary.
}
}
Else
{
InitBugDictionary ();
If (BugDictionary. ContainsKey (bugId ))
{
Bug = BugDictionary [bugId];
If (bug! = Null)
{
Bug. BugCount ++; // you do not want to change the BugCount attribute value of the Bug object in BugDictionary.
}
}
}
Return bug;
}
}

Public class Bug
{

Public int BugId {get; set ;}
Public string BugTitle {get; set ;}
Public string BugContents {get; set ;}
Public int BugCount {get; set ;}

Public static Dictionary <int, Bug> GetBugs ()
{
// Do some thing to get bugs.
Return new Dictionary <int, Bug> ();
}
}

 

You can see the error operation on the bug variable in the code. If you do not want to change the BugCount attribute value of the Bug object in BugDictionary, the correct code should be:

Private Bug RightGetBug (int bugId)
{
Bug bug = null;
Bug tempBug = new Bug ();
If (BugDictionary. ContainsKey (bugId ))
{
Bug = BugDictionary [bugId];
If (bug! = Null)
{
TempBug. BugId = bug. BugId;
TempBug. BugTitle = bug. BugTitle;
TempBug. BugContents = bug. BugContents;
TempBug. BugCount = bug. BugCount;
TempBug. BugCount ++;
}
}
Else
{
InitBugDictionary ();
If (BugDictionary. ContainsKey (bugId ))
{
Bug = BugDictionary [bugId];
If (bug! = Null)
{
TempBug. BugId = bug. BugId;
TempBug. BugTitle = bug. BugTitle;
TempBug. BugContents = bug. BugContents;
TempBug. BugCount = bug. BugCount;
TempBug. BugCount ++;
}
}
}
Return tempBug;
}

From the code above, we can see that a temporary Bug object is used to receive the attribute value of the bug, and then the BugCount ++ avoids modifying the BugDictionary.

 

I believe that many new users will encounter this error when using static, and I believe that many veterans like me will cause this error if they are not careful. The cause of the error is simple, but it is easy to eliminate the bug from more than 3 thousand lines of code! Therefore, be cautious when using static variables. In addition, you should be cautious when using the singleton mode. In comparison, the singleton mode is much easier to troubleshoot than I do.

 

I am writing this cainiao article, and I am eager to give some valuable comments to the older birds!

 

PS: I didn't expect a buzz! ----------------- PS2: Let me talk about my intention. In fact, I don't want to make any changes to the elements in BugDictionary. Well, that's right. It's still caused by the reference type. From this perspective, it's not related to static. But from the static point of view, if static is not used, this problem will not occur. I can directly retrieve the elements in the BugDictionary and return them after processing. Therefore, in general, this error is caused by my negligence when I use static and cannot modify the BugDictionary. I only want to be able to alert others! The comments are correct, but the points of view vary depending on the problem. ----------------- PS3: actually. net. net has two data types: Value Type and reference type, but why are there still many people frequently encountering exceptions that "object reference is not set to object instance? This is worth thinking. Although this is a low-level mistake, why do we always make these low-level mistakes frequently? Isn't it worth thinking about?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.