tags:

views:

885

answers:

17

What programming language do you prefer for code examples?

I want to provide clarity to the most people possible when asking a question (while only using one language). While a specific language may provide clarity (for people who know it), it may not be clear to the largest audience.

Edit: Another consideration, does syntax highlighting work better for some languages?

A: 

It depends. I will not try to answer an Actionscript question with C++ STL ;)

However, for generic problems, like regex, I follow the language I have the best grip on. And I try to write what I am upto just in case the code isn't very lucid.

dirkgently
+3  A: 

This site is most heavily .NET focused, so C# or VB.NET (for overall readability). But in reality it shouldn't matter. Unless you pick a language that is way out there, people are going to be able to glean the meaning of the code if not understand every nuance of the syntax.

TheTXI
+26  A: 

If the question you're asking is language-agnostic, use pseudocode. Why use a specific language when dealing with general problems?

Welbog
As a language, I'm not sure that pseudocode is a very popular one, personally I've used it only briefly in college. It also may not capture some of the latest concepts in programming (for example, it omits variable declarations) does it? Or are you referring to just impromptu examples people create?
Brandon
Really? I love pseudocode; I just keep having compiler issues. Good for code examples, though.
Rob Lachlan
I think using pseudocode makes it too easy to be imprecise about things. Writing code in a "real" language that would be executable forces you to be more precise.
dsimcha
*As an abstraction of programming languages, I'm not sure that pseudocode is a very popular one ....
Brandon
I don't know... it feels that every book which attempts to describe algorithms in pseudocode end up with a bad BASIC-like dialect. It's even worse when the book aren't in english: in french programming books, you often end up reading BASIC "pseudocode" with keywords traslated in french. I'd take some C, C++, Python, Java or C# anytime over this stuff.
Trillian
A: 

I try to match the code to the question at hand if it is language specific, and ideally it will compile/run and actually solve their problem. If the question is language agnostic, I just use a pseudo-code that tends to look a lot like a python/C# hybrid.

grieve
+3  A: 

You could do worse than C#. Most people are familiar with a C-style curly-brace language of one form or another, and C# strips out most of the pain for basic work. Just avoid lambdas, collection initializers, anonymous types, and other too-recent features that might confuse non-users.

Joel Coehoorn
+5  A: 

Everybody should know a litte bit of C

Oscar Cabrero
+4  A: 

I'd say C# or Java.

Robert S.
+4  A: 

Psuedo-language based on C syntax, but without any C specific hacks, especially without pointer manipulation.

vartec
A: 

If I understood the question correctly, you're asking what would be the language in which you could write code, that would be the most similar to pseudo code ? (so that most people could understand it).

Well, imho, fortran for physics and engineering community, or C family, have always been relatively ok accepted. Python is gaining popularity for general purpose algorithms. I'm mostly going towards numerical algorithms here.

ldigas
+4  A: 

I prefer Python. By far the biggest problem with Python as a language is that all implementations that exist or will exist in the foreseeable future are slow compared to static or JIT compiled languages. This is obviously irrelevant for code examples. Otherwise, it's an incredibly featureful, high-level, readable language.

Other possibilities:

Java, C#: Too verbose for example code. Too many language-specific details.

C, C++: Too low-level. The "what" gets lost in the "how".

D: I mention is because it's my personal language of choice. Not a bad choice except that few people are familiar with it. However, I use it for examples on SO and people some to grok it as long as I avoid very language-specific features. It's syntactically from the C family and modulo features that would never be used in simple example code, it's basically a higher-level C++ or a less verbose, rigid Java.

Perl: The opposite of Java, C#: Terse to the point of being cryptic if you're not familiar with its syntax.

Functional languages: Might not be bad if your audience is familiar with them and your code lends itself naturally to being implemented w/o mutable state. However, the rigidity of programming without mutable state can obscure the point you're trying to make.

On the other hand, I don't like using pseudocode because I feel that using a real language and trying to produce code that would run forces one to be more precise than if pseudocode is used.

dsimcha
A: 

Object Pascal has a nice clear syntax without being overly verbose. It ends up reading very similar to an actual pseudocode sample.

Example: Levenshtein Edit Distance in

Pseudocode

Object Pascal Code

JosephStyons
+3  A: 

The best is one that is readable. If you'd like pseudocode that you can use as real code also, then check out Python or Boo.

Michael Trausch
+1  A: 

I use pseudo-english (or any written language you (and your target audience) know best) for first draft intended to non-technical:

If my data is valid then
  transform the data
otherwise
  warn the user.

The next iterations will be more and more closer to the actual programming language I will use :

if IsDataValid(data)
{
  TransformData(data)
}
else
{
  WarnTheUser(data)
}

I usually stop when I have to write down API/SDK functions.

Max
A: 

With apologies to Knuth and his superb collections of algorithms in TAOCP, it's not MIX or MMIX.

Jason S
The vast majority of algorithms in TAOCP are given in English: MIX/MMIX is only used when implementation details are relevant.
Michael Dorfman
A: 

I would simply program the examples 'fluently' using a familiar readily available conventionally syntaxed language that emphasizes code, as in any of the curly braced languages. How about D as a not so arbitrary choice:-)

I would avoid a language that would tend to 'crowd out' your code with either too wordy or too abstruse syntax.

Eiffel is well suited for emphasizing good programming practices.

For a pseudo code like examples, how about something like PDL (Program Design Language) PDL—A Tool for Software Design" (Caine and Gordon 1975)

Roger Nelson
+3  A: 

It really makes no difference, as long as the pseudo-language is sufficiently generic. E.g.

if ( kettle.temp >= 100 ) {
    kettle.switch_off();
}

If kettle temperature >= 100:
    Switch off kettle

Not generic:

(if (>= (temperature_of kettle) 100) (switch_off kettle))

la c1, kettle+8
li c2, 100
jlt A1, c1, c2
push kettle
jmp switch_off
A1:
rjh
Devil's advocate: your "generic" snippets are better described as "generic imperative language", while the other two I would categorize as "generic applicative language" and "generic assembly language". Who is to say what's more generic? It all depends on your background...
Derrick Turk
No -1 for you though, because (unfortunately, from my point of view) Joe Programmer is much more likely to quickly understand the first example. He may even use big words like "intuitive" to describe it.
Derrick Turk
It does depend on your background, yes, so I suppose 'intuitive to your target audience' is a better term than 'generic' - I'm assuming that most programmers are proficient with imperative languages, while relatively few are *only* proficient in a non-imperative language.
rjh
I agree, except I would put extra scare quotes around "intuitive", because I feel it's pretty meaningless in this context.
Derrick Turk