There is no one-size-fits-all language in the world. We have to learn a new language to meet different situations and needs. Recently, I prepared to enter the pit of Flutter(technical reserve), learned some Dart, and shared some experience with you.
The outline
- introduce
- 1. Assume the premise
- 2. Basic principles
- 3. Basic posture
- Existing language cognition
- Build a tag cloud
- Deliberate learning, take Dart for example
- ① The most annoying language of 2018?
- ② Optimize for clients
- ③ Object-oriented
- (4) operator
- ⑤ Const /final vs. static data
- ⑥ Type system
- ⑦ Asynchronous asynchronous
- Today metaprogramming
- practice
- Learn more about the language
- conclusion
- extension
introduce
1. Assume the premise
I think everyone reading this article is a programmer, right? It’s easy to learn a new language quickly by reusing what we already know. If you are white, this article is probably not for you
2. Basic principles
-
Determine the location and context of the language. Stop saying PHP is the best language, everyone knows that. Each language has its own niche and scenarios for solving different problems. So when learning a language, it’s important to understand the language’s location and domain first, so that you can understand the motivation of the language designer for designing a particular feature. Such as:
JavaScript
: Browser scripting language hegemony, writing front-end must be able to avoid JavaScript. A language made in ten days, don’t ask[] = =! [] // true
What’s the motive? Don’t follow the crap.Dart
Optimizes the language ‘Flutter’ for the client, originally claimed to replace JavaScript. The official position of Flutter is the clientGo
: it is said that isC++
A language forced by slow compilation. So you can think of C++ as the opposite of its design: simple syntax, fast compilation, garbage collection, high performance, high concurrency. Half are to solve C++ problems. Suitable for server programming, distributed programming, network programming, cloud platform.Rust
: System programming language,C/C++
The most serious challenger
Of course, there are some languages that can only be used on certain platforms or scenarios, and this is a commercial barrier. For example,
Swift/Objective-C
It’s mostly Apple only, and although Swift is open source and can run on Linux, it’s rarely seen outside of Apple app developmentC#
Similar to Swift,
-
Don’t get bogged down in the grammatical details of your language. Strip away the grammatical sugar. When learning a new language, you can ignore the details of grammar for a while and switch to the perspective of God
-
Based on the original cognition, horizontal comparison is made. Normally, 80% of the concepts or paradigms in programming languages are universal, which is why you can quickly start learning other languages if you are familiar with one.
-
Break the cognition. The other 20 percent contains features and ideas unique to the language, and that’s what we need to focus on.
3. Basic posture
- Identify the problem/scenario you want to solve. We don’t usually learn a language for the sake of learning it. You should first have scenarios and problems to solve, and then choose a programming language with those problems in mind, identifying multiple language candidates.
- Think about how these languages solve your problems. This is the process of ‘breaking perceptions’, analyzing the strengths and weaknesses of these candidates
- Sure you want to learn the language?The following steps will be followed
- Build a tag cloud. Collect 20% of the language’s unique features/ideas, such as Killer features, slots, and blow points, and study them on purpose.
- Skim the official documents. That’s the 80 percent who have a basic impression of basic grammar and can quickly understand it by analogy to their familiar language.
- Start practicing. Now you’ve got the first impression of the language. Get started while it’s hot, for example by following the official introductory tutorial. Practice while consulting documents, will soon be proficient
- Learn more about the language
Existing language cognition
Here is a map of common programming languages. Do you know all of these concepts? Do you really know the guy you’re eating with?
Don’t understand? It seems you haven’t learned a real (complex) language like Scala, C Gaga, Rust. 😺 Climb over these mountains, and the rest are small. It’s too difficult
Kids make choices, smart people want all of them. You can also learn a few representative languages. Refer to seven Days and Seven Languages to start the skill tree:
-
By market:
- Generic type language (for eating). Examples include Java, JavaScript, Python, C/C++, Go, PHP, Objective-C/Swift(iOS developer, not strictly ‘universal’)
- A niche language for your taste. Rust, Elixir, Ruby, Kotlin, Clojure, OCaml…
-
By paradigm:
- Object oriented: Ruby, Java, Python…
- Multiple paradigms: for example JavaScript, Scala, Rust…
- Functions: Lisp(such as Clojure), Erlang, Haskell…
- Procedural: e.g. C, Go(object-oriented, Whatever)
- Prototype language: Io, so niche
-
Other ways of division:
- Type: strong type, weak type; Static type, dynamic type
- Execution mode: static language, scripting language
- System level: system programming language, application language
Build a tag cloud
As mentioned above, 80 percent of knowledge is reusable, and the other 20 percent should be deliberately learned for features and ideas unique to the language. One approach I’m going to introduce here is to build a tag cloud. The tag cloud is a key description of the language. For example, its main features, advantages, jokes.
These key descriptions are great for quickly understanding a language, and the tag cloud represents your basic impression of the language. In other words, you learn a language but don’t use it very much, and after a while you forget all the grammatical details, but the basic impression of the language stays in your mind for a long time. I think these impressions are the essence of the language.
So how do you collect this tag cloud?
- Open the official website. See how officials describe their language, what its main features are, and what its position is.
- You can also see the system description and definition of the language in the Wiki
- Zhihu. See how others blow or make fun of the language
- What I hear
- Quick preview of the official guide. Looking for a bright spot
Just to name a few. The Dart language:
The tag cloud is generated using WordClouds
The Go language:
Javascript:
Elixir:
Deliberate learning, take Dart for example
Then I will learn this language with these questions. Here I take Dart as an example, because I happened to learn Dart and prepare to sink into Flutter these two days. I don’t like Dart.
The learning method is always What/Why/How: What is it, Why is it designed this way, and How to do it?
① The most annoying language of 2018?
The language of Flutter would indeed die without it. Programming language also depends on dad
② Optimize for clients
This is the official position.
Client-specific optimization is mainly reflected in the development experience and operational performance
JIT
(Just in Time) Fast compilation works, which is the basis of Hot Reload.Hot Reload makes Flutter feel like a Web development experienceAOT
Generate efficient native code Ahead of Time. You can get faster running speed and startup speed- On the other hand, Dart is a language that is very similar to JavaScript. Syntax, single-thread/event loops, event-driven, async/await, Isolate, Generator, Future/Stream, Collection if/for are comparable to JSX
- Support compiling to JavaScript. Browsers are important clients, how dare you say client optimization without JavaScript support?
③ Object-oriented
The syntax is very similar to Java, some syntax sugar is quite sweet.
-
There is no keyword to distinguish between class and interface
-
Mixins. The front-end should be familiar with the concept of Mixin, after all, so many people use Vue?
-
Operator overloading. Javascript does not support operator overloading. So it’s a new thing for the front end. But I don’t recommend it. JavaScript works pretty well without operator overloading. Moreover, the semantics of the operators are not clear, which can add mental burden, so it is better to use well-defined methods. Meaningful names are better remembered than symbols.
-
New optional. Make code more concise in some scenarios, such as the Flutter component declaration. Kind of make up for not having JSX.
void main() { runApp( Center( child: Text( 'Hello, world! ', textDirection: TextDirection.ltr, ), ), ); } Copy the code
-
Callable Classes. Grammar sugar, I didn’t think there were any application scenarios.
class WannabeFunction { call(String a, String b, String c) => '$a $b $c! '; } var wf = new WannabeFunction(); var out = wf("Hi"."there,"."gang"); / / 🍬 Copy the code
Can I just wF. Call? Inspired by JavaScript? JavaScript functions are also objects that can have their own attributes
(4) operator
Dart also has some interesting operators/expressions to see how sweet they are:
-
Cascade Notation. The cascade operator is a very sweet syntactic sugar. No nonsense, look at the code:
querySelector('#confirm') // Get an object. ..text = 'Confirm' // 🍬 dessert, which is an enhanced version of jquery-like serial calls ..classes.add('important') ..onClick.listen((e) => window.alert('Confirmed! ')); Copy the code
Is equivalent to:
var button = querySelector('#confirm'); button.text = 'Confirm'; button.classes.add('important'); button.onClick.listen((e) => window.alert('Confirmed! ')); Copy the code
-
Collection Operators. This grammar sugar will also be sweet. It was painful to describe the Flutter view with Dart in the early stage. Dart has added sweet spots like the expansion operator, Collection if/for, named function arguments, and the new option to make it close to JSX
[0. list]; [0. ? list];// Supports null-aware expansion operators // collection if var nav = [ 'Home'.'Furniture'.'Plants'.if (promoActive) 'Outlet' ]; // collection for var listOfStrings = [ '# 0'.for (var i in listOfInts) '#$i' ]; Copy the code
⑤ Const /final vs. static data
Dart uses const/final ina variety of ways, from modifying variables to instance variables to object creation.
Note: Static data and immutable data are different concepts
- Variable modifiers
final name = 'Bob';
const bar = 100000;
const foo = [];
const baz = [];
console.log(foo == baz); // true compile-time constant
Copy the code
Const is treated as a ‘compile-time’ constant. Optimized relative to final
- Modify object creation
var foo = const [];
Copy the code
Dart instantiates the object in a const context by default:
const primaryColors = [
Color("red"[255.0.0]),
Color("green"[0.255.0]),
Color("blue"[0.0.255]]),Copy the code
PrimaryColor is decorated with a const modifier, so all object creation below it implicitly uses a const modifier. The above code is equivalent to:
const primaryColors = const [
const Color("red".const [255.0.0]),
const Color("green".const [0.255.0]),
const Color("blue".const [0.0.255]]),Copy the code
Dart’s built-in container objects support const by default. For custom classes, you need the class to provide const constructors, and all instances must be final.
class ImmutablePoint {
static final ImmutablePoint origin =
const ImmutablePoint(0.0);
// All instance variables must be final
final num x, y;
// const constructor
const ImmutablePoint(this.x, this.y);
}
Copy the code
⑥ Type system
-
Nominal type. No Duck Duck Duck 🦆🦆🦆
-
Sound Type System(Soundness, strict Type System). Static typing + runtime checking. For example, if you assign an int to a variable whose static type is String, the compiler will report an error. However, we can bypass the compiler check by some means, such as casting. Sound Type System can do Type checking at run time to catch these errors.
main(List<String> args) { int a; a = "1" as int; Static type checking is bypassed, but an error is reported when run } Copy the code
Benefits:
- More robust code
- Conducive to AOT
- As far as possible to eliminate bugs, the compilation phase of the fish will be detected, do not hold a fluke mentality.
-
Dynamic: Can you view this as any? Avoid using
⑦ Asynchronous asynchronous
Future/Stream, async/await, Generator. Needless to say, familiar with JavaScript a look to understand
Today metaprogramming
-
The MetaData. Similar to Java annotations. As the name suggests, MetaData provides more information about your code. It can be used to prompt the compiler and retrieve MetaData information at run time through the reflection library.
class SmartTelevision extends Television { @override voidturnOn() {... }/ /... } Copy the code
-
NoSuchMethod (). Similar to Ruby’s method_missing. Called when no property or method is found, you can implement some dynamic properties or methods. Metaprogramming artifact. You can achieve the same effect with a Proxy in JavaScript.
To be honest, Dart doesn’t have many compelling features. You can see many other languages in it, such as Java, JavaScript, Swift… No wonder, modern programming languages look more and more alike.
The good thing is that it’s incredibly easy to use. The bad thing is that I can’t see any other reason to use it except for Flutter binding.
practice
With the help of existing experience, you can quickly get started, this time can immediately start to write is the best.
You can start with Hello World, or you can start with the official introductory tutorial. The Dart.
hello world !
void main(List<String> args) {
print('Hello, World! ');
}
Copy the code
Some key information can be learned from the simplest hello World:
- C – like prefixed type declarations.
- Main entry
- The semicolon cannot be omitted
- The standard library. The print is derived from the
dart:core
This package is global - The List array
- The generic
- .
Flutter up!
Learn more about the language
If you love the language and want to take your relationship to the next level, you need to know more about it:
- Understand its best practices
- Read the language specification
- To build the wheels. Write a static web page generator; With an object-oriented language, you can implement several common design patterns
- Learning standard Library
- Understand the implementation principle
- Understand performance analysis and optimization
conclusion
Programming languages are threefold:
See the mountain is a mountain, see the mountain is not a mountain, see the mountain is still a mountain
- ① I use the best language, such as PHP, which is awesome
- ② All languages are similar, essentially the same
- ③ Back to language, language is just a tool. Just like a painter’s brush, it is only a tool to realize his idea. At this stage, we stop arguing about the best language and choose different brushes for different effects.
A programming language is never the threshold, or the easiest problem to overcome, as long as it solves the problem we need to solve. Just as others teased Flutter to use Dart instead of JavaScript.
extension
- Seven weeks seven languages
- Wiki: Comparison of programming languages
- Dart Official Documentation