Ming-Tang
Ming-Tang

Reputation: 17651

Code Examples For Programming Languages

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

Also, list some simple algorithms that worth to be written as a showcase?

Upvotes: 2

Views: 1052

Answers (4)

slebetman
slebetman

Reputation: 113974

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.

Upvotes: 0

Cheery
Cheery

Reputation: 25443

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.

Upvotes: 1

Jon Smock
Jon Smock

Reputation: 9641

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)

Upvotes: 4

joe snyder
joe snyder

Reputation: 3659

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".

Upvotes: 2

Related Questions