Code style (style of the codes)

Source: Internet
Author: User
Tags documentation

1. Introduction

2. What is style?

3. Why is the style of code so important?

4. Less work, more documentation?

5. Recommended Code Style

6. Simple documentation

7. Output style

8. Conclusion

Brief introduction

Let's face it ... If you are a senior programmer, you always work under deadlines and your ultimate goal is to complete the software you design. If you are a novice, your efforts are more research, experimentation, and, of course, mistakes and a desire to have your programs run as intended. However, the sense of success when your program is actually doing its job is exciting, but more programmers find the sense of pride when they can generate the code inside a good style.

If you are a member of a programming group or if you are writing a school assignment, you will have the opportunity to be required to comply with certain style rules that must be followed. The vast majority of programmers think about style the same way a 10-year old child looks at bedtime. But the senior programmers and the team leaders I've seen and the programmers who have used many styles over the years will try to convey a good code style to you as important as some of the code-writing techniques you're proud of. In this article I will try to include some output style views that may be helpful in your programming journey.

Less work, more documentation?

So style means to add annotations to each line of code, right? Wrong. If you don't do it right, annotations can become a disaster. I still want a programmer to learn to like to input annotations or really pay enough attention to annotations. For future serviceability, you must force yourself to place annotations correctly. So how can you not use annotations?

Obviously the hardest thing for a novice is to know what a good variable name is. Let's take a look at the following code. Can you tell me what the following code does?

float __fastcall TExampleForm::CalcAverageGrade(void)
 {
  int y=0;
  int i=0;
  for (i; i<x; i++) {
    y=y+g[i];
  }
  return float(y/x);
 }

In the example above, you can easily tell me what Y stands for. What is X? This code may need some annotation to explain the meaning of these variables and what is happening in the code? Application of the above code style, do not see any logical relationship. Now let's take a look at what the code looks like after applying a better code style.

float __fastcall TExampleForm::CalcAverageGrade(void)
 {
  int total=0;
  for (int i=0; i<maxGrades; i++)
   {
    total+=Grades[i];
   } //求所有成绩的和
   return float(total/maxGrades);
 } //计算平均成绩

You don't have to say more than two pieces of code that are superior to the other. Now let's talk about some of the recommended styles that will make your code more maintainable.

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.