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. |
|||||||||||||||||||||
|
|
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). |
|||||||||
|
|
In VB.NET I love the
versus:
|
|||||||||||||||||||||
|
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:
|
||||
|
|
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:
|
|||||
|
|
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:
|
|||||
|
SQLThrowing a SELECT statement inside a JOIN. e.g.
Update Removed the C# one - |
|||||
|
LINQ in C#What could be cooler than creating both simple and complex logic using natural language?
|
|||||
|
PythonArray slicing is very elegant.
|
|||||
|
PythonPython's comparison syntax is brilliant. I wish all languages had this. In Python, instead of
you can do
|
|||||||||||||
|
C++C++0x lambda syntax very quickly grew on me despite its odd use of symbols:
Unfortunately, it doesn't work in all compilers. :( |
|||||
|
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(); |
|||||||||||||||||||||
|
|
Monads (Example is F#)
|
|||||||||||||
|
|
Scala The fact that I don't have to use "." between function calls. Makes DSLs a breeze. |
|||||
|
|
Anonymous Functions (aka Lambda Expressions) Sometimes you don't have or want to name everything...
|
|||||||||
|
|
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 |
|||||
|
|
The List Comprehensions from Python
|
|||||||||||||
|
JavascriptUsing
Now suppose that
Note that I've written this using jQuery, but |
||||
|
|
SmalltalkSmalltalk's blocks/anonymous functions/closures have the lightest-weight syntax I have ever seen:
|
|||||
|
JavascriptResize 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.
|
|||||
|
Javaobviously the main method
Getters and Setters method Setter
Getter
|
|||||
|
|
As an Oracle developer, the one feature I couldn't live without is how SQL integrates simply and cleanly with PL/SQL. |
||||
|
|
|
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. |
||||
|
|
PHPOn-the-fly 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.
oh oops, I forgot my javascript I want in my footer..
For me it makes the process so much easier. |
||||
|
|
|
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:
(The To call it, you'd use each part of the name as a specifier:
I find it much more readable than |
||||
|
|
|
The ?: and ?. operators in Groovy. They handle any nulls that may happen during a sequence of method calls, so you can do stuff like:
..and not worry if there's still a pizza place open or not :) |
||||
|
|
|
In R probably the vectorization in the conditions combined with index power. This allows you to do something like :
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. |
||||
|
|
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. |
||||
|
|
