Hacker News new | ask | show | jobs
by appplication 993 days ago
> Maybe now I'll be able to actually figure out what data to send libraries without actually reading their source code.

One could hope, but any library abusing kwargs in all their methods is showing they’re willing to go through the absolute minimum to make their code usable, let alone readable and self-documenting.

6 comments

It feels like we're going in cycles. C was somewhat lax with type checking, thus C++ and Java were both made more strict. Looking to escape the tyranny of static typing, the rise of Python, Ruby, or JavaScript instead left us with a desire that Rust, Go, and TypeScript now fulfill. I wonder what's the next step? LLMs are extremely broad in what they accept, but don't exactly fill the same niches.
I always saw C++ and Java addressing other issues, like low level memory management, and higher level abstractions like objects. Not so much type policy. Anyways I see a strong tendency in lots of programmers dismissing anything that is not type static-strict-safe, and others advocating for a more relaxed system.
I see C++ as a scientific DSL definition language. It lets you create an abstraction and define arithmetic operators, iterators and even lets you control dereference and call semantics. The standard helps this scheme by defining return value optimization.
golang isn't in the same league as the other languages mentioned. And Java and C# have come a long way since to be expressive as well as lesser syntax heavy (type inference, records, pattern matching, string templates, etc.)
I'm holding out hope for a Fortran resurgence.

It's a tiny hope. But a hope nonetheless. Fortran is fun.

I'm curious - what do you particularly like about Fortran that isn't otherwise broadly available? Is it a matter of cultural idioms, a unique composition of features, or something else entirely?
Not OP, but I know a good use case: where I work we do lots of math and signal processing. It is done in matlab, which is great, but then we need to run it in some embedded processor. Using the C++ generated by matlab is beyond any hope. Had the code be written in Fortran (which is very possible, and would make the code clearer) it would run very fast. Now we had a team of people translating matlab to C++
Check out D language, it should be suitable for math, signal processing, data science, embedded, etc, and it's intended to be better than Fortran, C and C++ [1].

[1] Is Fortran easier to optimize than C for heavy calculations?

https://news.ycombinator.com/item?id=37606477

I know that Fortran is highly used in the numeric world, especially due to widespread libraries such as LAPACK and BLAS, amongst others; in your opinion, what are the characteristics that make such code much more clear when written in Fortran as opposed to C or C++?

Also, do you prefer a specific version of Fortran, or is the latest one fine?

A couple big things: Fortran natively performs operations on arrays directly like Matlab or Numpy in Python (Matlab was originally a REPL-style front-end to Fortan), and Fortran compilers tend to yield quite fast code (though specific cases will have another language outperform Fortran).

You can read more about the language and its high-level features here: https://fortran-lang.org

That website/community was created in part by the original author of the Python Sympy library, Ondřej Čertík. He is also working on his own Fortran compiler that you can use via webassembly to play around with Fortran; find links if you want to play here: https://lfortran.org

I've only dabbled a little, but I like the general idea, and I appreciate a F/OSS Fortran compiler being developed like this alongside actively seeking to grow the Fortran community & push the language & its libraries forward.

I expect more widespread adoption of Fortran to be quite a ways out, but what Ondřej is doing for Fortran is necessary (not sufficient) for such adoption to be the case.

It is not about the language. Is about the people using it. They are typically not CS people. You cannot expect they program all the languages. They know typically python, matlab, and fortran. Of the 3, the one that would perform better is Fortran.
Can you give an example? I'm always curious what could be more readable than calling a few functions and composing them using properly named variables.
Modern Fortran (2008+) has built-in support for matrices, complex numbers, co-arrays (parallel programming), array slicing, etc. This makes it easy to write performant compiled code if you mostly deal with numerics.

Fortran has also added support for e.g. object-oriented programming, pure functions (no side effects for better optimization), and pointers. So idiomatic modern Fortran code looks very different from the “Fortran 77” code that many people might think of when they hear the name :).

I would really like to see more fortran.
What does a modern Fortran bring that Julia does not? Small binaries?
Language stability, and easy C ABI for embedding into other programs. Julia isn't really a language for embedding afaik.
Sure, not in the traditional sense. Since it does not make small standalone libraries, even though possible, I would not make a native extension with it. But juliacall works fine for Python and R. Quite seamless.
Nim/Julia, IMO.

However....

Nim is unlikely to go mainstream. It's not the next Rust and not for technical reasons.

Julia won't budge Python out of the top slot anytime soon, although it should in many scenarios past simple scripting.

Python is very strict about type checking, it just does it at runtime.

The next step is Idris, where you define starting and desired type and start interactive shell to figure out right way to get there.

It’s like pointer chasing, except the docs having you chase around kwargs are incomplete and there’s gaps. You have to read source code.

The Azure SDK is full of them, making liberal use of kwargs.pop. What a nightmare.

`kwargs.pop`, what a phrase – it's amazing how the right arrangement of syllables can make your brain shudder. (:
Well, who hasn’t used that classic datastructure `dictstack` (and famously the dict.pop operation)?

At first I was kinda giggling. But actually there are such things, if the Mapping is also ordered.

LRU cache, trees, tries… and—oh wait—all CPython dicts are ordered these days!

(Honestly I have only used the modern ordered-nature of `dict` for serialization to versioned or human-editable files. But why not an algorithm with a “`stackdict`” I guess?)

The Azure SDK generally have a lot of silliness in the Python implementation. Functions that take string as inputs, except of course they don't, they take two or three very specific string values and use them to control functionality. What those values are... Well, you should take a look at the ENUM in the C# implementation to figure that part out.
Relieved to learn its not just the Java libs that are piles of hastily cobbled-together crap.
Python has enums though :(
Sure, but Microsoft doesn't always use them.
A big use case for kwargs is not breaking compatibility and not having to copy/paste a ton of parameters when just forwarding them. But that's exactly the case which is difficult to type correctly.
Either copy/paste or rolling them into config objects and passing those down is generally preferable. Copy paste doesn’t always feel great for pass through arguments but it’s perfectly interpretable.

Naked kwargs is so difficult to work with that I hesitate to think of a use case where it wouldn’t be an anti pattern.

> Either copy/paste or rolling them into config objects and passing those down is generally preferable

Preferable for whom? I do not prefer it. I much prefer to avoid the extra work it creates for me vs. the simplicity of kwargs. I use explicit args for the function I made and then add *kwargs on the end and then I don't have to write bespoke config objects or copy and paste a bunch of stuff that might be obsolete by a future update to some library and also pollute my function's signature. I would very much welcome a way to tell callers where kwargs is going without having to do extra work.

Preferable for those who would use your code. If it’s just you then it’s your exclusive preference. If you have users, args/kwargs is going to be more opaque than a more explicit option.

For code with many users, creating a few extra minutes of work for one dev is preferable when the alternative would be every dev who uses that code has to spend that same extra work and then some to grok what exactly is going on with the method signatures. Being explicit also creates traceable code, in that you can search a keyword to find everywhere it’s used or passed rather than tracing methods where it might be used.

I can promise very few users would be thankful for the elegance and minimalism of args/kwargs when they’re source-diving trying to figure out how to get some basic functionality to work.

I think people kind of miss the idea of kwargs in python, the idea is that they are a dict. You can cast a dict to kwargs, in contrast to positional args. I see the typedict being super useful. I really don’t see your argument and would say that this typed dict is more developer friendly than having positional arguments. https://docs.python.org/3/tutorial/controlflow.html#more-on-...
I haven't often been the designer of code others have used, but I have used someone else's wrappers for libraries that we use in many parts of our code. My experience of trying to use their wrappers has been a guessing game of how they decided to arbitrarily rename an argument and finding places where they don't support arguments that I need. I get that kwargs is a bag of mystery and that there is benefit to being explicit, but it comes with tradeoffs. I don't like really like kwargs, but sometimes the use of kwargs better serves the purpose than the alternatives. I've looked for solutions, but haven't found anything that avoids the above issues. If anyone has tools or techniques that eliminate these pain points please share. The typedDict is promising, but I'm not sure how composable they are. Time will tell.
I think naked kwargs can be abused, but there are many legitimate use cases for them. For example, we interface with a message bus that uses JSON for transport. There are several different ways to enqueue a message onto the bus, and it would add a ton of complexity and no real value to define the parameters for each of those Send APIs.

Looking at it another way, the hunk of code in charge of serializing your message does not care one whit about the innards of each message, and making it become aware would add tremendous complexity with no real value.

There's at least a handful of places that you can't escape them, one of the most evident in my mind is when constructing higher order functions or other decorators. Maybe a simple example would be a retry higher order function (or decorator), where you can't know the arguments and their form ahead of time, and want to invoke the wrapped function as is (and only do something like repeating if an exception is triggered). Keyword arguments are helpful for writing certain kinds of generic code, but definitely can be easily abused (much like most of the meta-programming facilities in Python).
This is excellent for adding typing to libraries originally designed without it in mind while keeping backwards compatibility.
If you didn't use **kwargs you'd have to copy and paste every kwarg and its default value from the superclass into the subclass, which is ridiculous IMO.
caugh fastai caugh