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."
|
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.
|
The ternary operator (or for pedants, the conditional operator):
Unfortunately, some languages make it difficult to use due to strict restrictions on what conversions can be performed in order to produce a consistent type from both possible outcomes. Nevertheless, it is a wonderful example of a language taking an extremely common pattern and providing a concise, readable means of representing it in code. |
|||||||||||||||||||||
|
PythonPython's comparison syntax is brilliant. I wish all languages had this. In Python, instead of
you can do
|
|||||||||||||
|
LINQ in C#What could be cooler than creating both simple and complex logic using natural language?
|
|||||
|
|
The List Comprehensions from Python
|
|||||||||||||
|
HaskellThe syntax for "point-free style," in which, ironically, one uses the point (
Point-free:
The functions are equivalent, but the latter is considered "cleaner." |
|||||||||||||
|
|
The C# Extension Method. It's a great way to make static method use a little cleaner and clearer:
|
||||
|
|
|
I'm still on the new language high for C#, so: Properties
A simple way to have the benefits of accessors (the ability to modify the implementation without changing the interface) and the benefits of public variables (brevity). |
|||||||||
|
|
Anonymous Functions (aka Lambda Expressions) Sometimes you don't have or want to name everything...
|
|||||||||
|
PythonArray slicing is very elegant.
|
|||||
|
C# null coalescing operatorthe ?? operator which returns the left-hand operand if it is not null, or else it returns the right operand. I often use it like so foo = foo ?? GetNewFoo(); |
|||||||||||||||||||||
|
|
Python's lack of braces and the fact it uses whitespace for code indentation. That combined with pep8 leads to one consistent code style across almost all python code. It also makes the flow of code much easier to follow because you're not wasting lines on syntactic salt. Before I started with python I hated the idea, but once I started coding in it, it's become second nature and anything else looks ugly. |
||||
|
|
|
I love the "everything is an expression"-idea, which can be found in haXe (I suppose the feature actually comes from functional languages).
Basically, the function body is just one expression. A block is evaluated to the last evaluated expression of the block. A loop is evaluated to the last pass (or |
|||||
|
|
I like Common Lisp macros (not sure they fit into the definition of syntax in this question). Macros shorten your code and lengthen your life. |
||||
|
|
|
In VB.NET I love the
versus:
|
|||||||||||||||||||||
|
Perl 5Perl 5 absolutely abounds in fantastic syntactic sugar I miss elsewhere, starting with sigils to quickly group variables by type ($scalar, @array, %hash). My absolute favourite is
or
with the elegance of:
|
|||||
|
SQLThrowing a SELECT statement inside a JOIN. e.g.
Update Removed the C# one - |
|||||
|
SmalltalkSmalltalk's blocks/anonymous functions/closures have the lightest-weight syntax I have ever seen:
|
|||||
|
PythonDict ComprehensionsSomeone mentioned list comprehensions, so I just wanted to add dictionary comprehensions which came in Python 2.7:
|
||||
|
|
|
I'm a big fan of postfix conditionals in perl and ruby:
Easy to abuse but great for readability when used properly. |
||||
|
|
Ruby BlockPass anonymous function as a parameter
|
||||
|
|
|
Scala The fact that I don't have to use "." between function calls. Makes DSLs a breeze. |
|||||
|
|
Scala: Pattern matching to extract data out of various structures like lists, tuples, and record types.
|
|||||
|
C++C++0x lambda syntax very quickly grew on me despite its odd use of symbols:
Unfortunately, it doesn't work in all compilers. :( |
|||||
|
JavascriptUsing
Now suppose that
Note that I've written this using jQuery, but |
||||
|
|
|
Scala: Everything is an expression, including Examples:
|
||||
|
|
|
F# forward pipe operator ( Makes code very readable than otherwise. Example:
For those who don't know F#, above code is equivalent to following C#:
|
||||
|
|
|
I work mostly with C, which makes me appreciate Yes, you can implement something a lot like it, with limitations, but it just isn't the same. |
||||
|
|
|
Interfaces. I was introduced to them when I first learned Java in 1997, but I think something similar had existed in other languages (Objective-C?) for a while. Not everybody likes them, but they have some big advantages:
|
|||||
|
JavascriptI love the fact that objects are essentially dictionaries so that
is syntactic sugar for
|
||||
|
|
|
A very short way to test a boolean and execute a single statement in Javascript:
Now, if test equals true then the browser will execute alert("hi"); This is because it will only test alert("hi") when test equals true and because Javascript is so loose, you can use this outside an if statement and alert("hi") can be anything This is not very useful for developing as it will make your code harder to read, but if you have to make a script as small as possible it is very useful indeed |
||||
|
|