4 ms·
I find myself wanting languages and frameworks that let me write the magic myself. Magic is about solving big sets of similar problems in really compact/effortl
by cormacrelf 9y ago
I find myself wanting languages and frameworks that let me write the magic myself. Magic is about solving big sets of similar problems in really compact/effortless ways. Rails tries to solve a really huge class of similar problems, i.e. web development, and starts pushing back on what you can achieve without ditching the framework entirely. I don't mean that in the sense that you stop being able to write regular Ruby (which can do anything), I mean that it's smooth sailing until you want to add your own magic in 'userspace'.
Take ASP.NET, for example using method attributes for authorisation:
[Authorize(Policy = "AdminOnly")]
public IActionResult Method(int projectId)
{
return Ok();
}
It's great and magical if you want to lock down a controller or a method to admins only, or people whose standard user object has an Age field > 18 or something like that. But if you want per-project permissions, with a user->permissions<-project relation? Sorry, you can't easily pass invocation parameters to your AuthorizationPolicy classes because C# Attributes don't support that. You could try having a naming scheme for your url fragments and get the project ID from there, but this means you need to have project parameters in every route, which is often redundant and would need to be checked on any object in a POST request. Any way you go, you have to write code inside your method to get the job done. You have to break out of the magic, and let go of the automatic 403 responses to do anything beyond the simplest case.
To actually build this without a clumsy if/else and manually returning 403 with JSON (to describe the error) in every controller method, it was necessary to create an exception and a custom exception handler middleware, and in the handler, produce a non-standard HTTP code and catch it again in a second middleware after the framework would otherwise gobble up 403s and return HTML instead of JSON. Exceptions inside controller methods are a perfectly adequate model for authorisation failures, but because the framework expects you to hook into their middleware for it, they made it very difficult to do it any other way.
Ok, that was a lengthy example. But it illustrates what I mean by wanting to write my own magic. Frameworks shouldn't cripple userspace magic by design.
- taeric 9y agoIf you want to write your own magic in code, you likely want a lisp.
- cormacrelf 9y agoI think you may have diagnosed me correctly. I have been enjoying Clojure. But weirdly enough, the more powerful a type system you have, the more you can do lisp-like magic. Think Rust's traits that can have default implementations, which basically gives you what multiple inheritance is supposed to provide. And Into<T> that essentially sprinkles opt-in dynamic typing through your codebase. There are so many bits of magic you can express with powerful static types, and I don't really mind writing a generic implementation once.
- taeric 9y agoStrong types give me the feeling of expressing some sort of universal truth in a math way. I am actually quite bad at that. Lisp let's me understand how procedures go together to build things. In particular, it helps get to a dual phase of programs. Macros aren't my program running. Rather, they are a program that makes the program I want to run. It just so happens that being able to cycle between those phases is very useful. I can make no claims that one is superior. Edit: s/string/strong/.
- wikibob 9y agoHave you taken a look at what can be done in functional languages (perhaps F#) ? I'm just starting to get into them [0] and it's been really fascinating. Jet.com has an interesting post on why they chose F# for similar reasons [1] [0] I'm working through the How to Design Programs book from Matthias Felleisen which I highly recommend despite its rough edges http://www.ccs.neu.edu/home/matthias/HtDP2e/ http://www.ccs.neu.edu/home/matthias/HtDP2e/ [1] https://tech.jet.com/blog/2015/03-22-on-how-jet-chose/ https://tech.jet.com/blog/2015/03-22-on-how-jet-chose/
- cormacrelf 9y agoYeah, way into FP. I've used Haskell, Clojure, and hopefully some Erlang or Elixir soon, and I pretty much use functional style wherever else I can, including C# and JavaScript. F# seems to be getting more attention, but I mostly just see people struggling to get EF migrations going, and it seems general ecosystem support is still shoddy (compiler bugs on .NET core 1.1? 3rd party VSCode support? official VS plugins crash all the time?). I have to use C# for work, so it's just not stable enough despite F# itself being around for ages :)
- jaegerpicker 9y agoTHat's not my experience with F#, I've used it with EF and .net core 1.1, and VS. I've been extremely happy and had little to none of the issues you listed.
- cormacrelf 9y agoI pulled it out of the drawer again a few days ago. It was lovely, I wrote a monad for asynchronous HTTP results (basically the async monad + an Either monad + some methods to produce some HttpErrors) and encoded a bunch of API functionality (including auth filters) with early returns. The only pain was not having anonymous dynamic objects in C# new { syntax = "syntax" } and using Dapper. I'd definitely like to give it a go for some bigger projects.