This project introduces "SHORTY," a C++ utility that aims to make lambdas more concise. It achieves this by providing a macro-based system that replaces standard lambda syntax with a shorter, more symbolic representation. Essentially, SHORTY allows developers to define and use lambdas with fewer characters, potentially improving code readability in some cases by reducing boilerplate. However, this comes at the cost of relying on macros and introducing a new syntax that deviates from standard C++. The project documentation argues that the benefits in brevity outweigh the costs for certain use cases.
The GitHub project "shorty" introduces a method for defining significantly more concise lambda expressions in C++. Traditional C++ lambdas, while powerful, can become verbose, especially for simple operations. This project aims to alleviate this verbosity by leveraging macros to create a highly abbreviated syntax for defining these anonymous functions.
Instead of the standard [](){}
structure for a lambda, "shorty" allows developers to use abbreviated forms like S{}
, S{_<0>}
or S{_<0>+_<1>}
. These compact expressions leverage placeholder syntax, represented by underscores followed by the argument's index, to denote the lambda's parameters. For instance, _<0>
refers to the first argument, _<1>
the second, and so forth. The core idea is to replace the explicit listing of parameters within the capture list []
and parameter list ()
with this placeholder mechanism directly within the lambda's body {}
.
This approach significantly reduces the character count required to express simple lambda functions. For example, a lambda that adds two numbers could be expressed with extreme brevity using "shorty," compared to the more verbose conventional C++ lambda syntax. This can lead to more compact and potentially more readable code, particularly when dealing with a large number of small, self-contained lambda expressions. The project provides the macro definitions necessary to enable this abbreviated syntax. However, it's important to acknowledge that such heavy reliance on macros can potentially impact code readability and debuggability if overused or misused. The core benefit offered is terseness, trading conciseness for the explicitness of standard C++ lambda declarations.
Summary of Comments ( 14 )
https://news.ycombinator.com/item?id=43629380
HN users largely discussed the potential downsides of Shorty, a C++ library for terser lambdas. Concerns included readability and maintainability suffering due to excessive brevity, especially for those unfamiliar with the library. Some argued against introducing more cryptic syntax to C++, preferring explicitness over extreme conciseness. Others questioned the practical benefits, suggesting existing lambda syntax is sufficient and the library's complexity outweighs its advantages. A few commenters expressed mild interest, acknowledging the potential for niche use cases but emphasizing the importance of careful consideration before widespread adoption. Several also debated the library's naming conventions and overall design choices.
The Hacker News post discussing the "shorty" C++ header for terser lambdas generated a moderate amount of discussion, mostly focused on the potential downsides and alternatives to the approach.
Several commenters expressed concern over the readability and maintainability of code using
shorty
. One commenter argued that while brevity can be good, excessive terseness can harm readability, especially for those unfamiliar with theshorty
syntax. They suggested that the potential gains in character count are outweighed by the increased cognitive load required to understand the code. Another user echoed this sentiment, pointing out that C++ is already a complex language, and adding more cryptic syntax likeshorty
further exacerbates the issue. They questioned the real-world benefit, suggesting that saving a few keystrokes is not worth the potential confusion.The discussion also touched upon the potential for namespace pollution and name clashes. One commenter pointed out the risk of unintended consequences when using generic short names like those provided by
shorty
, especially in larger projects. They suggested that more descriptive lambda names, even if longer, are generally preferable for clarity.Alternatives to
shorty
were also proposed. One user mentioned using an editor snippet or macro to achieve similar brevity without introducing new syntax. Another suggested leveraging existing C++ features likeauto
and structured bindings to simplify code without sacrificing readability. A commenter highlighted the benefits of refactoring complex logic into separate functions, thereby reducing the need for lengthy lambdas in the first place. They argued this approach often leads to more organized and understandable code.A few comments briefly acknowledged the potential usefulness of
shorty
in specific, limited contexts, such as competitive programming or code golfing, where character count is paramount. However, the general consensus seemed to be that for most practical applications, the potential drawbacks ofshorty
outweigh its benefits. There was a clear preference for maintaining code clarity and readability over achieving extreme terseness.