Slashdot Mirror


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.

Okay, on to yours!"

12 of 312 comments (clear)

  1. Self-execution by BollocksToThis · · Score: 4, Informative

    My favourite thing is languages that can execute strings of their own code.

    For example, clipper can do this via blocks:

    cVar := &("{ || nVar += 43")

    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.
    1. Re:Self-execution by BollocksToThis · · Score: 2, Informative

      I'm not sure what you mean by 'valid', but I'm going to go with 'useful'.

      Say you have a database with multiple tables that all require user data entry. You can write code for each and every data entry screen (apparently the "visual basic" school of thought), or you can write a generic data-entry screen and include pieces of code for specific field validation in the database itself - so in effect, based on structural tables, the program builds the correct code for each screen. This also saves having to distribute a new version of your app every time you update or add a new table.

      Another good reason might be embedding pieces of code in an XML document or some such, but that seems a little less useful than the database idea.

      --
      This sig is part of your complete breakfast.
  2. This "ask Slashdot" is a concurrent "cross-post" by Paul+Bain · · Score: 4, Informative
    The poster, johnnyb, also asked this question on Advogato just a short time ago. It will be interesting to see the differences in the comments made there and the ones made here at Slashdot.

    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).
  3. Re:C pointers and arrays by ComputerSlicer23 · · Score: 3, Informative
    As I recall, in C x[y] and y[x] are defined to be identical. I believe you can do 1[y] and have it work. That's because a[b] is must be identical to *((a) + (b)). I'm over using paranthesis intentional. Because addition of a pointer and a constant is communitive, either one works. Because (1 + y) is a legal, and returns a pointer it works.

    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

  4. Lameness Filter - Java Correction by mosel-saar-ruwer · · Score: 2, Informative

    The java code should have read as follows [I didn't notice that the shift operator had been caught by the /. HTML filter]:
    public class SixtyFourBit
    {
    public static void main (String args[])
    {
    long theLong = 1;
    theLong <<= 32;
    theLong += 1;
    System.out.println("theLong = " + theLong);

    double [] theDoubleArray = new double[theLong];
    }
    }

  5. emacs solves that problem by mkcmkc · · Score: 2, Informative
    I use emacs, so moving blocks around with proper indentation is quite trivial. Even if I had to edit with Notepad, though, it'd be a small price to pay for the readability.

    Mike

    --
    "Not an actor, but he plays one on TV."
  6. Re:Lists by bill_mcgonigle · · Score: 2, Informative
    Perl does have this, to an extent, but only on static lists.

    What do you mean by 'static lists'? Do you mean they're immutable?

    You can do
    push(@{$outer[$inner]},$value)
    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.
    man perlref
    should be helpful.
    --
    My God, it's Full of Source!
    OUTSIDE_IP=$(dig +short my.ip @outsideip.net)
  7. Re:Many by PSUdaemon · · Score: 3, Informative

    x ^= y
    y ^= x
    x ^= y

    bitwise, no overflow worries...

  8. Re:Speed and RAM by krakrjak · · Score: 2, Informative

    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.

  9. Re:anonymous inner classes by Bazzargh · · Score: 3, Informative
    Java's inner classes (anonymous or named) are not even first class! (Try coding an inner class that refers to a non-final attribute in its enclosing scope.)

    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.

  10. Don't bash C++ by Chemisor · · Score: 4, Informative

    > #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;

  11. My must haves... by Fahrenheit+450 · · Score: 2, Informative
    Things I've found to be invaluable to the way I program:
    • 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-