Favorite Programming Language Features?
johnnyb asks: "I'm curious what everyone's favorite programming language features are. I'm looking for both the general and the specific. I'm especially looking for features that few people know about or use, but are really useful for those who do know about them. What are your favorite programming language features?"
"A couple of examples to kick off the conversation:
- Continuations
Continuations are very interesting, because they can be used to implement a number of flow-control features such as exceptions, coroutines, cooperative multithreading, and are better at modelling web interactions. This is a more general feature, but most people use these in conjunction with either scheme or ML.
- Tuple-returning
It is a huuuuge time-saver when languages like Perl allow functions to return tuples. Instructions like '($a, $b, $c) = $sth->fetchrow_array()' is a wonderful thing.
- The flip-flop operator [Perl's '..' operator]
Another perlism that I just think is cool. Read more about it here.
My favourite thing is languages that can execute strings of their own code.
:= &("{ || nVar += 43")
For example, clipper can do this via blocks:
cVar
Python has the same thing via "exec":
>>> b
NameError: name 'b' is not defined
>>> exec "b=2"
>>> b
2
This means you can build up strings of code at runtime and execute them, or store field-specific database logic in another database table, and fetch it when needed.
C# is not quite so convenient - you have to build up a complete class and compile it, but it can all be done in memory at runtime so it's just a little more work. Clipper and python can both affect the current scope directly (which can be both bad or good, I suppose).
I believe ruby has blocks similar to clipper (probably better), but I don't use it, so I'm not sure. I also don't use perl, so I have no idea if it supports this...
This sig is part of your complete breakfast.
Hey, johnnyb, where else have you posted this question? When you get answers, will you analyze them and post your conclusions? It could be interesting.
A lawyer & digital forensics examiner. Also an expert on open source software (OSS).
Personally, I think it's a completely crappy thing. You should get an error telling you: "Attempting to use a constant like a pointer".
So it should print out "10\n10\n";
If that's an ANSI C complier: you should get a warning about no return in main, an illegal declaration of main, and an unknown function printf. You might get away with main, because I believe all functions implicitly return an int.
Kirby
The java code should have read as follows [I didn't notice that the shift operator had been caught by the
Mike
"Not an actor, but he plays one on TV."
What do you mean by 'static lists'? Do you mean they're immutable?
You can do
to add to a list in a list. The syntax is tough, but I think it does what you mean. @{} is like a cast-to-list. should be helpful.
My God, it's Full of Source!
OUTSIDE_IP=$(dig +short my.ip @outsideip.net)
x ^= y
y ^= x
x ^= y
bitwise, no overflow worries...
Perl has a standardized documentation feature: PODs. You can embed all manner of documentation alongside your code and then see the output (much like a man page) by running 'perldoc [perl module]'. If you use classgen you get a POD skeleton in your perl modules.
I do agree that standardized documentation built into a language is quite powerful.
This isn't quite right. First off, they are first class, and you can refer to a non-final attribute from a named or anonymous inner class. What you can't do is refer to a non-final local variable from an anonymous inner class.
The intended effect is similar to closures - variables referenced from the enclosing scope have the value when the closure was instantiated (see, e.g. Scheme) - except that unlike scheme, you can't modify the now-private copy. If you want a modifiable copy, you just make one, like so:
final finalFoo = foo; Object bar = new Object() { private myFoo = finalFoo; // myFoo now acts as 'foo' would if this was // *really* a closure.
}
I'd agree that the construct sucks. I'd rather be in a language with closures myself.
> #1 Garbage collection. a.k.a. automatic memory
> management. Not very sexy, but by far the single
> biggest productivity boosting feature of any
> language. I hate housework. It is just a waste of time.
Garbage collection does not free you from memory management. It simply converts one kind of problem into another: namely it eliminates accesses of unallocated memory, but it creates memory leaks instead. The thing is, it is not always easy to figure out when you no longer need a block of memory. That's with garbage collection it is supposed to be good practice to "free" your pointers anyway, by assigning NULL to them. Why they can't just use STL containers instead, I don't know.
> #2 No pointers, no buffer overruns, no memory
> corruption. Related to the first point. Memory
> corruption is just so hard track down. You can
> keep your pointers
You won't have any memory corruption if you don't use arbitrary indexes to access your arrays. For example, when iterating over a container, you run your iterator from ctr.begin() to ctr.end(); no corruption possible. The other cause of memory corruption is using unverified data to directly access your arrays. That happens when you ask the user for a number and then use it to index; this is wrong in so many ways, I can't even begin to list them all. Verify your data, and you will not have any data corruption.
> #3 Stack traces. Not a language feature per se,
> but it takes a lot of the drudge work out of
> debugging.
#include
backtrace_symbols()
> #4 Python's 'for' loop for iterating over the
> contents of a list or array:
>
> for thing in myarray:
> mutate(thing)
#define foreach(t,i,c) for(t i = c.begin(); i #5 Dictionaries, a.k.a. associative arrays. It
> just makes a lot of problems much much simplier
> and faster to solve.
map m;
> Sure, most other languages
> have dictionaries available as a class, but when
> they are seamlessly built into the language you
> use them as easily as any other primitive '
> datatype.
You can use map as easily as any other primitive data type of the same category: as an array.
m["january"] = 31;
cout "january has " m["january"] " days" endl;
- Strong typing: Much like pig and elephant DNA just don't splice, one should not be allowed to add characters to integers then divide by floats and expect a sensible result.
- Type inference: I want a language that can figure out what I'm doing without having to specify every last detail. And I want it to be able to tell me when I'm trying to splice said pig and elephant DNA.
- Higer order/first class/anonymous functions: makes life and coding so much nicer.
- Pattern matching: pretty and powerful.
- An interactive environment: why should I have to compile my whole freaking program to test one little function?
- Paradigm neutrality: if a problem is best solved with object, let me use objects. If an algorithm is most naturally expressed functionally, gimme that. If imperative code is what I need, let me use it. If I want to mix'n'match hand me the blender
- Native and byte-code compilation: choose between speed and portability depending on your needs
Thank goodness there's OCaml there to provide all this for me in one happy little package...-30-