Hacker News new | ask | show | jobs
by edvardas 805 days ago
> He gives some examples of ways that programmers, even after being taught in intro classes not to use magic numbers, still litter their code with constants like 404. But I wish he’d tell Python programmers to stop designing APIs where you write string constants like “r--” and “bs” to denote that your scatterplot should use red dashes and blue squares.

Intriguing. I assume the author would prefer a stronger-typed alternative like explicit parameters and enums. Yet I wonder if having a small DSL-like syntax is actually the better for a scripting language. Most of these plots will be hacked together in a local notebook anyway.

What would be a better alternative to this terse DSL in such case?

4 comments

This line style API was included MATLAB[0] (and perhaps designed elsewhere earlier) in the Olden Days where terseness was both more necessary (due to space and performance constraints) and more accepted. MATLAB development started in the 60s, though this DSL was likely added in the 80s.

Later, Python's matplotlib library started life as an emulator for MATLAB graphics in Python so naturally included the same plotting DSL.

Only later still did matplotlib morph into the defacto general Python plotting library. And then because plotting is so complex and matplotlib exposes so much control, most subsequent plotting libraries were based on matplotlib, opting to add value via high-level abstractions and better defaults, often exposing the underlying matplotlib objects to allow for fine-tweaking. And so the linestyle API leaks into those libraries too.

All of that to say, this DSL was likely invented by a scientist in a lab in 1981 and has survived through inertia and "jumping hosts" a couple times, rather than careful design.

I think the DSL is bad. And the matplotlib developers may agree, because while you can pass a combo like "ro--", you can also pass these parameters separately and more descriptively like

   color='#f00', linestyle='dashed', marker=matplotlib.markers.CARETDOWNBASE
[0] https://www.mathworks.com/help/matlab/creating_plots/specify...
Indeed this comes from MATLAB which is god-awful, and I say this as an enthusiast hardcore MATLAB user.
Is there another library besides matplotlib (and its derivatives) that use this "linespec" format? And doesn't matplotlib's API derive from Matlab?

What's worse, using a terse linespec syntax and being stuck in Matlab-world, or using a terse linespec syntax in Python, and at least possibly being exposed to relatively modern and maintainable software practices?

I suspect that the plot formatting syntax is very old. I remember doing stuff like that with SuperMongo (popular amongst astrophysics ca 2002), which predates e.g. MatPlotLib. It may be as old as computer controlled plotters themselves.
Use enums and types to compose an API that shows you what options are supported and how you can combine them.