Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

The logical companion to the Which do you hate most question. What's your favorite syntax element in a programming language- what nicety to you like best? I'm sticking with the 'syntax' specification to avoid broader answers like "dynamic typing" or "is interpreted."

share|improve this question
1  
This seems like a good candidate for CW. – greyfade Sep 3 '10 at 3:24
1  
@Greyfade: Why's that? (consider meta.programmers.stackexchange.com/questions/8/…) – Fishtoaster Sep 12 '10 at 17:53
show 1 more comment

closed as not constructive by Aaronaught, Walter, Mark Trapp Jul 1 '11 at 17:42

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or specific expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, see the FAQ for guidance.

43 Answers

1 2

Monads (Example is F#)

async {
    let! asyncValue = getSomethingAsync
    if asyncValue = something then
        do! alertSomeone asyncValue
        do! alertSomeoneElse asyncValue
}
share|improve this answer
1  
Another feature borrowed up from Haskell. :-) – Orbling Dec 6 '10 at 22:39
2  
I don't understand this. I wish that I did. – Carson Myers Dec 7 '10 at 3:22
1  
@Carson Myers: Monads have been described as "overloading the semicolon". Namely, you specify your own "bind" function that takes an "action" and feeds it to a function which returns another action. See Phillip Wadler's paper "Monads for Functional Programming", which gives full examples of monads hiding the plumbing of exceptions, state, and output. Note that this paper predates monads appearing in Haskell, so its notation is a tad outdated (it uses a star instead of >>= for the bind operator). – Joey Adams Dec 7 '10 at 6:15
show 1 more comment

As an Oracle developer, the one feature I couldn't live without is how SQL integrates simply and cleanly with PL/SQL.

share|improve this answer

PHP

On-the-fly include()

It simply work great for inserting a function library, 3rd party API, headers, footers, and really anything else I can think of. There is no rule as to where its placed within the file, so I never have to fight with my compiler.

<?
// the web page
include('header.php');
echo '<span>some HTML I want here</span>';
include('body-template.php');
echo '<span>some stuff I want here</span>';
include('footer.php');
?>

oh oops, I forgot my javascript I want in my footer..

<?
// footer.php
echo '</div>'; // body content wrapper
echo '<span>my footer is just this line.<br /></span>';
echo '</html>';
include('javascript.js.php'); // <--- yeah I just added this just now, no problem
?>

For me it makes the process so much easier.

share|improve this answer
show 6 more comments

Surprised no one mentioned keyword methods in Smalltalk, one of the languages best features.

A method can have a name broken up by colons, like:

Table >> atRow:column:put:

(The >> is just a documentation shorthand for showing the class that owns the method.)

To call it, you'd use each part of the name as a specifier:

aCell := DataCell new.
aTable := Table new.
aTable atRow: 3 column: 2 put: aCell

I find it much more readable than table.addCell(3, 2, aCell); even if it is a little more verbose sometimes.

share|improve this answer
show 1 more comment

The ?: and ?. operators in Groovy. They handle any nulls that may happen during a sequence of method calls, so you can do stuff like:

food = pizzaChain.nearestOpenPlace?.orderPizza() ?: makeSandwich()

..and not worry if there's still a pizza place open or not :)

share|improve this answer

In R probably the vectorization in the conditions combined with index power. This allows you to do something like :

x <- 1:10 # a vector from 1 to 10
mean(x[x<5]) # takes the mean of all numbers smaller than 5

This is a trivial example, it works with matrices, arrays, lists, more complex conditions as well. And it's a manyfold faster than any kind of looping structure.

share|improve this answer
show 1 more comment

When you need to construct something on the fly, it's hard to beat JavaScript's brevity.

var obj = {name: "Joe", age: 23};
var list = [1,2,3,4]; // Of course, functional languages do sequences better...
var objList = [{name:"Joe",age:23},{name:"Fred",age:21}];

Compared that to C#, which is a bit more verbose:

var obj = new { name = "Joe", age = 23 };
var list = new [] { 1, 2, 3, 4 };
var objList = new [] { new { name = "Joe", age = 23 }, new { name = "Fred", age = 21 } };
share|improve this answer
show 3 more comments

Plain old for-loop:

for (int i = 0; i < length; ++i)

Wherever I can, I use it instead of the while loop, because:

1.) It minimizes the scope of variables 2.) It puts everything (declaration, initialization, increment,...) in one place.

share|improve this answer

Javascript

Resize the Length of an Array

length property is a not read only. You can use it to increase or decrease the size of an array.

var myArray = [1,2,3];
myArray.length // 3 elements.
myArray.length = 2; //Deletes the last element.
myArray.length = 20 // Adds 18 elements to the array;
//the elements have the undefined value. A sparse array.
share|improve this answer
4  
Well, I never knew that! Though it looks dangerous, as you could easily resize an array by missing an = when you are evaluating the length. eg. if (myArray.length = 2) { // oops! } – Dan Diplo Sep 17 '10 at 18:52
show 1 more comment

Java

It may sound trivial, but I like foreach + generics a lot (or maybe I just hate iterators)

List<String> list = new ArrayList<String>();
for(String element : list) {
    log(element);
}

comparing to:

List list = new ArrayList();
Iterator iter = list.iterator();
while (iter.hasNext()){
      String element = (String)iter.next();
      log(element);
}
share|improve this answer

Scala:

The syntax for constructor parameters:

class Color(val r: Int, val g: Int, val b: Int){
   //Example method:
   def luminosity = (r + g + b) / 3
}

Compare this with the Java equivalent:

public class Color{
   public final int r, g, b;

   public Color(int _r, int _g, int _b){
     r = _r;
     g = _g;
     b = _b;
   }
}

The Java class is much longer than the Scala class without even defining a method. Each field has to be mentioned 4 times. Note that this Java example is generous, since I could write public final int r, g, b all on one line due to the fact that they all share the same type and access modifiers. Even Python and Ruby aren't this concise with constructors - they only cut out the field declarations, reducing the repetition factor to 3 instead of 4.

share|improve this answer

Haskell:

The do statement in haskell allows monadic composition:

do putStr "What is your first name? "
   first <- getLine
   putStr "And your last name? "
   last <- getLine
   let full = first ++ " " ++ last
   putStrLn ("Pleased to meet you, " ++ full ++ "!")
share|improve this answer

Java

obviously the main method

public static void main (String [] args)

Getters and Setters method

Setter

public void setVar(datatype variable) {
  privateVar = variable;
}

Getter

public datatype getVar() {
  return privateVar;
}
share|improve this answer
3  
Good to have properties in C# instead of these getters and setters. – Gulshan Oct 20 '10 at 4:44
show 3 more comments
1 2

Not the answer you're looking for? Browse other questions tagged or ask your own question.