Over the years, I have seen a lot of anti-programming models. I feel I should share some with you.
Today, I want to introduce the anti-pattern I call the anti-for-if programming mode. "If you are interested, check this article: Why is a for loop hateful ?」, That is what people call "we sell you the entire seat, but all you need is one side ."
This is a special anti-case mode, in which only one of all cases will be null.
- For (INT I = 0; I <100; I ++ ){
- If (I = 42) {do_something (I );}
- }
In this case, you can simply write
- Do_something (42 );
This reverse-If mode can be represented in various forms. For example:
- Foreach (string filename in directory. getfiles ("."))
- {
- If (filename. Equals ("desktop. ini", stringcomparison. ordinalignorecase ))
- {
- Return new streamreader (filename );
- }
- }
It is used to traverse a specified file in a directory. If a specified file is found, the data stream of the file is returned. One way to write this code is
- If (file. exists ("desktop. ini "))
- {
- Return new streamreader ("desktop. ini ");
- }
Note that the code snippets of the two versions have the same competition condition: If this desktop. INI already exists, but it is deleted before you create streamreader, and you will get a filenotfoundexception error.
Another example:
- Foreach (Object o in hashtable. Keys)
- {
- If (O = "target") return hashtable ["target"];
- }
Equivalent
- Return hashtable ["target"];
I guess these guys don't like to find a book by name in the library, because their approach is so tedious:
They came to the library and said, "Give me all your books." Then they sat down in the corner and said to themselves, carrying baskets full of thousands of books:
"No, the title of this book is incorrect ",
"No, this is not ",
"The title is still incorrect ."
"What about this book ?"
"No, neither. "
"My God, when will I need such a copy... "