Understanding software development in advance (16) How to optimize the program

Source: Internet
Author: User
Tags comments lowercase new features printf strlen

To optimize the program is a problem that the Software Development engineer will inevitably involve. So why do you want to optimize the program? There are several reasons for this:

First, the original program based on the new, delete or modify the function, need to change the original program flow. Customer needs may change at any time, the functionality has been implemented today, perhaps tomorrow will be modified or removed. Implementation to the program above, we need to be ready to write the code to modify, and do not expect to write good after never move.

Second, the original program has bugs. This kind of situation appears very frequently, many software has 1.0, 2.0, 3.0, and so on, in part because the previous version of the program has problems, in the process of modifying the program so that the software version is constantly upgraded.

Third, the original program efficiency is low or not easy to read, optimize the program can improve efficiency or easier to read. In software projects, each function contains a number of lines of code, if more than the specified number of lines, it is necessary to consider the optimization of the code, part of the function to extract a separate function.

For program optimization, we have to follow the following two principles:

First, the principle of "small steps run". This principle refers to every change in a little bit of the program to test, after the test passed and then modified a little bit, and then to test. So keep on looping until the program is modified and tested. This ensures the correctness of the program's functionality and reduces the cost of significant changes later in the period.

Second, the "two hats" principle. One is to refactor only the code without adding new features, and one is to add additional functionality to achieve new requirements. That is, if you find that the original program has many problems, you need to optimize before adding new features, then the first step to optimize the original code without adding new features, the second step on the basis of new code to add new code to achieve new features.

In this paper, a practical program as an example, detailed description of how to optimize the program code.

1. Optimize the previous procedure

This program implements the function of turning uppercase letters in the input string into lowercase letters, as follows:

#include <stdio.h>
     
#include <stdlib.h>
     
#include <string.h>
     
      
     
void Main ()
     
{
     
    Char s[100];
     
    unsigned int  i;
     
         scanf ("%s", s);
     
    For (i=0 I<strlen (s); i++)
     
       {s[i]=tolower (s[i]);
     
    printf ("%s\n", s);
     
}

As can be seen, this procedure has the following problems:

(1) Code typesetting is not neat and no comments.

(2) The variable naming is not canonical and is not initialized at the time of definition.

(3) Code indentation is not standard, for statement writing is not standard.

(4) There is no corresponding text reminder for the input and output.

(5) The function of converting uppercase to lowercase can be considered as a function to facilitate reading and other module calls.

As can be seen, although the program can achieve basic functions, but not optimal. Here we will step through the code optimization.

2. Optimize the Code

Step 1: Re-typesetting the program and adding comments

For code that is not neatly formatted and has too few annotations, the first step in optimization is to standardize the layout and add the necessary annotations. The rule of typesetting is that the statements within "{}" are indented 4 spaces relative to "{}", and the same Code is aligned. Add a comment at the head of the program, the head of the function, and the key statement.

The modified code looks like this:

/********************************************************************** * All rights reserved (C) 2014, Zhou Zhaoxiong. * * File name: STRTOLOWERCASE.C * File ID: no * Content summary: Turn the uppercase letters in the input string to lowercase * Other notes: no * current version: V1.0 * Author: Zhou Zhaoxiong * Completion Date: 20140426 * * Modify record 1://modify history, including modified date, version number, modified person and modified content * Modified Date: 20140426 * version number: V 1.0 * Modify Person: Zhou Zhaoxiong * Modify content: Create ********************************************************************** /#include <stdio.h> #include <stdlib.h> #include <string.h>/***** 
     
* Function Description: main function * input parameter: no * output parameter: no * return value: None * Other Notes: No * Modified date version number modified by the person modified content *--------------------------------------------------------- -------------------------------------*20140426 V1.0 Zhou zhaoxiong Create *********************** ******************************/void Main () {char s[100];
     
      
     
    unsigned int i;
     
    printf ("Input The source string:");                        scanf ("%s", s);   Read the original string for (i = 0; I < strlen (s); i + +) {S[i] = ToLower (s[i));   Converts uppercase letters in a string to lowercase, other characters unchanged} printf ("Output The destination string:%s\n", s); Output Destination String}

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/

Related Article

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.