开发者

Code Examples For Programming Languages

开发者 https://www.devze.com 2023-01-09 08:26 出处:网络
When you are designing a new programming language, or comparing existing programming languages, what types of code examples should you write? They should:

When you are designing a new programming language, or comparing existing programming languages, what types of code examples should you write? They should:

  • emphasize the important features of the language(s)
  • take considerations into important/common features
  • allow learning by examples.

Also, list some s开发者_如何学Goimple algorithms that worth to be written as a showcase?


The code examples should:

  • Show how to start a fresh app (ex. Hello World)
  • Show how to do common patterns (ex. Instead of loops, functional languages use tail-recursive calls)
  • Show what makes the language unique/right for certain tasks (ex. Meta-programming in Ruby, Pattern-matching in Erlang)


The best code examples for a language demonstrate why that language is better than any other language for that particular piece of code. Essentially the opposite of "hello world".


You should write real programs that become easy to write or extend because of your new language features. If you use libraries, account their complexity.

Of course this is nothing I propose to do. Write your programming language in a way that those real programs you're interested about become shorter and better. Only idiots care about features. You write programs with a programming language, it's not a decoration. Therefore you should concentrate on program development task and ignore aesthetic parts of your language that do not contribute on the usability.

Start with the simplest language you can write a program on. Improve the language iteratively from that and work with all issues you had with earlier versions and other languages.

There's a problem you're solving by writing a new programming language, right? Emphasize how that problem gets solved with your language.


One example I see more often in newer dynamic languages is a simple static web server. I first saw an example of a tiny web server in Tcl (not a new language) a few years ago. Now it seems most new languages have a web server written in under 50 lines of code.

Google's Go language actually has a tiny web server as one of the example code in its official documents. Although Go cheats a bit by using a library. But it's a good showcase of how good its networking library is. Node.js also include a web server example in its official docs.

If your language support writing a simple web server in under 50 (100?) lines of code then you should use it as an example. A web server is a good example because it shows you how the language handles networking, file I/O and string manipulation. And lets face it, apart from 3D games and physics simulations most code these days deal more with networking, file I/O and strings than numbers.

0

精彩评论

暂无评论...
验证码 换一张
取 消