Talking about C Language Teaching: we should cultivate students' good programming styles and habits from the very beginning.

Source: Internet
Author: User

Today is blank. After turning over the technical support record of the next few days, a QQ conversation record is unexpected: a student is about to graduate and is not very familiar with writing independent functions yet. If you think about it, you cannot blame the students. The problem may be caused by books and teachers. Review the previous C language tutorials. For many examples, the functional code is written in the main function, and the output series of information strings are also directly written in the Code. Although this is relatively simple, for beginners, it is also easy to understand, but I think this is very bad for cultivating students' good programming habits from the very beginning. We have cooperation with many universities. As far as I know, in most universities, the first language students learn is C language. The Style and habits developed by the first language will have an important impact on the future study and work. Therefore, we should cultivate students' good programming style and habits from the very beginning. Attention should also be paid to this point.

The following is a dialog. The code written by the students reflects some problems in the C language teaching. The modified code of our on-duty technical support reflects our suggestions. To protect this student, replace the student's QQ user name with a student in the record.

 

Classmate A 11:43:54
I want to ask why my Code cannot be tested.

 

Kile technical support 11:44:10
What version of VU are you using?

 

Classmate A 11:44:17
2.6

 

Kile technical support 11:44:26
What are the specific problems?

 

Classmate A 11:45:03
I wrote a piece of code in VC ++ 6.0, but I cannot enter the use case during the VU test.

 

Classmate A 11:45:26
Maybe I don't know how to use vu, so I 'd like to ask.

 

Kile technical support 11:45:50
Has it been compiled?

 

Classmate A 11:46:10
Yes. results can be run in VC ++ 6.0.

 

Kile technical support 11:46:56
In the use case code window, click "generate input and output code ":

 

Classmate A 11:47:28
Can I send the code to you for help?

Kile technical support 11:47:50
Okay.

 

Classmate A 11:48:25
# Include <stdio. h>
# Include <math. h>
Main ()
{
Int A, B, C;
Scanf ("% d", & A, & B, & C );
If (A <(B + C) & (B <(A + C) & (c <(a + B )))
{
If (A = B | B = c | A = C)
{
If (A = B & B = C)
Printf ("equilateral triangle ");
Else if (a = B | A = c | B = C)
Printf ("isosceles triangle! ");
}
Else
Printf ("normal triangle! ");
}
Else
Printf ("cannot form a triangle/N ");
}

Classmate A 11:48:29
Thank you!

 

Kile technical support 11:50:10
How can you test such code? The actual project won't write the code like this.
To separate the triangle function, a B C is the parameter, and the return value is an integer that represents the triangle type.

 

Classmate A 11:50:38
How can I modify it?

 

Kile technical support 11:51:12
Separate the code used to determine a triangle into a function.

 

Classmate A 11:52:10
Can you modify it for me? Thank you.

 

Kile technical support 11:52:30
Are you a student?

 

Classmate A 11:52:51
Yes

 

Kile technical support 11:53:48
I think you should learn to write code first. unit testing will be later.

 

Classmate A 11:54:33
Tell me this is my graduation project

 

Kile technical support 11:55:27
I still cannot write independent functions after graduation? Do you usually write all the code in the main function?

 

Classmate A 11:56:31
Are you sure you want to study?

 

Classmate A 11:56:47
Do you have to say this?

 

Kile technical support 11:59:37
In this way, I can't stand it anymore? I am also kind. The current employment environment is so bad that I am worried about you.

 

(My comment: on-duty technical support is a programmer rather than a full-time customer service. It should be criticized, but it is good to be careful. It is not the role of technical support to help students modify code .)

 

Classmate A 12:02:01
Can you modify the configuration for me?

 

Kile technical support 12:03:01
Modification is a small thing. It does not help you, but it hurts you.

 

Classmate A 12:04:13
You can modify it for me first.

 

Kile technical support 12:06:31
If you are in a hurry, I can help you change it. If you still have time, I suggest you solve these simple problems by yourself and develop your own habit of solving the problems. Otherwise, what should you do in the future? When you get into the society, you will understand what I said.

 

Classmate A 12:07:18
Well, I will pay it in the afternoon.

 

Kile technical support 12:07:41
That's all. I can help you change it.

 

Classmate A 12:07:52
Thank you.

 

Kile technical support 12:28:19
Collect files

 

Kile technical support 12:29:08
Then test the triangle ()

12:30:56
The file "Triangle. c" (1.00kb) is successfully sent ).

The content of the triangle. c file is as follows:
# Include <stdio. h>
# Include <math. h>

Enum {
Tr_not, // non-Triangle
Tr_equ, // equal edge
Tr_iso, // equal waist
Tr_nor // average
};

 

Const char * msgs [] =
{
"Cannot form a triangle/N ",
"Equilateral triangle/N ",
"Isosceles triangle/N ",
"Normal triangle/N"
};

Int triangle (int A, int B, int C)
{
If (A <(B + C) & (B <(A + C) & (c <(a + B )))
{
If (A = B | B = c | A = C)
{
If (A = B & B = C)
Return tr_equ;
Else if (a = B | A = c | B = C) // this judgment is redundant. Thank you.Yboy8 points this out.
Return tr_iso;
}
Else
Return tr_nor;
}

Return tr_not;
}

 

Int main ()
{
Int A, B, C, RET;
Scanf ("% d", & A, & B, & C );
Ret = triangle (A, B, C );

Printf (MSGs [RET]);
Return 0;
}

Good testability and maintainability are the basic requirements of code. It is necessary to cultivate students' awareness of this aspect from the very beginning. I have read an article in computer programming skills and maintenance, which contains the following code:
If (Var = 0)
Showmsg ("**********"); // Information 1
Else if (Var = 1)
Showmsg ("*************"); // Information 2
....
Else if (Var = 40)
Showmsg ("****************"); // information 41

 

If the information string is independent, the 70 or 80 lines of code can be replaced by one line of code. This article, published in a veteran programming magazine, may further explain that it is important to cultivate a good programming style and habits from the very beginning. Otherwise, this bad style and habit may not be changed for a long time.

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.