11 ms·
Alternative code styles
- letientai299 8y ago> Poetry... quite eye-pleasing I disagree with this comment. That style makes the code extremely hard to follow, as there's no proper indention to identify scope.
- saagarjha 8y agoMost of these examples are meant to be a joke, I’d assume.
- swalladge 8y agoYes, the whole article is semi tongue-in-cheek. I tried to bring it back to reality by mentioning my appreciation for the creativity of people who designed the styles. :)
- letientai299 8y agoProbably I've taken this so serious since the article mentioned Bourne Shell code, which is a real project, and the Fibonacci indent style is actually nice.
- stinos 8y ago'eye-pleasing' is a subjective thing here and doesn't have much to do with functionality/readability (on the contrary, which is why you posted your comment). I think I understand what the OP means: that code sample, when looked at as an art form and not as code, indeed can be considered eye-pleasing by some because of the symmetry etc. For that aspect whether the code is readable has nothing to do with it.
- xaedes 8y agoDesign rule for non-designers I like: Don't use center alignment (unless you know what you are doing)
- janlaureys 8y agoThat Fibonacci based indentation might not be a bad idea after all. Especially when doing callback-style javascript.
- lifthrasiir 8y agoOr as a cautionary measure against heavily nested code.
- emodendroket 8y ago, but you repeat yourselves.
- Sharlin 8y agoI'd prefer something like 3^n. Would really make you try to avoid needless nesting.
- z3t4 8y agoWhy should I avoid needless nesting ? Is there any other reason besides the holy 80 character limit !? Nesting can for example be used to limit variable scope and make use of closures, or to avoid creating more code paths, or just make the code more readable. You can however often break some code down to named functions and it will be easier to read, with the added benefit of less nesting. But I don't see how avoiding nesting is a good rule by it's own.
- keedon 8y agoI think it would help with clarity - too much nesting in a function can mean it's doing too much and might need splitting into several functions
- jeremy_wiebe 8y agoOne place where nesting makes things difficult is in error handling. If at each potential error point you best one level, you quickly find yourself deeply nested. I find the pattern of exiting early and having the “happy path” running to the end of the scope to make it much easier to comprehend a function (I’m sure there’s a term for this coding style but I can’t find the name of it at the moment).
- aequitas 8y agoThe best coding styles are the ones that allow to be translated back and forth to another style. It doesn't matter which style you prefer. As long as a computer can pick it up and translate it into the project standard style and back to yours again. You can simply setup git filters and live in you own style world.
- swalladge 8y agoUsing git filters is a great idea! I've heard of this for translating between unix and dos line endings in git, but hadn't thought about using it for entire styles. There are heaps of automatic code formatting tools becoming more popular these days too.
- deckard1 8y agoCode is meant to be read first and foremost by humans. As such, code should be written for clarity and presentation. To communicate intent. Automated tools destroy this human/code connection. They reduce your coworkers to simple cogs in a machine, rather than authors and communicators. I'm not a fan of this cold, brave new world.
- aequitas 8y agoHow code is styled doesn't distract from the main purpose to communicate intent. Most often humans are the ones mindlessly layering on the meaningless formatting/style on top of syntax and its then becomes the pivotal point of code reviews, burying the essence of the code. I applaud companies like Google and Hashicorp for forcing formatting as compilation errors. As it lets you get on with the real tasks ahead.
- mmjaa 8y agoPutting every single item in this on my list of "reasons to kill a motherfucker". Seriously though, I find myself oddly attracted to the Python-braces style .. if only I could use it without running afoul of my own list.
- uryga 8y agomake your syntax highlighter show braces and semicolons in a faint color – similar effect, better editability!
- WorldMaker 8y agoOr just use Python or other languages that require fewer braces/semicolons in the first place? We have so many options now, we don't have to deal with the tyranny of overly precisely punctuated languages if we don't want.
- vhb 8y agoSurprised nobody talks about the kdb codding style: https://github.com/KxSystems/kdb/blob/master/c/c/k.h https://github.com/KxSystems/kdb/blob/master/c/c/k.h
- swalladge 8y ago:O that is ... interesting. Sure that's not an entry for the ioccc? ;)
- vhb 8y agoNope :) This is being used everyday in production by most financial institutions. https://code.kx.com/wiki/Cookbook/InterfacingWithC https://code.kx.com/wiki/Cookbook/InterfacingWithC I've been told that the guy who wrote this believe that the brain only has a limited buffer to compute source code. So he is trying to use the shortest code possible everywhere.
- saagarjha 8y agoI think looking at this code has already caused a buffer overrun in my brain ;)
- 0xfeeddeadbeef 8y ago"This is being used everyday in production by most financial institutions" — you can't be serious...
- userbinator 8y agoI'm not surprised, it's an APL family language.
- mruts 8y agoKDB developers make BIG money. It's an APL-like language and used to analyze financial time-series.
- deleted 8y ago[deleted]
- runemadsen 8y agooh god no stop.
- dan-robertson 8y agoThe only style there that is actually used by a person is the Bourne style. There are other styles that people develop for themselves, for example there was some Haskell program posted to a mailing list with a warning about personal style. This defines: (.) :: a -> (a -> b) -> b x.f = f x And then the author can write e.g. x.length Instead of length x I think this is really quite a natural operator (and other languages support it, eg in F# it is pronounced |> and closure has something like it with ->)
- ulucs 8y agoneat, I think D has language level support for this too
- chris_mc 8y agoThat's basically uniform function call syntax. D and Nim use it and a few others.
- aeneasmackenzie 8y agoThis operator is (&) in Haskell these days.
- crehn 8y agoNowadays I couldn't care less about what style is used. As long as the code is consistent and there's an easy way to adhere (clang-format, black, prettier, etc.) I'm happy.
- mruts 8y agoI don't think even you believes that. You're telling me you would be "happy" if every source file was one line?
- xboxnolifes 8y agoJust replace "style" with "half reasonable style".
- gdfasfklshg4 8y agoLint to personally preferred style on checkout. Lint to project style on checkin. Not a problem.
- hyperpape 8y agoDo you actually do this? From what I hear (and this makes sense from what I know about formatting/parsing algorithms) it's a huge pain, and not implemented for many languages.
- emodendroket 8y agoI have my IDE do all the formatting every time I close something and rarely personally intervene.
- hyperpape 8y agoThat's easyish: it's a one-way street. The OP imagined something where everyone checks out code in whatever state it's in, applies their own system, then transforms it back, with no artifacts. That's hard.
- XorNot 8y agoBraceless Rust does look very nice. I wonder if a python style syntax is possible?
- steveklabnik 8y agohttps://github.com/mystor/slag https://github.com/mystor/slag
- vlunkr 8y agoI appreciate the honesty of the readme
- HiroshiSan 8y agoWow! I've never heard of bournegol. That is really cool.
- imglorp 8y agoSadly I have been asked to use it. Horror story time. First job out of school, I joined a place with a lot of EE's writing VHDL tools. So EE's--no offense--at that time viewed software as a solved problem if you did everything by a book (any book) and a spec and a waterfall. So despite working in C, which was already plenty idiomatic with a fine K&R standard, they insisted that while(p-- = p--) was verboten and instead made a bunch of FOOL/LOOP, WHILE/ELIHW, IF/FI and other abhorrences to hide the idioms they didn't like (such as braces) and make it more like algol/pascal. This was all less important than actual CS principals. Then to make matters worse, there was a SPEC, a binder full of PAPER pseudocode mostly in algolish, some hand written. But every piece of code needed to refer back to its spec. Of course those things got out of spec quickly so any new feature was always about triple work. Yes, and it was uphill both ways.
- inherentFloyd 8y agoThis has to be satire. Most of these are terrible and I'm pretty sure the fibonacci indentation originated on /g/.
- artsyxxx 8y agoI'm totally into these suggestions but they couldn't possibly work in a team setting
- sidlls 8y agoI love the Fibonacci indentation. Deeply nested code then looks like the gravity well in a black hole, drawing a direct and clear analogy to where readability and understanding are headed.
- swalladge 8y agoYes, satire and some examples of programmer creativity. I hope none of these styles are used in production code, but they are all definitely styles that someone has used for something. Even if just for fun.
- aasasd 8y agoI followed the link to the OOPS source code from the comments: http://people.idsia.ch/~juergen/oopscode.c http://people.idsia.ch/~juergen/oopscode.c (originally from http://people.idsia.ch/~juergen/oops.html http://people.idsia.ch/~juergen/oops.html) Aaaand… “we're achieving levels of nerdery that shouldn't be possible.”
- jpfed 8y agoKevlin Henney gave a talk ( https://vimeo.com/97329157 https://vimeo.com/97329157 ) that included an interesting idea for style: Imagine every non-whitespace character in your code were replaced with garbage. Indent so that the reader could still understand the structure of your code.