8 ms·
> You must not use INTERN or UNINTERN at runtime. I.e. you must not read Lisp data at run time, if it contains symbols, because that will call intern. > You s
by kazinator 6y ago
> You must not use INTERN or UNINTERN at runtime.
I.e. you must not read Lisp data at run time, if it contains symbols, because that will call intern.
> You should avoid using a list as anything besides a container of elements of like type.
Good-bye, code-is-data.
I could reduce this guide by a good 30% with "You should avoid using Lisp as anything as Go or Java".
But that could be seen as defining a macro, which you must seldom do.
- Spivak 6y agoI really don't think that this is bad advice in general. Mess with the code all you want at compile time, but don't touch it at runtime is the good kind of boring. CL is an extremely powerful language, it doesn't mean you should be using it all the time in your day-to-day work.
- avmich 6y ago> CL is an extremely powerful language, it doesn't mean you should be using it all the time in your day-to-day work. That's the problem of code guides - they try to avoid problems with some abuses, but to make a good judgement when some rare decision is justified is hard. So the guide misses the mark by making an approximate limitation - often on the safe side. It has benefits to write on boring, safe subsets of languages. Still writing code guidelines is hard.
- kazinator 6y agoThe thing is, you usually shouldn't be calling INTERN even at compile time. A better rule is this: Don't use code to calculate character strings, which are then converted to symbols via INTERN. The main exceptions to this rule are structs (which generate slot-reader functions by combining the structure name and slot names). Macrology which calculates names using code, which are then supposed to be explicitly referenced in code, is pretty stinky. E.g.: (define-blob foo ...) Here, you're suposed to Just Know that the above is referenced as blob-foo and not foo, because internally it catenates "BLOB-" onto (symbol-name 'foo), and calls intern on that.
- Spivak 6y agoI feel like there needs to be a refinement that allows for something like (define-blob foo) to produce the symbol FOO but no other symbols.
- dreamcompiler 6y agoAbove is one of the many reasons I prefer defclass to defstruct. Defclass doesn't do this ridiculous nonsense.
- tgbugs 6y agoMany times when working with code that uses structs in Racket I have found myself fruitlessly grepping for the definition of some mysterious and troublesome function before eventually remembering to check whether there is a struct named foo with a member named bar somewhere. Normally this is not a problem so long as the file can make it far enough through the syntax phase that you can resolve the names. Yet somehow when the code is well and truly broken then somewhere, somehow, a struct will be involved without any way to resolve the member name. I say this having written my own symbol creating and binding macros before I realized the pitfalls and now dread having to go back and fix it. It seems like such a cool idea in principle.
- dreamcompiler 6y agoIt's a cool idea for programming and a terrible idea for software engineering.
- rvense 6y agoWhat it says is to not abuse lists, isn't it? I think what they mean is something like, don't use a list as a "object"/tuple that gets picked apart with a lot of cdddaring? Rather, use real structures and other containers as appropriate, keeping in mind the performance characteristics. It specifically says lists are appropriate for macros and functions used by macros at compile-time.
- reikonomusha 6y agoThis is exactly what they mean.
- kazinator 6y agoI think it's perfectly fine to use a nested list object that gets picked apart with destructuring until the point that it becomes a maintenance or performance problem. (That point could arrive later that same day, or it might come never). This approach is one of the things that make Lisp Lisp; if it gives you an allergic reaction, use something else.
- reikonomusha 6y agoThe purpose of code guidelines is to do exactly that: guide developers into a certain practice that’s consistent and broadly agreeable in a team setting. Using lists to fake objects isn’t good modern style, and it’s a reasonable guideline to suggest not doing that. I’m sure Googlers won’t gripe if you temporarily cons up a list to shuttle it around. I’m sure they will gripe if you’re doing SICP 101 style object definitions exclusively with lists. The latter easily gets out of hand in a language renowned for its several robust facilities for object definitions. MAXIMA (née MACSYMA) is known for lists-as-objects not only being depended upon, but also getting so out of hand it’s nearly impossible to refactor now. MAXIMA at least has the excuse of being very old software. I feel you’re interpreting this style guide as some kind of draconian law of writing Lisp code at Google, and making awful conclusions from it (e.g., “well if you’re allergic to objects as lists might as well not use Lisp” or “most of this can be summed up as writing Lisp like Java”).
- divs1210 6y ago> you must not read Lisp data at run time, if it contains symbols, because that will call intern or use a non-interning reader. Clojure does exactly that - instead of clojure.core/read, you use clojure.edn/read to read data without running it as code
- kazinator 6y agoThat doesn't work if you want symbols to be symbols, such that if x occurs in two places in the data, it is the same object according to eq. According to this coding guideline, you cannot develop a .fasl format that is made of Lisp read syntax, or exploit Lisp for sophisticated, structured data formats in general.
- ambulancechaser 6y agocan you explain? ((juxt type identity) (clojure.edn/read-string "x")) [clojure.lang.Symbol x] It seems that the reader returns symbols just fine.
- kazinator 6y agoI don't use Clojure; I have no idea. If two occurrences of "x" are mapped to the same object, that is interning; maybe what that does is use its own package-like namespace, separately allocated for each call. The documentation for EDN says that "nil, booleans, strings, characters, and symbols are equal to values of the same type with the same edn representation." The only way two symbol values can be equal is if they are actually same symbol, I would hope.
- john-shaffer 6y ago> The only way two symbol values can be equal is if they are actually same symbol, I would hope. Why is this important? Specifically, why do symbols need to be interned? In Clojure, "Two symbols are equal if they have the same namespace and symbol name." In general, "Clojure’s = is true when comparing immutable values that represent the same value, or when comparing mutable objects that are the identical object." [1] [1] https://clojure.org/guides/equality https://clojure.org/guides/equality
- AnimalMuppet 6y ago>> You should avoid using a list as anything besides a container of elements of like type. > Good-bye, code-is-data. Could you regard that as a list of AST nodes?
- kmill 6y agoDid you see the justifications? For example, > Not only does [INTERN] cons, it either creates a permanent symbol that won't be collected or gives access to internal symbols. This creates opportunities for memory leaks, denial of service attacks, unauthorized access to internals, clashes with other symbols. It even has some advice on using wrappers for INTERN if you really need it. The document has provisions for exceptions to the rules. There's discussion about using EVAL despite the fact the rule says you must not use it. Also, "should avoid" means that you need a good reason, addressed in a comment and code review. Many examples you're probably thinking about are easily containers of elements of like type anyway (allowing mild cases of sum types and such). Though things tend to be more robust with intentionally created data types, I find.
- deleted 6y ago[deleted]
- oalae5niMiel7qu 6y agoAll that can be solved by carefully choosing which package you INTERN the symbols into. If you want to avoid having the symbols stick around forever, you can create a temporary package with MAKE-PACKAGE and then use DELETE-PACKAGE when you don't need it anymore.
- kmill 6y agoYou're right that the rule as stated is incorrect (and I internally corrected it to "it can either create a permanent symbol that won't be collected or give access to internal symbols", so for a moment I didn't know what you were responding to). I think the spirit of the rule is still good, which is raising these sorts of questions at design time and code review: Do you need INTERN? If so, are you INTERNing into the right package? If you are using a temporary package, are you remembering to DELETE-PACKAGE? Do you really need INTERN in an Internet-exposed interface? Does your use of INTERN prevent exposure of sensitive symbol plists to attackers? Does your code rely on private symbols to keep keyword arguments from being used in the public interface, and will INTERN accidentally circumvent it? Is the lifecycle of the temporary package sufficient to prevent DoS? etc.
- dguaraglia 6y agoTo be fair, all of Google's language guidelines revolve around making code easy to maintain, which means easy to read. Most of the other language guides also include such arbitrary restrictions that seem to go against the spirit of the language.
- oalae5niMiel7qu 6y agoGoogle open-source code is repetitive and hard to read.
- logicchains 6y ago>I could reduce this guide by a good 30% with "You should avoid using Lisp as anything as Go or Java". You should see their C++ style guide, it basically bans most of modern C++. Unsurprisingly enough, Google C++ libraries (like Tensorflow, or GoogleTest) are some of the ugliest open source C++ libraries out there.