Write program is an attitude (1) strcmp

zhaozj2021-02-16  56

Write programs as being a person, it is an attitude. Write a business-grade program, is it compliant with the default code style from the hierarchy of the write code? Is it simple, easy to read and efficient? Does it prevent the expected error? Do you check the memory leak? Do you think that there will be multi-threaded reunion? and many more. Is the idea of ​​software engineering from the level of design? Is there an OO mode analysis? Do you use UML to dominate the entire process? Do you consider QA synchronization of unit testing and integration test? Do you regularly generate a stable version? and many more.

All "Yes" is a correct software development attitude that will make us short-term pain, but it is happy. I want to be a truly responsible software engineer, or a truly experienced software architect (Architect), only from this one "Yes", slowly understand.

Six-year non-stop software development, sometimes there is no pride, reverse the classic code, facing the seniors or the top of the thoughts, my cold sweat overflows each pore, so I felt in this field The small and ignorant. Perhaps a good thing, life is originally a spiral rise, maybe I have reached another spiral. I hope to use this series, and you will explore the growth process of growth instead of the original ground. I will throw away all the impetuous impetuous, starting from the beginning of the head.

Analysis classic code is a best way, I will start from the simplest but most wonderful start: strcmp

INT __CDECL STRCMP (

Const Char * SRC,

Const char * DST

)

{

INT RET = 0;

While (! (unsigned char *) src - * (unsigned char *) DST) && * DST)

SRC, DST;

IF (RET <0)

RET = -1;

Else IF (RET> 0)

Ret = 1;

Return (RET);

}

This is the STRCMP implementation source code in MS C Run-Time. Several questions will reveal its essence and provide guidance on the high quality code we write.

1. How does SRC and DST return for an empty string?

2. Why use unsigned char * forced conversion?

3. Can you use * SRC to replace * DST as the While condition?

4. Is Multithread-Safe?

5. Can you use Multibyte-Character Strings comparison?

6. Can you express less lines?

After you have answered these questions, would you find that the idea is open? I hope there is this effect.

转载请注明原文地址:https://www.9cbs.com/read-25585.html

New Post(0)