183 points · by todsacerdoti · 3y ago · 164 comments
The elipsis ... especially rubs me the wrong way because multiple periods either .. or ... denotes range types, but now we have a special case where ... is something completely different. Bah humbug.
They seem nice in clean blog scenarios with trivial examples, but in my experience they are not conducive to writing readable and maintable code.
Thankfully I can just disable their use in my projects (for the most part) with rubocop. All the hip young kids can write their magic code, I'll stick to more straightforward constructs that anyone can figure out.
If you have a wrapper for an API (say, that does some logging or whatever), you can use `…` to forward arguments, maintenance free.
Perhaps the wrapped function started out with no arg, but then added one. You never took/forwarded a `*vargs` array, so now you have to manually update that.
Then perhaps some other function started taking a block. Now you need to add `&block` and forward it.
So `…` is a maintenance-free way to forward arguments to functions that are free to change.
Actually, Perl has had signatures <https://perldoc.perl.org/perlsub#Signatures> as an experimental feature (requiring `use feature 'signatures'`) since 5.20.0 (May 2014), and they’re considered stable (requiring `use v5.36` or later) since 5.36.0 (May 2022):
use v5.36;
sub test($x, $y) {
print "x=$x y=$y\n"
}
test(1, 2)
# prints x=1, y=2It’s practically impossible to remove features from a language. If you keep adding “improvements” to your language you end up with the unholy mess that is modern c++
Expect to be building your own environments if you want to run things that are not in your distro.
LTS is exactly this pain!
I really don't understand why anybody would use that for development. Your SW won't land anyway in the current "stable" distri release, but at the earliest in the "next" version. So you need to develop against that "next" version anyway. The "next" version is something like Debian Testing…
And if you don't plan to release your SW properly into a distri you need to bring your own environment anyway.
I will note that most places I have worked professionally with Python code have compiled their own CPython interpreter and not relied on the distro version.
Secondly it's very well possible to remove features from a language. It just needs to be well enough designed that there is a good migration story (ideally though automatic rewrite by the compiler). Just see what Scala 3 did!
https://docs.scala-lang.org/scala3/reference/dropped-feature...
No. Give the same environment as production.
And you don't run your apps directly on the production boxes either these days.
There is just no reason to endure the constant pain of outdated dev environments.
Of course that doesn't mean that you can't develop against a stable environment, in case you plan to deploy directly onto such thing. That's usually independent of your tooling. You just need to look for the right versions of your deps (which is trivial within some "container"; which runs of course on your up to date dev box).
Back when I was heavily involved in blender development I'd sometimes have to pause my hackery while waiting for fedora to catch up with whatever version of python blender was using because I didn't want to bork my system by upgrading python (again!). Well(...) I also wanted to ensure that it would run off libs from the repos and this was a little higher priority than being on the cutting edge so it was a sacrifice I was willing to make. The only time this caused any significant delay also coincided with one of the open movies and they didn't want to merge what I was working on until after production which meant it didn't matter either way.
I don't ruby but it seems like a nice way to do something like:
def apply(callable, ...):
callable(...)This is the most common way I use it:
class ApplicationService
def call
raise NotImplementedError
end
def self.call(...)
new(...).call
end
end
class MyService < ApplicationService
def call(service_specific_arg: 123)
#...
end
endI can understand what it does but I don't get the intend behind that. (I'm not a Ruby programmer, so maybe I miss something "obvious")
But I prefer to replace ‘raise NotImplementedError’ with ´raise “Not Implemeneted”’ because that exception has different semantics (method unavailability on the current platform)
You can pass positional and keyword args together in a way which is rigidly formalized since Ruby 3.0 because, at one point in history, you could pass a hash, you could pass positional arguments, and you could pass keyword arguments, and nobody was ever able to tell reliably what you were doing:
https://www.ruby-lang.org/en/news/2019/12/12/separation-of-p...
You can pass a block because in Ruby, a block is an object like anything else. And you can pass an anonymous block because in Ruby, the "yield" keyword is an idiom that's commonly used with anonymous blocks. The curly braces and the "do" keyword that are both common idioms in Ruby are nearly identical forms of anonymous block.
The throwaway answer to your question is that you can pass anything that is an object, and in Ruby since anything is an object, you can just pass anything! I haven't read the whole article, but I get the sense that I shouldn't read much into the clickbait headline, since it looks like this is a really thoughtful show or treatment of all the neat handy things that Ruby lets you do with various types of arguments, and not a hit piece! whew relieved
And arguments may be passed either positionally or by name.
Blocks are the weird part, they have special support because they're a bunch of syntactic sugar / magic, which doesn't even have to be part of the function's prototype e.g
def foo
yield if block_given?
end
"yield" will invoke the implicit block, "block_given?" is a metamagic kernel method which checks if the current context was passed a block.I haven't kept up to date with Ruby so I don't know if that style is still en vogue. The equivalent "explicit" version is
def foo &block
block.call if block
end
in which the block — if passed — is reified to a Proc, which you can manipulate normally. If no block is used, then `block` will just be `nil` (as the essay notes, blocks are always optional).Fun fact: Both yield and block.call may never return. The block is free to return from its enclosing method, which effectively pops multiple frames, including foo and possibly more, off the stack.
As an aside, this is one of the key differences between a lambda and a proc. Last I checked, you can pass a lambda as a block, but it is implicitly converted to a proc.
Named args, unnamed (positional) args, and a special block. Because Ruby was basically "let's make Python but without Guido's hatred of metaprogramming", so it copied Python's *args and **kwargs and then added the block so you could make functions that look like control blocks.
To me the problem is that they're handled as 3 vars instead of 1 object that has 3 public properties.*
Essentially, it offers a server-side alternative to "prop drilling," where you repetitively pass the same parameter through a chain of function calls.
I also write my Ruby Seattle style, so I’m one of those weirdos.
Admittedly it's a bit more complicated than Ruby's syntax, which I find to be a bit ironic.
I'm not sure if Clojure has something equivalent to &block... I think you would just pass a function in Clojure since they are first class?
“with-open” is a macro because it needs to do something more complicated (create a bunch of bindings then close each one created, in addition to executing a fn body where those bindings are available).
Passing an fn is preferred because functions are more broadly preferred in Clojure code to macros, for both practical (more composable) and cultural reasons.
Lisp macros are expanded at compile time.
From my limited understanding this would seem like a dynamic, run time, Lisp macro.
But I don't know if the &block actually operates on the syntax. It looks it doesnt.
From my research, it appears that blocks are more like a lambda that has an extra return statement bolted onto it.
It also looks like you can pass them into methods to significantly alter what the method does, and how it returns.
Pretty fascinating pattern. I'm not sure how I would implement something similar in Clojure.
Maybe higher order functions, with lazy sequences combined with predicate checks.
(defun wrap-foo (&rest args)
(apply #'foo args))
TXR Lisp: (defun wrap-foo (. args)
(foo . args))
The (. args) syntax (consing dot not preceded by item) is equivalent to args; if we write it that way, we are not using any syntax that didn't exist in 1958.We need a standard for this. When I'm writing example code, or redacting real code, I usually opt for an ellipses with spaces: `. . .`
PS> function . {$Args}
PS> . . .
.
The first dot is an operator which invokes a command in the current scope, the second dot is our command ".", and the third dot is passed to the command as a string. $ .(){ "$@"; } # define function called "."
$ . . . # nothing
$ . echo hello
hello
$ def `. . .` = println("That's valid syntax!")
@main def entryPoint = `. . .`
[ https://scastie.scala-lang.org/BsvbSctXTmaGWaCbM6JX9Q ]I said almost because HN suppresses the back-ticks…
.(){ :; }
This creates a function called "." that does nothing.Then this is valid code:
. . . def will_implement_later():
...
Which I prefer over "pass" in this case because the incompleteness is a little more intuitive.I think using asterisks for wildcardy-things like variadic args makes more sense too, coming from UNIX land.
(In this case, the article used them as valid syntax and added a comment to clarify that this is indeed valid syntax!)
If that isn't what you were doing, and it still isn't clear enough, add some text to the comment about what the "intentionally omitted part" was that you covered by an ellipsis.
Don't make language features that use three-dot-ellipsis as a keyword. Just like don't make a language where "Hello world" is a special string with special significance, or where variables named foo, bar, baz, quux, or i have special meaning.
I mean, it's nobody's fault but your own if you make a language feature that breaks industry conventions.
I don't mean understanding as in "I get what this demo snippet is doing kinda", but as in "what is exactly happening here, how to edit it, and what might break if I edit it".
Feels like the antithesis to Perl's write-only code. Ruby is read-only code.
I don't think this corresponds to my experience.
I am working as a staff engineer in big production codebases (with all the traits of those: some code is good, some is bad, some is unrecognizable legacy, sometimes we are in a hurry and write awful code, sometimes we have time for refactoring etc.)
And my observations about the logic and perception of the features are drawn from the practice of code reviews, mentoring new people, and discussing ways of solving tasks in this environment. Obviously, I am frequently a driver of new code practices, but I am also trying to be a good person and a good colleague and notice how comfortable people are with various parts of the codebase, various idioms, etc.
One of the main topics of this article series is uncovering the language's logic and intuitions (to stop perceiving it as a "bag of random syntactic features you need to learn or should guess") and using those intuitions for code that is, yes, better for the reader, but the code that can be created in a quickly-changing production environment and rewritten fearlessly.
A smart individual can adapt to any environment and its quirks and thrive. But some of their capacity always goes towards serving the "quirks adaptations". It's kind of like GPT. It's a smart bot, and if you ask it to answer a math puzzle by writing backwards in Romanian haikus - it will. But the effort to make haikus in backwards Romanian make its math puzzle answer that much more likely to be wrong.
I have no shadow of a doubt that people who are used to Ruby know every shade of gray in how to forward arguments through byzantine syntax. Much like PHP programmers have zero issues with every function in stdlib randomly switching needle/haystack parameter order, or remembering which identifiers are case sensitive and which aren't, and the mix of conditions where an identifier needs a $ in front or doesn't need it. Or which one of 10 different stdlib APIs doing kind of the same, but in different state of deprecation and introduction they should use. Or the fact PHP has a type system, but not for the most common data type (array), and there are two modes of validating parameters (strict and non-strict) which can be switched on and off per file and bomb on you unexpectedly when combining both, despite your code is logically correct in isolation. It's trivial stuff to them. They can juggle all this and more. But someone coming from outside would take one look and say "how about no". Because PHP for all its advancements is not a good language.
And Ruby is also not a good language. In fact, it's clearly getting worse over time.
It also has the lowest floor. With the usual makeup of teams of frequently-rotating mostly-junior engineers who don’t have the experience, understanding, and restraint to use Ruby’s more… let’s call them idiosyncratic features, the sheer volume of completely inscrutable crap that can be put out in a given amount of time is truly breathtaking.
But the benefit of RoR is not really Ruby, its Rails. When you embrace the convention-based approach of Rails, you can move very quickly. A lot of the evolution of Rails over the years has been making it possible to have that cake and eat it too; being able to override the conventions when needed while the conventions keep the boilerplate you must write to a minimal level. And ActiveRecord is by far the best ORM I have ever used.
That being said, some of Ruby's design considerations are important to why I still enjoy Rails development: being able to control your system manually in a fine-grained way from the Rails Console by interacting directly with models is really lovely, and the power of that isn't really matched by any other language+framework combination that I'm aware of. This ability to call directly into your model layer really requires a global namespacing arrangement (unlike for example JS) which is not too deeply organized (like in Java), with syntax that makes calls into the model layer not too onerous (ie omitting parenthesis for method calls, because every single `.` call on an object is a method call in ruby).
I wouldn't necessarily pick RoR/Ruby for new projects myself, as Typescript's IDE-time erasable static typing is the number one thing that I wish Ruby had (there are several initiatives to bring over these benefits but they are still nascent), but I have no problem collaborating in teams working on Rails projects as it is indeed a very enjoyable, very mature platform for building APIs, backend systems, and even customer-facing frontend systems.
I find it interesting that Go and Ruby can be placed at two different ends in so many spectrums and yet both still managed to be popular.
Goes to show that trying to build THE ONE TRUE LANGUAGE THAT WILL UNITE ALL DEVS is a fool's errand.
Absolute anything else would be better than it.
Static typing in Python is a complete disaster when it's used on real commerical projects. I've seen the code thank you very much.
The major piece of the puzzle missing is generics support, but other type handling is already there.
I agree, but in that case, using a scripting language to build and maintain applications with significant LOC ... is poor engineering.
Static typing is there so you can get compile-time support for validating assumptions made throughout your source code, which is critical for developing and maintaining LARGE applications by teams of engineers of all skill-sets, over significant periods of time.
And that is very poor software engineering. You should be breaking down those large applications into smaller more maintainable micro-services.
It's bad really bad.
"compile-time support for validating assumptions"
Bad software development again. If you are checking assumptions at compile time then you are slowing down your software development iteration loop. It's the 4 hour compile time problem from C++ revisited.
"over significant periods of time"
Bad, really bad software development again. As business requirements change over the life-time of a software project, you need to retire and replace old code with new code that better meets the changing business needs.
Static typing is useful for performance reasons but that doesn't apply in a scripting language. In pretty much every other context it's terrible.
Another one, “how can we make a powerfully expressive programming language feel malleable, accommodating and pleasant in the hands of experienced devs?” I equally valid.
As someone who writes Ruby I appreciate the language every time I use it. There isn’t another language that has given me such satisfaction - so far. Most accommodate the interpreter / compiler. This one accommodates humans.
Really, what’s needed is separation of concerns, instead of a single “do stuff” function that takes an “everything” argument.
But what’s really wanted is global variables with everything is a single scope.