1. helper Big Bang
. NET Framework provides us with a variety of class libraries, but this is not omnipotent. Most of the time, we need to customize our general class libraries for our projects.
Often, we can construct a class that encapsulates some methods in the class. However, in many cases, we cannot extract such a class. For example, in many cases, we need to save the URL to the database as a unique identifier, however, we know that the URL occupies a large amount of space. If you use a URL to create an index, it will be very space-consuming and will affect the efficiency, the most common method is to make the URL hash as an index alternative.
At this time, we have no way to extract a class and write such a method in the class. At this time, we usually have to do this:
Public static class hashhelper
{
Public static string gethashcode (string S)
{
// Gethashcode ........
Return string. Empty
}
}
Then we will use:
Public static void main (string [] ARGs)
{
String url = "www.fandongxi.com"
String SQL = "insert into test values ('" + hashhelper. gethashcode (URL) + "')"
// Execute SQL
}
Here is just an example. It doesn't mean we want to splice strings like this.
Soon, there will certainly be another situation, saying that we want to save the content of the webpage, but the content of the webpage is stored directly in the database, which is too big, then we need to encode the webpage text with base64 and then compress it. // Some of the previous errors have been picked up by many people .....
Then, we have to continue writing:
Public static class base64helper
{
Public static string getbase64text (string text)
{
// Base64 ........
Return string. Empty
}
}
Next we will use another base64helper. In a few days, there will be various helper, such as sha1helper and md5helper.
Gradually, will we find that the number of helper has left us intolerable?
2. Proposal of extension methods
Next, we all know that the extension method is introduced in. NET Framework 3.5, that is, in C #3.0.
Let's extend the method to solve the above problems.
You must know now,Whether it is urlhashcode, base64 compression, sha1 encryption, or MD5 encryption, these are all processing strings or text segments.Naturally, we need to write all of these data into the extended method of the string class.
Public static class extensionclass
{
Public static string gethashcode (this string S)
{
//........
}
Public static string getbase64text (this string text)
{
//.......
}
}
Public static void main (string [] ARGs)
{
String url = "www.fandongxi.com"
String SQL = "insert into test values ('" + URL. gethashcode () + "')"
// Execute SQL
}
Here, I don't want to analyze the implementation nature of the extension method. Here we only talk about the significance of programming thinking and extension methods.
3. Extend the method to make C # more object-oriented
From the object-oriented perspective, everything in the world is an object, all attributes, and all methods belong to a specific object, so let's take a look at it from this perspective,There should be no static classes or static methods. The so-called static method is just a kind of obedience to the immature syntax implementation of object-oriented languages.
We require base64 encrypted text, in fact, is a method of the text call itself, the reason why we need a base64helper in the previous method, rather than this "http://www.fandongxi.com % 22. replace (% 22com % 22, % 22cn/") is called directly because. net Framework cannot predict all of our business scenarios, so we can only encapsulate the most common methods into the existing class libraries.
4. Let's talk about the extended method.
Let's gradually explore some coding standards from the Extension Method to the periphery, and someCodeElegant question. Let's assume that we do not have the "+" operator, or that we disableProgramUsing the + operator, that is to say, we need to make a simple encapsulation of the "+" operation. What will we do in the general sense?
Public int add (int A, int B)
{
Return A + B;
}
Public static void main (string [] ARGs)
{
Int result = add (3, 4)
Console. writeline (result)
}
Let's look at this function. We will read down, add, 3, 4 according to the meaning of the Code. This is obviously not in line with our conventional mathematical thinking. If we use the extension method, we should write it like this.
Public static class Extension
{
Public static int add (this int A, int B)
{
Return A + B;
}
}
Public static void main (string [] ARGs)
{
Int A = 3;
A. Add (B)
}
But this "." operator looks a little awkward ..... No way. At least it makes our code much smoother by reading it like this, isn't it? Image writingArticleWriting code like speaking has always been the highest level pursued by our programmers, and such code is always good.
Good: people. Eat (food)
Instead of bad: Eat (people, food)
Pair!
5. prefix, infix, and suffix expression
Speaking of this, we have to talk about prefixes, infix and suffix expressions.
Those who have learned the data structure should remember that there is a classic exercise in the data structure, which is to use the "stack" to implement prefix, infix, and suffix expression conversion. Such exercises often appear in the exam questions. Now let's review what is prefix, infix, and suffix expression.
A prefix expression is an arithmetic expression that does not contain parentheses. It writes operators before them, and the operands are written in the following expressions, also known as the Polish expression ".
The well-known lisp is a typical prefix expression. Let's take a look at the simplest small example, or the classic Fibonacci series:
(Define (fib n)
(Fib-iter 1 0 n ))
(Defile (Fib-iter a B count)
(If (= count 0)
B
(Fib-ITER (+ a B) A (-count 1 ))))
Every time I write lisp, it will be scared by the dense brackets, but there is really no good solution!
The infix expression is very simple. It is the same as the Code involved in our general practice. The suffix is also a truth and will not be repeated here. Since the application of the suffix is not very large, Here we only talk about the meaning of prefix and infix.
So let's think, why should lisp adopt such a lame prefix expression syntax?
I remember when I first learned the C language in my sophomore year, the teacher asked us to write a simple calculator. At that time, every student wrote +,-, *,/operations, however, at that time, most of us were unable to write more common mixed operations and () operations. At that time, only some of our friends in the class wrote the code that we could not understand at that time. It wasn't until the junior year learned the data structure and thought about his current code.
With a bunch of nonsense, the biggest significance of the prefix expression is that it is more close to the computer's thinking. He only needs two operations to complete the computation, that is, the inbound and outbound stacks. Let's look at a simple example.
3 + (1-4). First, this is an infix expression. convert it to a prefix expression + 3-1 4. The computer will scan the expression from right to left, 4 into the stack, 1 into the stack, and then encounter-, 1 and 4 out of the stack, and complete the operation, (-3) into the stack, 3 into the stack, + into the stack, (-3) and 3 to complete the operation.
That is to say, when the computer completes the mathematical operations we write, we usually convert our infix expression to a prefix expression first and then complete the computation, while lisp uses a prefix expression, this step is omitted to improve the interpreter efficiency.
Let's summarize the meanings of prefix and infix expressions.
Prefix expressions are more closely related to computer thinking and facilitate computation. The infix expression is more closely related to mathematical thinking and is easy to understand.
Let's review the code of ADD. If we delete it. operator, and the method is not enclosed in parentheses, whether to use the extension method, the C # syntax and the lisp syntax are combined, in fact, this form.
Public int add (int A, int B)
{
Return A + B;
}
Public static void main (string [] ARGs)
{
(Set! Result (add a B ))
}
Public static class Extension
{
Public static int add (this int A, int B)
{
Return A + B;
}
}
Public static void main (string [] ARGs)
{
(Set! Result (a add B ))
}
Or the latter is closer to our natural thinking.
The. NET Framework is very powerful and provides us with the concept of extension methods. If there is no extension method, what solution does other languages provide?
Let's take a look at the Haskell solution.
5. Check the Haskell method.
Haskell is a functional language. As FP is widely used today, Haskell's long-lived language has gradually surfaced.
Let's just look at how Haskell solves the inconsistency between syntax and natural thinking without an extension method.
Let's write a simple Haskell function first.
Add x y = x + y
The code is very simple and there is nothing to say. Let's take a look at how Haskell calls it.
This is our traditional call method, but Haskell provides a convenient call for the method with the number of parameters 2 in order to stay closer to our natural thinking:
This is the solution that Haskell provided to us for "infix expression.
The extension method is good, but when there is no extension method in our language, Haskell provides us with an excellent example.
6. Languages and Class Libraries
Speaking of this, I want to talk about Language extension and Class Library Extension by the way.
In masterminds of programmming, Guido, the father of the Python language, talked about Pep (Python enhanced processing) in an interview, By the way about how to writeProgramming LanguageHow to handle language implementation issues based on users' opinions.
He said:
If a user proposes a new feature, it will hardly succeed. Because the user does not have a comprehensive understanding of the implementation, it is almost impossible to propose a reasonable new feature.
In my opinion, what is a user? Users use this language to complete their work tasks. They often need to add new features. In other words, they only need a method.
Guido also gives a reasonable explanation of what is the language feature and how to add a class library.
If a feature is really good for the web, it may not be a good feature to be added to the language. It may be a good thing to add a shorter function or a more maintainable class to a language.
In fact, Guido is very simple: whether it is added to the language. Whether this feature is related to the domain or not. If it is related to the domain, it may only need to expand the class library, no matter whether you add Python class libraries or use C to expand Python APIs, you do not need to change the language.
So for C #, what is the modification of the class library and what is the modification of the language? In my opinion, the modification of each version must have the modification of the class library, but if we say the modification of the language, it should be that when the msil changes, we can say that the language has changed. // I thought about it for a moment. There is a problem with this point of view... but I didn't find a more appropriate language for metaphor. It may be said that only when the compilation rules of the syntax change can we say that the language has changed.
The same is true for python. The method added is the modification of the class library at best, but only the modification of the language interpretation process can be considered a language-level modification, for example, from Python 2. major version changes from X to python3.x.
7. Summary
In this article, we will talk about how to better compile programs that are closer to natural languages.
Then there are some compromise implementations provided by languages without extension methods. For other languages such as Python and C, I have not found a proper solution.
If you have a good solution, especially for python, after all, this is my job. I hope you can provide a solution.
Thank you.