Fork me on GitHub
#off-topic
<
2020-02-11
>
hindol06:02:51

TIL, one of the stated goals of Clojure is to make programs substantially smaller. In Rich Hickey's words, "So one of the things Clojure is oriented at is making it easier to understand whether or not your program is going to do what it is supposed to do, mostly by making it substantially smaller, and also by making it more functional." From this talk: https://github.com/matthiasn/talk-transcripts/blob/master/Hickey_Rich/ClojureMadeSimple.md

andy.fingerhut07:02:03

Clojure programs can certainly be more concise for a lot of tasks than programs performing the same function in a lot of other languages. I don't know if anyone has tried doing much in the way of numerical comparison against other similarly high-level languages or not.

👍 4
Mattias07:02:58

How do you experienced clojurers view the number of functions used? As I keep up my (low intensive, 30 mins here and there) effort to get into Clojure I’m drawn to doing more and smaller functions. Soon most of my code will be function definitions... which just might obscure the important parts after some point. :thinking_face:

hindol08:02:27

I also like to create many small functions because it makes sense to me. For one off functions however, I define then locally with a let (and letfn) instead of polluting the namespace. I tend to avoid private functions (`defn-` or defn ^:private).

Mattias10:02:55

Hah! Didn’t know about letfn. Thanks! 😄

👍 4
Ivan Koz14:02:49

you also can do simple (let [fn-name (fn fn-name* [] body...)] *(optional for recursive calls)

andy.fingerhut15:02:33

Many small functions can make things easier to test and understand, if divided up well.

andy.fingerhut15:02:18

Take a glance through the clojure.core source code some time, just to see how small a lot of things are. Certainly some are large, but not many, and some of them are only large because it helped to find a way to make them perform better by doing so.

👍 4
emccue05:02:26

I like to do some ascii-magic to separate things

emccue05:02:11

;; ================
;; Section 1
;; ================
(defn a []
 "")
;; -------------
(defn b []
  )

emccue05:02:24

(imagine that the dashes or equal signs went to character 80)

emccue05:02:37

and i roughly try to make it so my namespaces read well

emccue05:02:57

just having the visual separation and grouping helps with the number of functions

futuro14:02:04

One heuristic I use @mattias504 is to ask whether I have to spend more time understanding what the function does than I save by using the function.

jjttjj19:02:22

If I use clojure.edn/read-string to read a string from an untrusted client, and walk that data structure to ensure all function invocations are in a whitelist, should that be fairly safe? or am I missing something

p-himik19:02:29

It depends on what you end up doing with that data. But calling clojure.edn/read-string on an untrusted input should be safe.

👍 4
andy.fingerhut20:02:41

For example, if I were a malicious person trying to get around these restrictions, note that many Clojure functions like map , mapv , filter , etc. take a function as its first argument. If I can write code that takes a function as an argument, then passes it on to one of those, then they can be executed.

andy.fingerhut20:02:01

eval should definitely not be on your whitelist, but it isn't the only thing that shouldn't be there.

andy.fingerhut20:02:51

Clojail is one project that tries to do some things like this, but I doubt there is any kind of proof of security anyone has done on it: https://github.com/Raynes/clojail

p-himik20:02:42

Clojail also doesn't work with Clojure > 1.6. Or maybe even older.

jjttjj20:02:27

oh yeah I'll have to think about how to use the higher order functions safely

andy.fingerhut20:02:55

Good to know. Someone may have created updated versions and published them, or similar library that is more recent. I haven't looked. It still may be worth examining if there is nothing else more recent, to learn from.

jjttjj20:02:56

I was looking at this a little bit: https://github.com/fif-lang/fifql which uses a stack based language built on clojure, for remote (whitelisted) code execution, wondering what are the barriers to doing a more lispy/clojurelike version

andy.fingerhut20:02:29

FYI, Clojure wasn't designed with 'jail' type execution safety in mind. It was designed to give you power, and let you decide what you want to do with it. This can very likely make a provably safe thing like Clojail either impossible, or at least challenging to get right. If you want "higher hurdles for malicious people" or "door locks, to keep the honest people out", it is likely a good enough approach. Anything else, and I think you are looking at full-time security researcher with expertise in breaking system security type work.

p-himik20:02:51

It may be worth noting that maybe you don't need to support functions at all if you implement a rich enough DSL on top of that EDN.

jjttjj20:02:29

Thanks for the tips again 🙂 useful to know this is probably not he right path

andy.fingerhut20:02:10

FYI, I think a path like this is exactly what some web sites that let you enter Clojure code, e.g. http://www.4clojure.com. uses. However, they probably also run it in a VM, and give it no access to anyone's private data, so even if the jailing fails, the worst that is likely to happen is it hogs the VM for a while until someone kills that.

jjttjj15:02:47

I was delayed in checking out Sci, but it seems to be exactly what i want actually, thanks!