Why don't I use do {} while ()

Source: Internet
Author: User

This is a very unexpected topic, I released a blog two days ago, "C language learning in variable parameters processing" http://tonyxiaohome.blog.51cto.com/925273/314371), did not expect to cause a debate. In this case, the biggest argument is that the variable parameter processing macro does not use do {} while (0) encapsulation, but directly uses braces. It should be said that a large number of such discussions are somewhat unexpected. I wrote this blog post about how to handle variable parameters and use a FUNCTION macro to handle them. It is a processing method and serves the idea of handling variable parameters. What I want to do is, more importantly, from this blog post, you can learn how to process parameters for changing parameters and apply them directly to your work and study. However, I did not expect that, many experts and quasi-experts did not focus on the central idea of variable parameters processing, but focused on macro books. This makes me feel a little bit confused. I am telling the truth. If you read a blog post later, you 'd better check what the blogger really wants to say. Don't always take your own ideas to read other people's articles. Otherwise, you have picked a lot of errors and the results, the main meaning of what the blogger wants to talk about is not understood. Apart from being able to look good at himself, it is really hard to learn. My book 0bug-C/C ++ road to commercial engineering has actually encountered this situation. Most readers love to learn it. However, there are really a few people, the purpose of reading a book is to find errors. In many cases, no matter what the original book is talking about, you just need to find errors in the direction you are familiar. There is a mentality in it. It is not ruled out that some gunmen deliberately aim at it, but I think there are at least a considerable number of people who may think like this:"Aha, there is a bug in this book. The author is not as good as me at this point. I have to write something or point, which seems to be better than the author. The author's level is not so good!". As a result, the shoot of bricks, sprayed by people, and PK will all come. Many times it is hard to hear, which makes it difficult for me to answer. In the end, I only need to delete the post. Let me give you a suggestion. I don't listen to anyone here. I suggest that you may encounter books, blog posts, speeches, or something in the future. Don't worry about it, or predefine your stance. First, let's listen to it and see if there are any shortcomings,I learned something from others, but I didn't talk about it myself. I couldn't learn it. You didn't make any money.. Haha. When reading documents, our programmers often encounter a word "Context", which is the Context. translation to Chinese is the cause and effect. In many cases, one sentence in the article is, the analysis must be based on context and context. It is a prerequisite and cannot be understood in a single sentence. I think everyone knows this truth. Here, I also suggest you read blog posts and be patient. After reading them from start to end, I find that many people have no patience, read a little, or read a sentence in the middle of the article, this is at least not objective, right? Just like when I gave a speech at the beginning of the year, "tomorrow's world -- the programmer's needs under cloud computing", some people started to scold me in less than ten minutes and said it was terrible. I'm dizzy. I 've been talking for two hours. In 120 minutes, I asked him if he was just reading a book in? He stops talking. I want to say this completely because I am a blogger, author, or speaker. Many times, from an objective point of view, if we want to understand other people's words, we should at least finish reading them, this requirement is not enough, right? Well, let's get down to the truth. The cause of the story is this. In "variable parameters processing in C Language Learning", there is such a section: Code:

  1. # Define TONY_FORMAT (nPrintLength, szBuf, nBufferSize, szFormat )\
  2. {\
  3. Va_list pArgList ;\
  4. Va_start (pArgList, szFormat );\
  5. NPrintLength + = Linux_Win_vsnprintf (szBuf + nPrintLength ,\
  6. NBufferSize-nPrintLength, szFormat, pArgList );\
  7. Va_end (pArgList );\
  8. If (nPrintLength> (nBufferSize-1) nPrintLength = nBufferSize-1 ;\
  9. * (SzBuf + nPrintLength) = '\ 0 ';\
  10. }
Since do {} while (0) is not used for encapsulation, the following brackets must be added in the if {} else {} pair:
  1. # Define TONY_LINE_MAX 1024 // maximum number of output characters in a row
  2. // Output to the console
  3. Inline int TonyPrintf (bool bWithTimestamp, // whether there is a timestamp mark
  4. Char * szFormat,...) // format the string
  5. {
  6. If (! SzFormat) return 0;
  7. Char szBuf [TONY_LINE_MAX];
  8. Int nLength = 0;
  9. If (! BWithTimestamp)
  10. {// Note that the braces if... else are required because they are function-type macros.
  11. TONY_FORMAT (nLength, szBuf, TONY_LINE_MAX, szFormat );
  12. } // Note that the braces if... else are required because they are function-type macros.
  13. Else
  14. {// Note that the braces if... else are required because they are function-type macros.
  15. TONY_FORMAT_WITH_TIMESTAMP (nLength, szBuf, TONY_LINE_MAX, szFormat );
  16. } // Note that the braces if... else are required because they are function-type macros.
  17. Return printf (szBuf );
  18. }
In fact, you do not need to add it. Remove the Semicolon ";" next to the macro call, so that you can avoid increasing the brackets and write them as follows: Code:
  1. # Define TONY_LINE_MAX 1024 // maximum number of output characters in a row
  2. // Output to the console
  3. Inline int TonyPrintf (bool bWithTimestamp, // whether there is a timestamp mark
  4. Char * szFormat,...) // format the string
  5. {
  6. If (! SzFormat) return 0;
  7. Char szBuf [TONY_LINE_MAX];
  8. Int nLength = 0;
  9. If (! BWithTimestamp)
  10. TONY_FORMAT (nLength, szBuf, TONY_LINE_MAX, szFormat) // note that there is no semicolon
  11. Else
  12. TONY_FORMAT_WITH_TIMESTAMP (nLength, szBuf, TONY_LINE_MAX, szFormat) // note that there is no semicolon
  13. Return printf (szBuf );
  14. }
This was a matter of detail, but it caused a lot of controversy because we can see from many C language libraries that do {} while (0) is used for encapsulation, there is no such restriction. So the call came, huh, huh. Later, I couldn't help. I only gave a detailed explanation. In my program, do {} while (0) is not required. The reason is as follows: Alas, let me talk a little more. I have mentioned in my book 0bug that it is strictly prohibited to use a multi-Definition Statement. The do {} while statement can be used instead of while () in loop writing, and more accurate. In my opinion, it is only used when writing macros. What does this mean? The only function of do {} while is to use macros instead of loop statements. This is just a multi-definition phrase, and it is just a mess, I think it is highly misleading to programmers. We all learned do {} while by learning loop statements. However, it seems that, in actual use, it is mostly used as macro commands, rather than loop statements. do you think it is messy? I hate this kind of stuff that sells dog meat. Therefore, my program does not use do {} while () or my team of programmers. I don't want our code, literally, it seems to be a different meaning. The only price I pay is that, as described in this article, when I use a macro, I encounter if {} else {} again. I need to explicitly write braces. This is a method that I have discussed with a few C-related friends for a long time. Over the past 15 years, we have been using this service for almost 15 years and have never encountered any problems. The C language is flexible and there are no prescribed standards. In fact, the do {} while () is used as a macro inclusion character rather than a loop statement. This is a typical non-standard writing method, do you think about it? I wrote so much, mainly to explain that do {} while () is not required, but more from the starting point of standardized programming, bug reduction, and team bug reduction, it's not that I won't use it, not that it's hard to write. After careful evaluation, the C/C ++ language has been used in many normative la S. There are many solutions to this problem. However, I tend to adopt a standard that is the simplest and least error-prone, so that my team can develop efficiently. In fact, the book 0bug-C/C ++ commercial engineering, in addition to parallel, focuses more on standardized development. Although it seems that, it limits the randomness and flexibility of programmers. However, this ensures the quality of team development and helps you make money. I think it is a good solution. In, I brought my team to build a commercial server cluster with tens of thousands of lines of code and only 51 bugs. The C/C ++ section contains only 7 bugs, this is the result of standardized development. This story is told in the book. I know that everyone understands that do {} while () can be used as a macro. I also know that, however, I hope you can think more about how to write errors easily, it is not easy to read wrong Code. Such code has few bugs and can make money. Do you want to write it? I think, at least I write a program not to show off, not to show that some things understand themselves, others do not understand, it seems that I am superior. What I think is more about how to output products in a simple and direct manner without bugs. Can you agree with this idea? Do {} while () seems simple, but it is very troublesome to talk about the idea of standardizing program design. Can you understand my replies?Sorry, what I want to talk about is the above section. If I still think it is not detailed enough, I suggest you read the book 0bug-C/C ++ commercial engineering path, which also has a context problem, there is an idea and principle behind the C/C ++ error-free design method I have defined, that is, there is a trade-off principle when using language cropping, this book is more detailed. After reading this, I want to understand why I have removed do {} while () here. I don't have to buy books to read my books. My books are available in all major bookstores. If I have time, I will go to the bookstore and look at Chapter 3, I don't think you will do anything that is cheap. Do {} while (0) is misleading. If you are interested, take a look at this example: Code:
  1. Int I = 0;
  2. Do
  3. {
  4. I ++;
  5. }While(0 );
  6. // I =?
To put it this way, while (0), as the name suggests, loops 0 times, but has I ++ been executed? I am not very familiar with C language. How can I guess I = when I see this code? Do {} while (0), meaning ambiguous. the literal meaning is the opposite of the actual meaning, so I don't need it. Let's take a look at this example: Code:
  1. Void TestDoWhile (void)
  2. {
  3. Int I = 0;
  4. I = 0;
  5. Do
  6. {
  7. I ++;
  8. Printf ("I = % d \ n", I );
  9. } While (0 );
  10. I = 0;
  11. Do
  12. {
  13. I ++; // The program is in an endless loop.
  14. Printf ("I = % d \ n", I );
  15. } While (1 );
  16. }
Good guy, do {} while (0) indicates that the task has been executed once and do {} while (1) indicates that the task has been executed countless times. do you think this semantic understanding is ambiguous and misleading? Okay, come here first. I am here, but I am also talking about it. Many times, I 've worked so hard to write something out. It was originally out of a good intention to share some of my research experiences with you, when someone comes up, it's really uncomfortable. What do you say? ========================================================== ====================
Buy my book 0bug-C/C ++ online at reserve price
Directly click the link below or copy it to the address bar of the browser)
Http://s.click.taobao.com/t_3? Limit % 3 Fpid % 3Dmm_13866629_0_0 Xiao Miao

This article from the "Xiao blog" blog, please be sure to keep this source http://tonyxiaohome.blog.51cto.com/925273/315416

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.