views:

1363

answers:

28

Before I started branching out and learning other languages, I naively believed I could get by with the features Java provided.

Since learning Ruby and some other languages that are very different from Java, I have found that there are some really powerful language features that I miss a lot when I go back to Java.

I am curious, what are the most unique features of your favorite languages that you have found to make you more productive, or enjoy your time programming more than when you are forced to use another language?

I'm talking especially about the features that get you fired up such that you would start writing a project suitable for the language solely so you can use that language.

+5  A: 

I don't know if this is the kind of construct you are talking about, but I sorely miss Linq whenever I go from C# 3.x to... well pretty much anything.

lomaxx
A: 

thing ||= Class.new

its just sexy, and c# can't do it

yeah theres ?? but you still have to do all the heavy lifting and then it botches nullables, this is super simple, if its got stuff in it move on, if not, do the thing on the right. Also the .new instead of new Class(); its just super sexy.

DevelopingChris
+4  A: 

For Ruby, I especially like the following features:

Singleton methods:

obj = Object.new
def obj.say_hi()
  puts "hi!"
end
obj.say_hi

This will let you define new methods on INSTANCES of objects, so these methods exist just for that object. I find this especially useful when I have some kind of agent interface where the agent can execute some commands, but these commands are highly dependent on properties of the agent... for example, in an RPG game, I could have each skill be a singleton method that are added to the player's instance when they are capable of using that skill.

Open classes:

class Array
  def binary_search(comparable)
    # binary search implementation
  end
end

Also known as "Monkey Patching" (among other names), this feature can be pretty useful when used with care (obviously there are dangerous impacts if overused, or used inappropriately). Best practice or not, I find it damn cool and fun to add new methods to standard API classes!

Closures (and higher order functions):

def accumulator(n)
  lambda { |i|
    n = n + i
    n
  }
end
a = accumulator(5)
a(0) # returns 5
a(2) # returns 7
a(1) # returns 8

If you don't know what closures are, learn them and start trying a language that supports them easily! They can do some pretty neat stuff, especially with functions like map (change every element in a list based on a closure) and filter (remove elements based on a predicate closure).

Mike Stone
cool question and answer, thanks Mike. Without knowing it, I always wanted Singleton methods in Java and C#: otherwise you can't mix and match in an object hierarchy, which makes it impossible to define.
Yar
+2  A: 

When leaving C#, I miss 'var', Linq (the extension methods, not the syntactic sugar) and the lambda expressions. I normally leave C# to go to assembly language or C++, rather than another language that has these features.

When leaving assembly language or C++ I miss the warm-fuzzy feeling you get inside when you make Windows API calls work.

Zooba
+5  A: 

In Haskell, I really like guards

fib n | n < 0     = 0
      | n == 0    = 1
      | n == 1    = 1
      | otherwise = fib(n - 1) + fib(n - 2)

Rather than littering your functions with if/elses, you can use a guard in Haskell which basically puts the if statement at the function definition, and thus the version of the function that matches the guard will be invoked.

Mike Stone
+1  A: 

When going from ruby to most anything else,

  1. I miss dynamic typing the most.

    In ruby I'd just make an array of stuff, and some plain old functions.

    in .NET I have to have interfaces and casting and if( blah is x ) and Func<T, IEnumerable<T, TResult>> crufting up my code all over the damn place :-(

  2. Closely followed by literal array/hash syntax

Orion Edwards
maybe it's a coding style thing?
chakrit
+1  A: 

@ChanChan:

I think what you are looking for in C# is the null coalescing operator (??).

Which I first discovered here.

@Orion:

True, not as concise, but still better than not having such a feature, which I believe (sadly) Java does not... unless it is a hidden feature I haven't come across..

Mike Stone
+1  A: 

@Mike Stone

I think what you are looking for in C# is the null coalescing operator (??).

Not quite.

Using the ?? operator, it'd be

var thing = thing ?? new SomeClass()

which is not quite as nice as

thing ||= SomeClass.new
Orion Edwards
+13  A: 

In Python:

List Comprehensions:

It's such a great way of function mapping or filtering a list in one line. Creating a one or two line for-loop just seems excessive now.

taxed_prices = [price * 1.0825 for price in prices if price > 100]

List unpacking:

If you have a list or tuple of a known length you can specify a variable for each on one line and Python will assign the correct value to each.

first_name, last_name = ("John", "Lennon")

for first_name, last_name in name_list:
     print first_name, last_name

In JavaScript:

Object literals:

It's really handy to create a first class object by just typing it out.

myObj = {one:1, two:2};

In Java:

Anonymous classes are really handy. Though they're not as handy in languages like Python and Ruby that have first order functions they still come in handy when you want to quickly subclass a builtin class.

myButton.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {
         System.out.println("click");
     }
};
Cristian
anonymous classes are java's way of dealing with the fact that it doesn't have first-class, anonymous functions
Claudiu
+1 just for list comprehension :-) And generators with the same syntax !
e-satis
+1 for python unpacking and list comprehension
chakrit
+3  A: 

Associative arrays (aka Maps, Dictionaries, HashMaps)

I couldn't think of working without them!

Mark Harrison
Not only associated arrays but the ability to create them without a whole lot of syntactical sugar, i.e. Perl, Ruby, etc.
Dan
+2  A: 

Call me oldschool, but I love the ternary operator

a?b:c;

I also like LINQ and lambda expressions, they've totally changed the way I code in C#.

Jon Limjap
+3  A: 

In short, Python's lists/tuples.

They are very useful for reducing accidental complexity and making the code more concise and the code clearer.

Of the top of my head, they are good for:

  • Iteration
  • Returning multiple values from functions
  • List comprehensions
  • Coupling related values (XY coords, lat/long, etc)

Python's array slicing is very useful too. There was one instance where we were using Python bindings for OpenCV and the C libraries as well, and Python's array slicing made extracting subsections of images much much easier than in the C version.

pbh101
+1  A: 

Learning closures has changed the way I think about Javascript. It's so easy to just specify an inline function to handle an event and have access to the scope of the parent function.

Lance Fisher
A: 

As I'm a very lazy programmer I have started to really like Key-Value coding and bindings in Cocoa/Objective-C. They might be weird but the code becomes so short, can take a while get the head around'em, and when things get shorter they reveal the new power of expression .

Key-Value Coding

Bindings

epatel
+1  A: 

+1 for Associative arrays, but also adding dynamic size arrays here. In Delphi they are arrays, in C# i can use a List, but the functionality is built in and I would not want to miss them.

And Generics, even though all you users of dynamic languages just laugh at that attempt to make a static typed language dynamic.

Michael Stum
A: 

Whenever I go from working in Python to some lesser language I miss the nice built-in list, tuples, sequences, sets, and dictionary data structures.

And the beauty of list comprehensions:

# Real code from my footy tipping calculator
standings = [ (stats.rating, team) for team, stats in ladder.iteritems() ]
standings.sort()
standings.reverse()

Array slicing I also find very useful.

grom
+3  A: 

The function. It is so much nicer to read code that is grouped into functions instead of having one large blob of spaghetti code doing it all.

HS
+2  A: 

Variable interpolation from Perl (among others) is the thing I missed most when I started learning c, C++ and Java.

print "Hello, $name!";

seems so much easier to read than

printf("Hello, %s!", name);

Bill the Lizard
A: 

In regards to C++ and C# I would say that one of the most useful constructs for me has been templates in C++ [1] or generics in C# [2]. While they do have a tendency to get over used in some places, there are other places where they can save you having to write a lot of redundant code.

[1] Templates - http://www.cplusplus.com/doc/tutorial/templates.html [2] An Introduction to C# Generics - http://msdn.microsoft.com/en-us/library/ms379564(VS.80).aspx

Rob
A: 

I love Scheme continuations, as well as Scheme's guaranteed tail calls. :-)

For those who have never used Scheme or any sort of continuations, this introduction may be of interest. :-)

Chris Jester-Young
A: 

Macros, in particular, of the scheme/lisp variety. Hygienic, non-hygienic, and reader macros. Lisp/scheme macros are miles ahead of Preprocessor macros, and they are well worth examining.

Continuations are also really interesting and cool, but I can live without them.

Jonathan Arkell
A: 

D templates, which are much more powerful than C++ templates and form a meta-language that is not only Turing complete, but also usable by mere mortals. More specifically, key features, with some toy examples of how they would be used:

Static if, conditional compilation based on any value known at compile time. Ex:

template factorial(uint n) {
    static if(n == 0)
        const uint factorial = 1;
    else const uint factorial = n * factorial!(n - 1);
}

Template tuple parameters:

T max(T...)(T args) {  //Finds the max of N >= 1 arguments.
    static assert(args.length > 0);  //Tested at compile time.
    T max = args[0];
    foreach(arg; args[1..$]) 
        max = (arg > max) ? arg : max;
    return max;
}

Mixins (allow any string generated at compile time to be evaluated as code.):

bool comp(string sign, T)(T lhs, T rhs) {
    mixin("return lhs " ~ sign ~ "rhs;");
}

The comp function could then be used something like comp!("<")(1, 2), or comp!("==")(1, 1).

Obviously, these are just toy examples, but D templates are pretty powerful when you're trying to write highly generalized code.

A: 

Functions as first class citizens. Their merits shine in functional languages but also Smalltalk (blocks) and everywhere you got them you really got something useful more.

Regards

Friedrich
A: 

Python: list comprehensions, generators (expressions and yield)

Scheme: tail call optimization, really good stack handling (can hanlde very deep recursion), foldl, type-case

Claudiu
A: 

Not really a programming construct but ...

Forkbombs.

:(){ :|:& };:
strager
A: 

Generics in C# (or java). Templated types for mortals.

This has saved me countless lines of boilerplate making type safe collections and repeating algorithms to deal with different types.

Hamish Smith
A: 

Anonymous Functions, definitely.

They make javascript so fun to write, and first class functions provide that ability to do anonymous recursion.

(function(foo)
{
  if (foo == null) 
       foo = "f";
  foo += arguments.callee(foo);
  return foo;
})

And as we all know: "To iterate is human, to recurse, divine."

cazlab
+1  A: 

Higher order and first class functions.

trinithis