7 ms·
Why do programming languages use curly braces?
- buster 14y agoI always wonder why so many languages are sooo bad to use on a non-us keyboard.. I'd love if language designers would consider other keyboard layouts more often.. :(
- kd0amg 14y agoFWIW, braces (and brackets and parentheses) aren't all that pleasant to type on a US keyboard either.
- buster 14y agoThis: https://en.wikipedia.org/wiki/File:KB_US-International.svg https://en.wikipedia.org/wiki/File:KB_US-International.svg is much easier then this: https://en.wikipedia.org/wiki/File:German-Keyboard-Layout-T2-Version1-large.png https://en.wikipedia.org/wiki/File:German-Keyboard-Layout-T2... Of course that's probably the reason why the braces were chosen at all :P
- ginko 14y agoThere's dedicated keys for brackets and parentheses in relatively easy-to-reach positions. How could you make that more pleasant?
- dlitvakb 14y agoMost programmers often write their programs in english anyway, to be easy to share or to review by others (even in closed source software), so it makes sense for programming languages to mostly care about English keyboard layouts. Anyways, if the keyboard layout is a blocking problem for you, consider changing it to what it suits you the best, or use multiple layouts for different tasks. Every operating system has a keystroke to swap layouts.
- buster 14y agoIt's not about writing english per se. How often do you need to write {[]}<> in a letter? Those keys are really easy on US layout but horrible to type on a german keyboard layout. Holding alt-gr and press 7 or 8 or 9 or 0 is surely not impossible to type but compared to US layout it's much more worse, imo. Fortunately Python does away with the overabundance of curly braces at least.
- TylerE 14y agoWhy is Alt+9/0 so much worse than Shift+[/] slightly worse I'll grant, but MUCH worse?
- ygra 14y agoIt's AltGr, which is on the right side. And that makes it quite awkward to use. I either break half my hand or need both hands. Or resort to Ctrl+Alt on the left side which isn't optimal either ...
- buster 14y agoWow.. now i'm wondering how quickly (if ever) i can adopt to using ctrl+alt instead of that awkward (and hated) AltGr. You never stop learning...
- TylerE 14y agoCouldn't you remap your keys so normal (left) Alt is AltGr and right Alt is "normal" Alt? Alt key bindings are pretty rare.
- ygra 14y agoThey are? Every time I want to access a menu or a control in a dialog by keyboard I need Alt. Also in Far Alt+typing searches which I use frequently to get around the file system. Sure, one could get used to doing that with the other hand, but at least for me it's not that little muscle memory that has to be retrained.
- rsaarelm 14y agoI realized I probably write more curly braces than accented characters during the day and switched to using an US-International style keyboard layout that puts the diacritic characters under the Alt Gr key.
- ygra 14y agoThe APL [1] designers heard you and created a language impossible to type on almost every keyboard layout [2] ;-) As for me, I've been using US International for a few years by now (because I use too many curly-braces languages and didn't want to give up being able to type ümläüts). But frankly, changing the keyboard layout just for a programming language is a bit evil. In that sense I liked BASIC and Pascal; I don't mind keywords instead of symbols that much either and I tend to be faster typing words anyway. ________ [1]: http://en.wikipedia.org/wiki/APL_(programming_language) http://en.wikipedia.org/wiki/APL_(programming_language) [2]: http://en.wikipedia.org/wiki/File:APL-keybd2.svg http://en.wikipedia.org/wiki/File:APL-keybd2.svg
- itcmcgrath 14y agoThis made me search to see if there was any 256 key keyboards so I could have a key for every byte, but I apparently no one has been that crazy yet.
- ygra 14y agoI like many parts of Unicode ∖ Latin-1. 256 keys might not be enough ;-)
- TheCoelacanth 14y agoYou'll just have to learn to do one of the encodings in your head.
- dexen 14y agoIn Poland, the most common layout is the `Polish (programmers)', virtually the same as US layout, with extra right Alt switching for polish accented characters. The alternative is `Polish (typewriter)' layout, with accented characters replacing most of punctuation, which doesn't seem to have caught on. Apparently entering accented characters with right Alt is comfy enough. Surprisingly all that happened in spite of MS Windows default settings -- IIRC up to and including Windows 98, MS Windows defaulted to the legacy `Polish (typewriter)' layout, and only since Win XP (or perhaps 7) it defaults to the common `Polish (programmers)'. At any rate, have a look at http://www.colemak.com/ http://www.colemak.com/ which is quite programmer-friendly. I am using a variation of it.
- ginko 14y agoI'd rather say that keyboard layout designers should consider programming languages more often
- nzonbi 14y agoThere is also the possibility of ditching entirely the approach of text based programming, for a more visual approach. This will not work for many people, but for those that it does work, it could improve productivity. Text is a good way to represent sequences of information. So it fits programs well, to some extent. But programs contains various kinds of information, that doesn't fit well with a textual representation: Hierarchical data, scope, branching flow, loops, dependencies, etc. These can be enhanced by a visual representational that goes beyond just text. Besides the Visual representation, there is also the UI. Things like Vim for example, can be awesomely productive, in the hands of someone that has mastered it. But I would claim, that there is still space for improvement. With a visual interface, there are lots of things that can be done. Touch interfaces have lots of possibilities. Special inputs devices, would also be a great opportunity. But that would be harder, for obvious reasons. Maybe 3D printing would open opportunities there soon. The key for a visual UI to succeed, is to allow to do more with less effort. Visual programming has not succeed so far. I would argue that it is because it has not been done right yet. I am currently working on these ideas, with one of my weekend's side projects. Since my time is limited, It is still going to take some time.
- kitsune_ 14y agoThere are a lot of visual programming environments that are used in the creative industries, environments such as MAX/MSP, Pure Data, Houdini (CGI / FX) or NI Reaktor... When I look at a complex Reaktor instrument (for instance Razor[1]), the entire paradigm seems to hit a wall at a certain point. The networks driving these instruments are an intricate mess and their visual representation can actually make it harder to grasp them in their entirety. [1] http://www.native-instruments.com/en/products/komplete/synths-samplers/razor/reaktor-5-player-faq/ http://www.native-instruments.com/en/products/komplete/synth...
- kps 14y agoI'm not convinced it can be done right, because alphabets are inherently superior to ideograms. Making a small set of symbols capable of representing language is one of humanity's greatest inventions. It's good to have an editor (like vi, following TECO) that takes advantage of the fact that this set of symbols is literally at the user's fingertips.
- kps 14y agoTreat keyboard layouts as a part of your software tool set. If the one the OS gave you doesn't work well (and they are universally pretty bad), find or build one that does.
- deleted 14y ago[deleted]
- calinet6 14y agoWell... why not? --Edit: No, seriously, why not? There are only a few different opening-closing character pairs on a keyboard: [], (), <>, {}, /\, etc. It's not that different to type any of them. So why is this even a question? It's because we used the others up and that was next on the list, and it looks somewhat logical as a container of things. So again, wholly and completely, the answer is in the question: "why not?"
- jonascopenhagen 14y agoBecause on non-English keyboards curly braces are a pain to type. E.g. in Danish it requires Alt + Shift + 8 (on a Mac), and if you look at the comments above you'll see that the problem is the same for many other languages.
- cgrubb 14y agoFeatures of BCPL (Martin Richards, 1967): * used { } instead of BEGIN and END keywords * single type called WORD * declared variables with LET * used // style comments like C++ Features of B (Ken Thompson, 1969): * used = instead of := for assignment * variables could be declared AUTO or STATIC * semicolons as statement terminators * ++ and -- incrementors/decrementors in prefix and postfix position Features of early C (Dennis Ritchie, 1970) * two types: int and char * struct introduced in 1973 * /* */ style comments from PL/I * && and || operators with short-cut semantics
- pjmlp 14y agoSadly proper arrays and strings were left somehow along the way.
- kps 14y agoBCPL vectors seem to be the same as C arrays, no? dmr wrote at http://cm.bell-labs.com/cm/cs/who/dmr/bcpl.html http://cm.bell-labs.com/cm/cs/who/dmr/bcpl.html that “C was invented in part to provide a plausible way of dealing with characters when one begins with a word-oriented language. [...] BCPL later found a solution better, on its own terms, by providing an operator (%) analogous to its !, such that v%i designates the i-th character in a vector. However, the earliest BCPL applications tended not to process packed characters in vectors, but to spread them out early into one character per word, process, and then pack them again if necessary.”
- pjmlp 14y agoI meant about the design decision to ignore what other system programming languages were already offering on those days. Algol and PL/I already offered what I consider proper arrays and string types. The performance argument about bounds checking is stupid, because any proper compiler offered the possibility to disable bounds checking for code regions. Sadly the decision taken was another one with the security consequences everyone was to endure with software written in C. At least C++ offers a way out for the developers that embrace STL and similar libraries.
- deleted 14y ago[deleted]
- dangoldin 14y agoA meta comment: It's pretty awesome that we're already having lore discussions about our craft 50 years later. I wonder what they'll be discussing in a 100 years about our languages now and why certain decisions were made.
- pjmlp 14y agoWe will discussing how it was possible that C could endure for so many years delaying the use of safer languages for systems programming along the line of Modula-2 and Ada.
- alan_cx 14y agoModula-2 ???????? Is that serious language then? We learned that back at University when I was doing my CS degree. Other students were scathing about it, going on about why we weren't doing C. But it was used as a simple introduction to programming techniques to prepare for C, Java etc. I understood its value back then as an introduction, but I never realise it had a use beyond that. I have to say, I quite enjoyed using it, and I was a little disappointed to move way from it to "proper" languages at the time. So, it is used in anger, as it were, now? If so, what can it be used for? Part of me would love to pick it up again for something vaguely serious.
- pjmlp 14y agoIt used to be a serious language back in the day (up to early 90's). The Lillith operating system was written in it. A fully working operating system with what could be considered a kind of GUI environment on those days. It was used at Zurich's university and a few others around the world. You can have a look at it here: http://www.youtube.com/watch?v=ob0lznzkykc http://www.youtube.com/watch?v=ob0lznzkykc (Lilith Comdex Demo) http://weblog.hansotten.com/?p=490 http://weblog.hansotten.com/?p=490 (Emulator) http://www.modulaware.com/mdlt52.htm http://www.modulaware.com/mdlt52.htm (Some background history) There were a few companies selling commercial Modula-2 compilers like Garden's Point and Excelsior. There was a UNIX system that had it as part of their compiler suite, I cannot remember which one now. It failed to fully replace Pascal, because eventually Mac/Turbo Pascal came along and most Pascal compilers had extensions that made them compatible with Mac/Turbo Pascal. Since those Pascal extensions were similar to what Modula-2 offered and many companies already had an investment in Pascal code, many kept using Pascal instead of moving into Modula-2. When UNIX gained much of the space in the enterprise world, C was the language to use if you didn't want to justify why you were using something else, so that was like the final nail in Modula-2's coffin. It is very hard for a systems programming language to gain market share if it isn't the official language used by the operating system's vendor. The language is quite powerful, when compared with C, with the added benefit of proper modules, strings and arrays. Not the pointer everywhere from C which leads to security exploits by design, so to speak. Its time has passed, now a proper replacement would be something like Oberon, when looking at Wirth's family of languages. You can use it to program embedded systems, http://www.astrobe.com/default.htm http://www.astrobe.com/default.htm.
- k3n 14y agoTokens of a bygone era. I wish more languages were like Python and didn't require so many cruft characters.
- cobrausn 14y agoThe first time I ran into the spaces vs. tab problem I wanted my curly braces back.
- rejschaap 14y agoInvisible cruft! The worst. Also, Python is crufty by convention with its __init__ and __new__.
- k3n 14y agoThat's an issue you run into once, set your editor, and then never have to worry about it again. On the other hand, curly braces will plague you as long as you use C-inspired languages. An editor will only help you to more easily produce the cruft, it won't absolve you of the responsibility.
- papsosouid 14y ago>That's an issue you run into once, set your editor, and then never have to worry about it again. HN needs /. style comment ratings for things like this. You deserve your "+1 funny" for that. >curly braces will plague you as long as you use C-inspired languages Curly braces don't cause (potentially hard to find) errors like mixed invisible indentation does. You can't be "plagued" by them at all, they are harmless. And they even let your code be completely unambiguous so you can use tools to reformat your code as you please/need.
- deleted 14y ago[deleted]
- qu4z-2 14y agoMuch as I like the /. "semantic rating" system, I'm glad HN doesn't have it, so we don't get these sorts of passive-aggressive ratings. Regarding your second point, curly braces do cause potentially hard-to-find errors, specifically when the indentation and the curly braces don't match up. Admittedly it's very easy to get into a set of habits where you don't do that, but the same can be said for the mixed indentation issue.
- jgrahamc 14y agoand why not something more reasonable, like [], which doesn't require the shift key at least on US keyboards The OP is assuming the current US keyboard layout. C was developed on the PDP-11 which would likely have used a DECwriter II for the keyboard/output. On that keyboard [ ] are on the same key (non-shift is [, shift is ]). Thus [ ] were not 'easier' on it. Similarly, { } were on the same key with non-shift being { and shift being }. C's predecessor BCPL used { } although it was developed on a machine without a keyboard and so typing those characters was just as easy as any other character. The developer might have had access to an IBM card punch with a keyboard but the keyboards on those were odd (IBM layout) with no [] or {} and $( was used in place of {.
- limmeau 14y agoOn German (and I think also Dutch) keyboards, {} are on AltGr-7 and 0, which is a pain to type (and the reason why I switch every computer to US keyboard). I wonder if that's why Python uses indentation and not braces for blocks...
- Tripfag 14y agoFlemish (Belgian Dutch) keyboards have them at AltGr9 and AltGr0. Works just fine, if you ask me.
- AlexanderDhoore 14y agoI'm also a Fleming and I HATE our keyboard. For example languages with $ signs for variables are horrible with it. $ needs AltGr and is all the way to the right. Thus requiring you to leave the home row completely. (Not even getting into how moronic the placements of (){}[] are.) Switched to QWERTY, not looking back.
- Tripfag 14y agoAh, nog ne Howest-er :p
- sjmulder 14y ago
- dragonwriter 14y ago> and why not something more reasonable, like [], which doesn't require the shift key at least on US keyboards [] are used for indexing, which is probably used more. So regardless of the historical reason, it makes sense to use something visually distinct for blocks.
- pmelendez 14y ago[Slightly off topic] It's just me or now a lot of very interesting threads in SO are being closed as "not constructive" Should we start to looking for a place to start this kind of discussion without lose the Q&A format? Reddit and HN might work but just a few of 'Ask HN' posts actually make the front page and, in my opinion, answers on Reddit don't have the same quality in comparison with SO.
- manaskarekar 14y agoSimple answer: http://meta.stackoverflow.com/questions/110350/why-are-useful-questions-on-stackoverflow-being-closed-as-non-constructive http://meta.stackoverflow.com/questions/110350/why-are-usefu... Not a fan of this myself. When there is a pool of thousands of smart people who are willing to answer in a certain format, hundreds of thousands who are looking to read those answers in that format, outright banning that format is a little unfortunate in my opinion. If you want to maintain a certain style for SO, then perhaps there's some outlet for these "discussion format" style questions on there that could be marked as such. To disallow smart people from solving problems because they don't adhere to one strict format is a tragedy.
- jiayo 14y agoThey already split off "programmers" from the main StackOverflow site to address this exact issue, and history is repeating itself. Now it seems we need individual sites for programmers-offtopic, programmers-trivia, programmers-history. Make those, and as time goes by, each one will be moderated more and more heavily until they form their own spinoffs.
- rtkwe 14y agoIt's spin offs all the way down.
- lelandbatey 14y agoThis is very much a side comment, but I've noticed that there are a lot of these kinds of questions on StackExchange, but it seems like a lot of them are closed (this one was closed within an hour of it being opened) because they don't fit within the goals of StackExchange. My question is: is there somewhere else where these kinds of questions (not totally on topic but still relevant to what programmers do) can be asked? I'd love to see more of them!
- thoughtpalette 14y agohttp://programmers.stackexchange.com/ http://programmers.stackexchange.com/ "This is a collaboratively edited question and answer site for professional programmers interested in conceptual questions about software development."
- smackfu 14y agoYou would think, but the same question was closed there as Not Constructive: http://programmers.stackexchange.com/questions/188455/why-do-programming-languages-especially-c-use-curly-braces-and-not-square-ones http://programmers.stackexchange.com/questions/188455/why-do... Very common on StackExcahnge. A bunch of high rep users swoop in and close questions that don't meet this weeks version of the rules that were decided in the Meta site that only the high rep users read. It reminds me a lot of Wikipedia.
- Turing_Machine 14y agoIt reminds me a lot of any human organization that develops a "manager" class. There are a lot of people whose goals in life have more to do with controlling what other people do and making sure that the rules are followed, rather than contributing actual work. You can see the process on a small scale with homeowner's associations. (I put "manager" in quotes because there are good managers out there. Oddly, they're often the ones who are willing to break the rules when they don't make sense).
- sharkweek 14y agoI really like the /r/learnprogramming subreddit -- You can ask anything relevant to programming and you'll get earnest answers with minimal trolling (the mods do a great job there).
- DodgyEggplant 14y agoThe "what question is interesting enough to reach HN front page so we can close it" watch is alert and active, thank you
- Thrall 14y agoI sometimes wonder why languages use sequences like "->". This is clearly meant to represent an arrow, so why has noone ever taken the time to make sure an arrow character is defined, make sure it's possible to type it in most keyboard maps and use it in programming languages. It can't be because physical keyboards wouldn't have the key, because my keyboard doesn't display a # symbol, but that is widely used. I can only guess that the character sets and "what characters are easy to type into a computer" became standardised early on, and no individual language has been able to push such a change (Early in development a language has few users, thus no influence; Later in development, the language has been using "->" itself for a while, so it's less easy to change). Also, proper languages like lisp use parentheses (), not curly braces. :p
- AlexanderDhoore 14y agoNot entirely the same thing, but there are some plugins for Vim, Emacs,... that show different figures/characters in place of things like "=>" or "lambda". But don't actually change your source code. For example: https://github.com/ehamberg/vim-cute-python https://github.com/ehamberg/vim-cute-python
- spacemanaki 14y agoSome languages do support Unicode for things like arrows and other mathematical symbols: http://wiki.portal.chalmers.se/agda/pmwiki.php?n=ReferenceManual.SourceFiles http://wiki.portal.chalmers.se/agda/pmwiki.php?n=ReferenceMa... Languages which support this (including Agda, I believe) usually support some fallback to ASCII, allowing you to type "->" instead of a Unicode arrow, because it's easier to type on the vast majority of keyboards. Of course, APL also famously uses all kinds of symbolic nonsense.
- kps 14y agoSeveral early programming systems did use custom character sets (custom type bars) as well as typewriter-oriented notations like overstrikes, half-line motions for superscripts (powers) and subscripts (indexes), and colour (red half of the ribbon for comments)¹. Except for APL, which had IBM behind it, these tended not to escape their original research environments. Relatively large-scale production required I/O devices to share a common character set. These tended to be based on pre-computer character sets (teletypes, punched cards) and were limited in size both by the I/O devices and by storage requirements (don't forget how small and expensive memories were). A lot of work went into choosing character sets.²³⁴ 1963 ASCII had an up arrow ‘↑’ dropped in 1967 in favour of ^ (because furriners wanted accents), and a left arrow ‘←’ dropped in favour of ‘_’.⁵⁶ ¹Jean Sammet, Programming Languages: History and Fundamentals 978-0137299881 ²Charles E. MacKenzie, Coded Character Sets: History and Development 978-0201144604 ³http://www.trailing-edge.com/~bobbemer/SURVEY.HTM http://www.trailing-edge.com/~bobbemer/SURVEY.HTM ⁴R W Bemer, Design of an improved transmission/data processing code http://dx.doi.org/10.1145/366532.366538 http://dx.doi.org/10.1145/366532.366538 ⁵American Standard Code for Information Interchange http://www.wps.com/projects/codes/X3.4-1963/index.html http://www.wps.com/projects/codes/X3.4-1963/index.html ⁶Revised U.S.A. Standard Code for Information Interchange http://www.wps.com/J/codes/Revised-ASCII/index.html http://www.wps.com/J/codes/Revised-ASCII/index.html
- abecedarius 14y agoSmalltalk uses [] for blocks, and somehow it just looks jagged and unpleasant to read, to me: [[[]]] doesn't nest nicely like ((())) or {{{}}}. I don't propose this as an explanation of anything; but I'm glad it isn't [].
- restlessmedia 14y agoI wonder what percentage of questions with the highest hit rate on SO and SE have "closed as not constructive by x" on them. My 3 groats here says lots.