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.
Next I love ruby's block system, especially for stuff like this.It comes in super handy for a ton of stuff. Especially when I'm doing XML.
Also, there is another thing that I first discovered in pythonSave me a temporary variable, w00t.
Lastly, its not really a language feature, but in any object oriented language I love being able to serialize the objects. It's so simple to use pickle or any other serialization library and just write objects out to file or network. I never have to design a binary file format ever again. It's even better when you use Ruby and you can marshall objects into XML or YAML with a single method. Then you've got a human readable and editable file format that you can magically transform into objects again later. Super useful.
The GeekNights podcast is going strong. Listen!
This is less of a favorite feature, and more of a feature I wish we had. What about having the representation of the language independent of the code itself? I think this will eventually happen and could really revolutionize things. I believe the inklings of separating 'physical' representation from the code were there in some languages like Algol 60 and CS work in the 1960's, but it never caught on (perhaps hindered by other features of those works?).
... then ..."? Better yet, really like Perl's "$_"? If you want it to be displayed like this, turn it on. Otherwise, say you don't like this feature, and it will automatically replace the "$_"'s (either implicit or explicit) with the variable to which it refers. Again, no problem.
In a little more detail, suppose I write a C program. It will have lots of functions and conditionals with their "blocks" surrounded by braces.
But what if I prefer my "blocks" to be started and ended by brackets instead of braces. Better yet, what if I am tired of typing these and would like indentation to control this. Or whatever -- start end commands, if you like. The point is that these are minor sytactic idiosyncracies, and we all have preferences. Why not store the code in an underlying format (XML would be okay, were it not for the bulk of it)? As long as there is a one-to-one correspondence between all possible representations, you could view it however you want.
And so on for all syntactic features. Prefer "if-fi" construction to "if () {}"? Or "if
At this point, I feel like I am repeating myself, but let me continue for a little bit. It would let each user have his/her personal favorite representation. We already let them control the colors of their syntax highlighting, lets take it a step further.
Hell, if you want to use a graphical viewer for those C programs, akin to LabView, go for it! Or (in my opinion) a much better graphical programming environment with a graph structure. The point is: you write it how you want and save it. It appears to another coder how he/she wants it to appear, but the content is exactly the same.
In short: why isn't this done? It seems like a spectacular step in unifying programming languages a bit, and letting each user tailor his preferences while maintaining compatibility. As long as there was simple one-to-one correspondence, the translation from physical representation to underlying code and back would be quick and fairly easy to handle. Are there any modern projects which attempt this? Or *any* which attempt it with some success?
On a somewhat related note, is it possible to put a "hook" to a comment in the code, and with the proper viewer have that comment displayed along with the code (say when you click the "hook", move your mouse over it, or drag the "hook" to a "comment box")? If this last paragraph doesn't make sense, please ignore it.
Closures and Currying are two of my personal favorites.
Overloaded Functions are sweet.
I am also quite fond of operator overloading.
Just to confuse people do this:
main() {
int x;
int y[2];
x=1;
y[1]=10;
printf("%d\n", y[x]);
printf("%d\n", x[y]);
}
What will happen?
Functions as first class citizens, that is functions can be returned from functions and provided as arguments as functions. The basis of the functional paradigm and it makes life much much simpler.
Pattern matching (some ML:)
fun has_a [] = false
| has_a 'a'::_ = true
| has_a _::xs = has_a xs;
Simple elegent functions requireing much less if_then_else's.
Automatic garbage collection and bounds checking: enables me to write the code to do the job, not the memory management.
Polymorphic typing: I can write general functions:
fun contains x [] = false
| contains x x::_ = true
| contains x _::xs = contains xs;
That will work with any type for which equality is defined.
These are the reasons I hate C for general programming. The most important thing is efficient algorithms, without them no amount of low level optimization will help. With good algorithms, functional languages are now normally at least as fast... (and much much easier to debug and even verifiable).
From non-functional languages, the object model is wonderful when used properly.
Smalltalk & co's complete environment is a nice feature.
I also have a soft spot for BBC BASIC with its speed, interactivity and simplicity. These are combined to allow windowed applications including at least one web browser and anyone can start programming simple programs (which is missing from most modern computers)
Then there's the specialist languages. They have all sorts of nifty features (Mathematica is a good example) but I wouldn't expect them in an everyday language.
This is not the issue it was ten years ago, but one feature I absolutely want tightly integrated into my language is robust string handling. I still like perl's best (although perhaps only because I know it best). It simply seems to be more tightly integrated into the language as a whole.
"He who would learn astronomy, and other recondite arts, let him go elsewhere. " -- John Calvin, commenting on Genesis 1
I use Common Lisp.
Lisp has almost no syntax, so it's extremely regular (barring exceptions like LOOP). Because it's so regular, it's easy to build macros that do powerful things.
Macros can completely transform the source, at compile time, but with the full power of the language. Having that ability, together with simplicity, means that it's easy to build a complete mini-language for one's manager or web designer to use on the site, and easy for them to learn it, since you can explain the syntax in 5 minutes or less, and they don't have to learn 50 built-ins to use it.
Common Lisp's conditions system not only allows exception-handling, like Python, but can also have an entire protocol for controlling execution flow built on it. More about that in the conditions chapter of Peter Seibel's forthcoming book.
Lastly, having a generic-function-based object system means that a method can "belong" simultaneously to more than one class, at the same time. So, instead of having a method inside a class, you call a method with any number of objects of various classes, and it figures out from the type of the object what method to run, of all the similarly named methods. You can even specialize a method on a particular object or objects, instead of a particular class or classes! Multiple dispatch rocks.
Property law should use #'EQ, not #'EQUAL.
Ranum is citing this as an example of a way that existing tools -- such as GCC -- could be enhanced in such a way that programmers using currently popular languages (C/C++) would have a better security safety net without having to be retrained in practices (like checking for buffer overflows) that while obvious are still under-utilized in most software. The whole article is interesting reading, but this remark about Perl's taint mode seems like one of the best concrete examples of a modern protective language feature.
DO NOT LEAVE IT IS NOT REAL
This is what I want: For the first criteria, if, at any point, you are forced to make contact with a 32-bit environment, then the platform fails the test.
For instance, if the platform requires you to use either Java or SQL, then it fails.
SQL fails because it is essentially an ASCII-based language that has almost no sense of primitive data types whatsoever, and its only undefined binary type, the binary BLOB, maxes out at 2^32 = 4 Billion bytes [so much for high-quality MPEGs of, e.g., Gone With The Wind].
Java fails because, as recently as the Java 1.5.x Beta, it cannot take long ints as array indices. For instance, the following will not even "compile" [i.e. "javac," whatever that is] under the Java 1.5.x Beta:
So your "middleware" for this hypothetical 64-bit platform is forbidden to touch either SQL or Java.As for the strong data typing, I want the environment to be natively aware of
According to Professor Kahan, Matlab has [or has had] some serious problems here: So: Any takers?Languages without "real" unification feel unexpressive or too low level. Whenever I program in something other than prolog (which is most of the time) I miss it.
Example: [foo(A,x)|B] = [foo([p,q],x),C,d,e].
which implies that A = [p,q], B = [C,d,e], and C remains unconstrained.
There are 10 kinds of people: Those that understand ternary; those that don't; and those that don't care.
You could do this in perl if you wanted to. For instance, you can code perl in Latin. It's done using the Filter::Util::Call module, which lets you preprocess your perl code. Read Damian Conway's discussion about it. He gives a simple example using Klingon keywords and talks about implementing a Switch function in perl.
My God, it's Full of Source!
OUTSIDE_IP=$(dig +short my.ip @outsideip.net)
One thing I've always wondered (since I mostly do C and not much into C++), how is this different than something like pointers to void in C? For example, the way qsort() works -- it can operate on any data type also.
I'm surprised I haven't seen this listed yet. In java I use reflection often and at the cost of a little processing overhead, it allows me to add remove fields from a database with very little code change.
I generally like to use struts in web applications and find I often have to tranfer values from an action form to a java bean and vice versa. Using the org.apache.commons.beanutils package it is incredibly easy to do this, and beanutils uses reflection to do this with.
My second choice would be good regexp support. IMO, Perl is the best at this and I also like the Java regexp package in 1.4 as well.
#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.
:)
#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, I've already got an OS, I don't need to write my own.
After spending years programing asm and C on a platform with no memory protection (Amiga), and then later C++, I think I've paid my dues here.
#3 Stack traces. Not a language feature per se, but it takes a lot of the drudge work out of debugging.
#4 Python's 'for' loop for iterating over the contents of a list or array:
for thing in myarray:
mutate(thing)
It is easy to remember, easy to type, much easier to read, and you use it _all_ the time. Compared to the same code in C, C++ or Java, it is a godsend.
#5 Dictionaries, a.k.a. associative arrays. It just makes a lot of problems much much simplier and faster to solve. 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.
--
Simon
In the days of 8-bit micros, as games became more advanced, allegedly self-modifying code was used to get enough stuff in the sparse RAM available and for performance reasons when a dynamically-modified unrolled loop was required IIRC.
Stick Men
I dont remember what the syntax actually was but when you defined a structure, you could point into its members with with.
like if the structure had members x and y and structure was named coords you could do something like this
var own_x;
with coords
begin
x=own_x
y=123.13
end
yush
Actually what I'm wanting to do, eventually, is write a book about great programming constructs people have probably never heard of, or don't understand well.
My last book took me 3 years to find the spare time to finish, so I don't suspect I'll have this done anytime soon.
I was originally going to just analyze scheme's features, but then I realized that many languages have features that need to be recognized, too. my original outline was going to be:
* Memory Management
* Symbolic programming - an intro to Scheme
* Functional Programming & Functional Programming Patterns
* Closures and higher-order functions
* Advanced Flow Control w/ Continuations
* Compile-Time programming 1: Macros
* Compile-Time programming 2: Partial Evaluation
* Compile-Time programming 3: C++ templates
* Lazy evaluation
* Lazy data structures
However, if I decide to open it up to other languages, I have no idea how I'm going to organize it or even how I will decide what to include.
Anyway, it was originally posted just to Advogato, but then I remembered that the only threads on Advogato that get any real response are flame-wars, which is sad because Advogato could be a real cool place. Then I thought "you know, this would make a good 'Ask Slashdot' as well. However, I don't expect the quality of responses on Ask Slashdot to be as good, although I expect there to be a LOT more of them.
Engineering and the Ultimate
Unrestricted integer size (e.g., Lisp bignums)
Have persistent objects between program invocations. It's so tedious and buggy to have to write to a file when a program ends and then read it again when you start it up again.
I don't look at the features alone in a language. Usually, I look at what its primarly used for. I use C because it's a general-purpose language, that has a strong issued standard, with ANSI compiler compliance. I find that beginner programmers who like languages with lots of features(i.e C++), they get carried away and don't learn the language like they should. C++ is an extremely complex language, that is hard to use correctly.