12 ms·
Google Common Lisp style guide
- 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.
- dang 6y agoIf curious see also 2012 https://news.ycombinator.com/item?id=4639490 https://news.ycombinator.com/item?id=4639490 Not Google's, from last year: https://news.ycombinator.com/item?id=20505807 https://news.ycombinator.com/item?id=20505807
- airstrike 6y agoBeat me to it! Link to the article in the 2012 submission is broken, so here's a web archive copy: http://web.archive.org/web/20130114221734/https://google-styleguide.googlecode.com/svn/trunk/lispguide.xml http://web.archive.org/web/20130114221734/https://google-sty... Seems like the exact same document which harks back to the ITA Software acquisition per comments at the time of that submission
- butterisgood 6y agoWent looking for advice on concurrency/parallelism and error handling.
- danielam 6y agoFWIW, speaking from memory, if QPX, Google's (previously ITA's) low airfare search engine, is any indication, then the lack of any real mention is not surprising because QPX did not make use of parallelization or concurrency. The way it was written made that move very difficult. From what I recall, there was interest in parallelizing some bits of the computation, but I don't recall it ever really going anywhere. (QRES, ITA's reservation system, which was also written in Common Lisp, may have made use of concurrency or parallelization in some way, but my knowledge of that system is limited.) N.b., QPX did not quite follow all of these recommended practices during my tenure (e.g., ubiquitous SETFing of object slots).
- aidenn0 6y agoThe 2 word guide to concurrency and parallelism: Use fork().
- jimbokun 6y agoIs it weird that I enjoy reading this, even though I haven't programmed in Common Lisp for a long while?
- ch_123 6y agoI assume (perhaps naively) that Google must have a non trivial amount of CL development if they have a style guide for the language... anyone know what they use CL for?
- carry_bit 6y agoMy guess: Google bought ITA Software about 10 years ago, and their search engine was written in Common Lisp.
- danielam 6y agoYes. The authors of this document were all engineers who worked on QPX/QRES.
- shaftway 6y agoIt doesn't take much need to write a style guide. And this one is rather lackluster. By comparison look at the shell style guide: https://google.github.io/styleguide/shellguide.html https://google.github.io/styleguide/shellguide.html
- gpanders 6y agoFrom the linked shell style guide: > Indentation is two spaces. Whatever you do, don’t use tabs. The recent obsession with 2 space indents is boggling to me. I find it much more difficult to read (especially in long blocks of code with lots of indentation switches) and I'm not even visually impaired. They also apparently really don't like tabs, which I find interesting. Personally, I was converted to the tabs camp after learning how much better tabs are for accessibility. I'm surprised that Google of all places doesn't take that more seriously.
- sgeisenh 6y agoObligatory disclaimer: comments are exclusively my own and do not reflect the views of my employer. A lot of these rules exist for the purpose of having a consistent rule. With modern tooling, there is virtually no difference between tabs and spaces. What is important is to have some rule to avoid the constant bikeshedding that would occur if there wasn't one.
- lordgrenville 6y agoSlightly off-topic but is this official, and if so...why is Google hosting stuff on Github Pages? Seems sort of amateurish. Not to mention it belongs to a rival of theirs.
- Jtsummers 6y agoGitHub hasn't always been owned by MS, I believe this was up on GitHub before the acquisition (along with a lot of other Google content after they closed up Google Code). And yes, it's official. Google acquired ITA which was rather famous as a Common Lisp shop. This meant that they had acquired a substantial Common Lisp codebase. If you drop the document from the URL you end up at [0] which includes links to other language style guides. [0] https://google.github.io/styleguide/ https://google.github.io/styleguide/
- slenk 6y agoGoogle doesn't have a product like that...
- cbarrick 6y agoGoogle moved a lot of their open source projects to GitHub after they shut down Google Code. That was before the MS acquisition. You can find a lot of smaller projects at https://github.com/google https://github.com/google, and obviously there are some big ones like https://github.com/tensorflow/tensorflow https://github.com/tensorflow/tensorflow. Notable exceptions are Android and Fuchsia, which have their own hosted git repos at https://{android,fuschia}.googlesource.com https://{android,fuschia}.googlesource.com.
- lordgrenville 6y agoThanks for clarifying. It makes sense to host repos on GitHub - it's so widespread - but what struck me as weird was using the.github.io domain, which I usually associate with amateur bloggers who don't want the hassle of registering a domain.
- jgodbout 6y agoMost Google open sourced code is on Github. Generally the code made by developers isn't "official" it's just code (documents) made by people at Google.
- alaaalawi 6y agoYMMV but i do prefer the following: https://www.cs.umd.edu/~nau/cmsc421/norvig-lisp-style.pdf https://www.cs.umd.edu/~nau/cmsc421/norvig-lisp-style.pdf
- _bxg1 6y ago> Macros bring syntactic abstraction, which is a wonderful thing. It helps make your code clearer, by describing your intent without getting bogged in implementation details (indeed abstracting those details away). It helps make your code more concise and more readable, by eliminating both redundancy and irrelevant details. But it comes at a cost to the reader, which is learning a new syntactic concept for each macro. And so it should not be abused. I really think this just applies to any kind of indirection - classes, functions, even named constants (vs literals).
- aidenn0 6y agoRight; all abstractions are only as good as they don't leak, but the question is "how easy is it to debug when it does leak?" I think you listed classes, functions, and named constants in approximately the order of debugability too. It can be unclear even which macroexpansions are in play from a backtrace, much less which one caused the breakage. (non inlined) functions are right there in the backtrace, and of course, debugging a named constant is as simple as typing its name into the REPL.
- fiddlerwoaroof 6y agoIn emacs, the macrostep expander solves most of the Macro debugging issues: you usually can expand a macro use, and see exactly what code is being generated or “refactor” the macro away by copying the expansion at a certain level of detail and replacing the original form with it.
- aidenn0 6y agoYes, this is a useful tool; I still maintain that debugging macros with this tool is harder than debugging functions with the various other SLIME tools.
- skybrian 6y agoSyntax changes have different consequences for reading comprehension. You can often skip over a function call, making only a reasonable guess at what the function does and relying on invariants for all function calls. A macro can arbitrarily change the lexical environment of anything contained in it with few constraints, so reading other parts of a file without knowing what each macro does is more precarious. And when it comes to navigating large amounts of code, you do need to stop somewhere; you can't do a depth-first read of everything before doing anything.
- dreamcompiler 6y ago> You should favor iteration over recursion I take slight issue with this. It's one of the ways Scheme programming style differs from Common Lisp style in general, and it makes sense. But I find the LOOP macro obtuse and ugly and many algorithms are simply clearer with recursion, so if I can write it tail-recursively and I know the implementation supports tail calls (most do), I often do that instead of using LOOP. The other handy case for recursion is "try foo, and if it doesn't work tweak this argument and try foo one more time." There tail calls don't matter because max iteration count is 2. LOOP is still critically important but sometimes it impedes rather than assists clarity.
- remexre 6y agoHave you tried iterate (wrt the loop sucks problem)
- dreamcompiler 6y agoYes but I've never used iterate enough to get comfortable with it. Maybe I should.
- kazinator 6y agoI made a macro tlet which lets you write pseudo-functions that look like labels syntax. Everything is done by tagbody/go under the hood. http://www.kylheku.com/cgit/lisp-snippets/tree/tail-recursion.lisp http://www.kylheku.com/cgit/lisp-snippets/tree/tail-recursio... With that, you don't even have to bother with ensuring that the calls are in tail position.
- smabie 6y agoYou probably shouldn't be using recursion either. maps, folds, and scans are the bread and butter of FP programming. tail recursion is almost as bad as a normal loop, and shouldn't be necessary most of the time. The most reasonable use of tail recursion is when the combination of the above operations doesn't have good enough time complexity, necessating that you do everything at once. Though, when I programmed CL in the past I used the iterate macro, which I found a lot nicer than the ugly loop one.
- Syzygies 6y ago> Symbol guidelines: You should use lower case Like flipping through for the soft porn in a friend's "romance" novel, I must confess I searched straight for this guideline. It astonishes me that Lisp systems still default to all caps. Of course one can quickly disable this, but why send the old gheezer "GET OFF MY LAWN!" message? That's exactly what people do, get off Lisp's lawn. They don't even get to the part where the parentheses (completely unnecessary for representing a tree in 2020) are a hazing exercise / loyalty test. I love Lisp, but its public relations is the poster child for "How can people who are so smart be so dumb?"
- WalterGR 6y ago> ... the parentheses (completely unnecessary for representing a tree in 2020) are a hazing exercise / loyalty test. I love Lisp Which Lisp do you use since you dislike parentheses so strongly? Dylan? Something else?
- oconnore 6y agoJulia has a full power macro system with “normal” syntax.
- Syzygies 6y agoAs does Nim. There's a thrill to monadic parsing of text in Haskell. That thrill hasn't been extended in any language to algebraic data types. To me, "full" in any full power macro system I've seen means they've adopted every lesson that Lisp learned. I don't see anyone going on...
- andi999 6y agoWhat are these lessons?
- Syzygies 6y agoI use my own preprocessor for Chez Scheme. Comments begin flush left, code is indented. Indentation implies the parentheses that it can. "|" opens a group that self-closes at the next unbalanced ")" or the end of the line. "$" is a placeholder where there's no Lisp symbol, when one needs to imply several opening parentheses at once. Together this avoids Lisp's signature 17 car pileup at the end of every expression. I look for the "$" or equivalent in any proposal out there, to see if the author has written lots of code or is just talking. It's like looking for bone marrow in beef stew, evaluating a cookbook. Marrow is central to the story of Lisp; we got our start being able to wield tools to crack open bones after lions and jackals had left a kill. The added nutrition allowed our brains to increase in size. Soon we mastered fire, then Lisp. I need a Trojan horse, to release this. I helped computerize algebraic geometry. The old guard doesn't change its ways; new people show up with open minds. I want to write a language whose core ability is concise monadic parsing of trees, with macros its core strength rather than a bolt-on. Then I'll "happen" to use it in a definitive successor to Okasaki's book "Purely Functional Data Structures". Oh yeah, and use this alternate syntax. I see no other way to adoption.
- twblalock 6y agoWho here can talk about how widely used Lisp is at Google, and what it is used for?
- rurban 6y agonconc over append, lol. You cannot use append when the first list is long, it's way too slow. The rest all makes sense.