Code analysis for an early-entry new employee (2)

Source: Internet
Author: User

The header file definition of a class is analyzed in the previous article. A general function of the class can be seen from the definition of a file, and it is possible to analyze whether the class is designed reasonably. Other types of coding specifications, function parameters, return values, etc. can also be observed. In the design of the class, it is important to be fully clear about the duties of the class, not in the scope of such responsibilities, do not arbitrarily let this class to deal with, maintain the independence of the class, reduce the coupling degree. Many new people on how to design the class is very vague, in many cases, the class design is unreasonable, which requires gradual accumulation of experience, improve their own.

Today, the function body is analyzed and the function body is analyzed to find out the problem and perfect its code.

void cstudentlist::initializestudentlist (int nsize,cstudent *student) {m_head=new studentnode;if (M_head!=NULL) {m_ Head->m_student=student[0];m_head->next=null;} Studentnode *prenode=null; prenode=m_head;for (int i=1;i<nsize;i++) {Studentnode *newstudentnode=null; Newstudentnode=new studentnode;if (newstudentnode!=null) {newstudentnode->m_student=student[i]; newstudentnode->next=prenode->next; Prenode->next=newstudentnode;} Prenode=prenode->next;}}

Please


First


Think


Test


No


Xu


Steal


See




Well, have you already made your own judgments? So let's analyze it.

First of all,m_head=new studentnode has a problem, if the m_head is not currently empty, then there is no memory leak it? So, before this sentence, you should clear the list.

Second,m_head->m_student=student[0]; also have a problem, OK Student This parameter is not empty? Should first check that the code should have a sense of self-protection, improve the robustness of the program .

In addition, since if (m_head!=null) is judged, then why not consider when the condition is not established? Once this is not true, because M_head is empty, all corresponding pointer processing in subsequent code will cause a crash. Similarly, the IF (newstudentnode!=null) in subsequent code has the same problem.

Through the analysis of this function, let us realize that writing code can not only see the ideal side, more to consider the various exceptions, to ensure that your program can withstand the test. Hopefully it will get something new and write your own strong code.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Code analysis for an early-entry new employee (2)

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.