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

Source: Internet
Author: User

The company just came to new employees, just graduated, after a few months of practice, completed a training coding practice. Through the reading of its code, some problems have been found. These problems are a relatively easy mistake for beginners, and they are not easy to find or understand in their current experience. This is a special interpretation and sharing, I hope you can inspire beginners, early to improve the level of coding.

Task is relatively simple, is to complete a student management system, now many college students themselves play such a system. Requires the use of lists, XML and other assessment points.

The following is a list of the Student list class header files that they write:

#include "Student.h"
#include "XMLWrapper.h"
struct studentnode
{
 cstudent m_student;
 Studentnode *next;
};
Class Cstudentlist
{public
:
cstudentlist ();
~cstudentlist ();
 Initialize student linked list
void initializestudentlist (int nsize,cstudent *student);
    void Initializestudentlist ();
 Add student
 void Addstudent (Cstudent Student);
Delete student
void deletestudent (CString studentid);
Empty student list
void Clearstudentlist ();
Find students
cstudent* selectstudent (CString strstudentid) by student number;
Find students in class
BOOL  selectcalssstudent (int *count,cstudent **student,cstring strmajor,cstring strgrade, CString strclass);
Save Student Information
BOOL savestudentinfomation (tixmlelement *rootelement,cstring strxml,cxmlwrapper *ptinyxml);
Check that the student list is equal to
BOOL checkstudentlist (cstudentlist *pstudentlist);
Private:
studentnode *m_head;
};
For this header file definition, beginners can see what the pros and cons are from their current experience. Then look at the following analysis, which may be more valuable.

Please
First
Think
Test
No
to be
Stealing
See
Well, here's an analysis of the deficiencies seen from this header file. 1. variable naming style is not uniform CString StudentID VS CString strStudentIDcstudent *student VS cstudent **student poor readability, it is difficult for readers to easily identify variable types through variable names, it is easy to confuse suggestion: Really do not know what to do, one is to find a code to learn a code, the second is to see the MFC bar.
2. variable name has no meaningStudentnode *m_head what M_head can represent. Cannot achieve the effect as the name implies. Recommendation: The best note is that you don't need to write a comment. No writing means that the code itself can be self-explanatory.
3. Function name has a word errorSelect CALSS Student class--class suggestion: Really not clear, you can look up the dictionary ah.
4. The return value type is not reasonablevoid initializestudentlist (int nsize,cstudent *student);void addstudent (cstudent Student); The operation is successful, how to let the user understand. Recommendation: Business functional operations, as far as possible to consider the retention of return value, whether the success of the external processing has a greater impact.
5. Too many parametersBOOL Select CALSS Student (int *count,cstudent **student, CString strmajor,cstring strgrade,cstring strclass ); How to increase the class attributes in the future. Recommendation: The relationship is relatively close to the parameters, and the number of large, there is the possibility of expansion, the proposed use of structure, can maintain the interface unchanged In addition, common out-type parameters are typically placed at the end of a function parameter
6. The responsibilities of the class are unclearBOOL savestudentinfomation (tixmlelement *rootelement,cstring strxml,cxmlwrapper *ptinyxml); Student List Class is responsible for reading and writing student information, but also as a container for student objects. Both scenarios seem to be partially functional, and encapsulation is not enough.
if only as a container, is abstract, business-independent, and read-write, is a business category if it is responsible for reading and writing, then there should be a read-processing part of the information, not only the save part extension considerations, storage in the future may have different storage methods, where the direct curing of the XML file format storage, the future can not be expanded. if only as a container, then the file storage class should be removed. In order to support external traversal of the container for related processing, the class should increase the traversal mechanism, such as Prev (), Next (), Head (), Tail (), etc. Also , as a container class, there seems to be a function to modify student information. dependency: In design mode, there are single principle of responsibility ; have iterator Mode . Other recommendations: You can define a student object in a node as a pointer type


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.