当前位置:网站首页>Software engineering in code

Software engineering in code

2020-11-10 17:23:22 thick fog

This paper is completed under the guidance of Mr. Meng Ning, Software College of China University of science and technology , Through the analysis of small programs to help understand the software engineering method 、 thought .

The references and resources for this article are from : Software Engineering —— Minoan's self-cultivation .

 1. VS Code Development environment configuration

1.1 VS Code install

     of VS Code Please refer to this material for installation and configuration of :https://mp.weixin.qq.com/s/sU9Wh12JZqQyJfEBBafFAQ

     In this paper , Let's give a brief introduction to this . We can do it in VS Code Download from the official website , It's a free lightweight IDE, Multiple languages can be supported through extensions .

     Download address :https://code.visualstudio.com/#alt-downloads

1.2 C/C++ Compiler download

     stay VS Code in , When we implement support by loading the extension , It doesn't contain compilers and debuggers for languages . The compiler and debugger here , about C++ for , That is to say MinGw.

    C/C++ The compiler is MinGw: http://www.mingw.org/

     After downloading and installing , We can get the following catalogue :

    

1.3 Environment variable configuration

     Usually , In the installation MinGw when , We will be advised or asked to add it to the environment variable . But if we forget to configure , You can also configure it manually by the following methods .

     First , We need to get MinGw The installation location in our system , As shown above , That is to say :C:\MinGw .

     after , Right click on this computer -> Select Properties -> Advanced system setup -> environment variable , Here you can manage the environment variables of our computer . We find... In the system variable below Path Options :

    

     By editing the Path Options , New add new environment variable address , Fill in the above installation position . It should be noted that , We need to fill in C:\MinGw\bin.

     The above is the C/C++ Compiler environment installation and configuration process . For other languages , Such as Java、Python、Golang, We can all use this method .

     We can see it from the command prompt MinGw Whether the installation of is successful , Use gcc -v Instructions :

    

1.4 VS Code Middle configuration C/C++ development environment

1.4.1 load C/C++ Expand

     stay VS Code In the left expansion option , We search C++ You can search for plug-ins , Click on Install After that, the installation can be completed :

    

1.4.2 task.json And launch.json To configure

    VS Code adopt task.json How to configure files to get compilers , By modifying the task.json You can use it VS Code Conduct gcc Compiled the source code .

     A reference task.json The file configuration is as follows :

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "gcc build active file",
            "command": "gcc",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"//
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

     About task.json The format of , Please refer to the following documents :https://go.microsoft.com/fwlink/?LinkId=733558

    

    VS Code adopt launch.json Configuration file to call the debugger to debug the program . Via shortcut key Ctrl+Shift+P, Search for launch.json, Can open launch.json To configure .

     A reference launch.json The configuration file is as follows :

    

{

    "version": "0.2.0",
    "configurations": [
        {
            "name": "gcc build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "gcc build active file",
            "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe" //  Here is the above MinGw Installation address 
        }
    ]
}

     About launch.json Configuration of files , Please refer to the following documents :https://go.microsoft.com/fwlink/?linkid=830387

1.4.3 Verify the installation

     You can see , Run the following example successfully , Indicates that the environment configuration is successful !

    

    

2. The idea of software engineering in code

2.1 Code comment style

     Good annotation style , Can help code readers 、 Maintainers and other personnel quickly and clearly understand the functions of the code , convenient Debug. A good code annotation style is as follows , This is the case given by Mr. Meng Ning , It's worth learning :

    

     from , You can see it clearly , The file name of the code in this file 、 author 、 Module name 、 Language type 、 Running environment 、 Version time and description, etc , It's very helpful for us to understand the code .

2.2 Modular design

2.2.1 Modular principle

     modularization (Modularity) Refer to , In the design of a software system, keep the parts of the system relatively independent , So that each part can be designed and developed independently . This allows us to , Each module performs its own single function objective , Can be independent of other software modules , This also facilitates the cooperation between developers .

     The basic principle of modularity is the separation of concerns (Soc,Separation of Concerns), Translated into Chinese is probably “ Divide and rule ”.

2.2.2 Degree of modularity

     In the actual software design and development , We usually use coupling degree (Coupling) And cohesion (Cohesion) As the degree of software modularity , They are also important references for judging the excellence of a software design .

     Coupling degree , It refers to the degree of dependency between various modules in a software , From high to low, it can be roughly divided into close coupling (Tightly Coupled)、 Loose coupling (Loosely Coupled). What we're looking for is loose coupling , It shows that the dependence of each module of our software is low , It's a better design .

 

      Degree of cohesion , It refers to the degree to which various elements in a software module depend on each other . The ideal cohesion is functional cohesion , In other words, a module only does one thing , Only one major function .

2.2.3 Modular design : Source code analysis

     In the implementation of modular design , We can encapsulate a variety of data into structures 、 Encapsulate the implementation function into a file , Can help us simplify the code :

    

    

    

     In the diagram above , We put the data structure and function declaration in one linklist.h In the header file , In fact, it is put in linklist.c file , When it needs to be called, it will be in main Function to call , In this way, a lower degree of coupling is achieved , It's also convenient for us to go as programmers debug.

2.3 Software module interface

2.3.1 Definition of interface

     Interface is a kind of specification that both parties of communication abide by together , The general interface mode within the software system is through a set of API Function to define the communication between software modules . Object oriented and process oriented languages are generally different in the implementation of interfaces . The interface of the former is a set of properties and methods open to the public ; The latter is the data structure and the functions that operate these data structures .

2.3.2 Application of interface

    

     The above figure borrows a source code file from the project source file provided by Mr. Meng Ning . In this file , You can see that it declares many interfaces , The implementation of these interfaces will be placed in .c In file . But not through specific implementation code , We can still use the name of the interface 、 Parameters 、 Return values and related descriptions learn about the general function , These interfaces can also be reused many times , Improved code reusability .

2.4 Thread safety

2.4.1 Basic concept of thread

     Threads (thread) It is the smallest unit that the operating system can schedule operations . It's part of the process , Is the actual operation unit in the process . A thread refers to a single sequential control flow in a process , Multiple threads can be concurrent in one process , Each thread performs different tasks in parallel .

2.4.2 Reentrant and thread safe

     Thread safety problems are caused by global and static variables . If global variables in each thread 、 Static variables are read only , No write operation , Generally speaking , This global variable is thread safe ; If there are multiple threads performing read and write operations at the same time , In general, we need to consider thread synchronization , Otherwise, thread safety may be affected .

     Reentrant (reentrant) Functions can be executed concurrently by multiple threads , It does not usually result in error results due to shared data ; Non reentrant functions are executed concurrently by multiple threads , If you can't keep threads mutually exclusive , Then it will lead to errors in the results . For Software Engineering , We should evaluate the function in a pessimistic way : The ability to reentry function is not necessarily thread safe , A thread safe function is not a thread safe function , The non reentrant function must not be a thread safe function .

2.4.3 Thread safety : The code analysis

     First , For thread safety , The focus of our attention is generally in the following aspects :

      i. Are all functions reentrant : Whether the analysis function has access to critical resources , If there is, it is necessary to carefully analyze the process of its mutex processing ;

      ii. Whether it is possible for different reentrant functions to enter the critical region at the same time : How to think about read-write mutex ;

     below , similarly , Through the code given by Mr. Meng Ning , We choose a piece of code to explain :

    

     When you need to enter the critical zone , We lock the code to ensure that other functions cannot use the resources of the critical section , When we exit the critical area, we unlock it in time for other functions to access , Finally, destroy the mutex , This is a typical thread safe code .

3. summary

     The various knowledge points that Mr. mengning explained in class , Make me to the development idea in Software Engineering 、 The method has been further understood . meanwhile , After writing this blog , I have a better understanding of this knowledge , more importantly , Hands on skills have also improved , Thank you very much .

     Last , Again , Here's the reference : Software Engineering —— Minoan's self-cultivation

 

版权声明
本文为[thick fog]所创,转载请带上原文链接,感谢