7 ms·
Swift Style Guide by GitHub
- rsendv 11y agoLost me at "Tabs, not spaces." :p
- tjallingt 11y agoTabs beat spaces in every single way; - only press a single key instead of tapping 2 or 4 times. - alter their width within your IDE to suit your preferences. - they are "designed" to intent a piece of text with unlike spaces which are "designed" to break words. But use whatever you like as long as it's consistent :)
- scrollaway 11y agoWell said. I see tabs as an accessibility feature. https://leclan.ch/tabs https://leclan.ch/tabs But like you said, consistency matters a lot more.
- oneeyedpigeon 11y agoI prefer tabs BUT ... don't you have to throw out any line-length requirements if you use them?
- krisdol 11y agoNah just hard wrap at the line length. Imagine everyone setting their preferred tab width and hard wrapping. That'll be fun for looking at VCS diffs...
- rsendv 11y agoYeah, consistency trumps preference.
- luso_brazilian 11y ago> But use whatever you like as long as it's consistent I would add "as long it's consistent with the codebase and the practices of the project" Mixing tabs and spaces in a project is very unpleasant and changing one for the other wreaks havoc in the blame functionality
- jonhohle 11y agogit blame -w …
- napperjabber 11y agoSpaces are more consistent than tabs. https://www.jwz.org/doc/tabs-vs-spaces.html https://www.jwz.org/doc/tabs-vs-spaces.html
- tychuz 11y agoSomeone doesn't like HackerNews.
- napperjabber 11y agoHah, news to me.
- Zikes 11y agoWe really shouldn't get into this, but I can't help myself. - Every single text editor that's come after notepad.exe has had automatic tab/space conversion. Pressing tab inserts N spaces, pressing backspace un-indents N spaces, etc. - In some languages, this is not a feature. 2-character tabs, using two tabs to indent for alignment: var myVar = 1, otherVar = 2, anotherVar = 3; Opened later in an editor using 4-character tabs: var myVar = 1; otherVar = 2, anotherVar = 3; - {}[]<> and more aren't used for anything close to their original literary intent.
- oneeyedpigeon 11y agoTabs should never be used for lining things up, for that very reason. In your example, you've stopped 'indenting' and started 'aligning'. And it's only 'working' because "var " % 2 == 0; other language keywords will screw that up.
- Zikes 11y agoSounds to me like you're saying there's no objectively correct answer that works for all languages and situations, which I agree with.
- scrollaway 11y agoThere... is, actually. If you don't actually attempt to align the variable names, then what you're doing is "indenting items in a list", essentially. In general, you should avoid aligning unless the readability gains are significant. Aligning things means that you end up re-aligning when you add or remove longer items. This increases maintenance and pollutes diffs (which also means code reviews and git logs).
- jahewson 11y agoMy editor aligns things for me - no effort at all.
- krisdol 11y ago"- only press a single key instead of tapping 2 or 4 times." I use spaces and I still only hit a single key.
- deleted 11y ago[deleted]
- MrScruff 11y agoTabs beat spaces in every single way; Spaces look the same in whatever application you might be using to view a source file, be it any number of random command line tools, or fully fledged IDEs.
- nielsbot 11y agoAssuming monospaced font.
- overcast 11y agoSo setup your tab to insert four spaces, like every editor on the planet.
- insulanian 11y ago> only press a single key instead of tapping 2 or 4 times. Consider changing your editor.
- jahewson 11y agoAh, tabs are bad, because if you let people choose their own tab widths then it becomes impossible to align anything anymore. It makes it impossible to align anything which is != a tab width. Tabs aren't "designed" for indenting text: they're a hangover from typewriters, where they were literally metal tabs used for creating tables. Tabs don't carry semantics - that's the job of the language's syntax. Tabs as a concept should be confined to history IMHO, especially their implementation in modern word processors, where they just give you ad-hoc semantic-less tables. The tab key on the otherhand is great for indenting - in most sensible editors it will insert a configurable number of spaces. Perfect.
- Stratoscope 11y agoThere is an easy solution to the alignment problem: don't align stuff. I haven't used column alignment in at least 20 years. I don't miss it at all, and I'll never go back to aligning things. Column alignment causes far too many problems. Once you give it up, any advantage of spaces over tabs disappears. Code written without alignment reads the same whatever you use for indentation, and it is even perfectly readable in a proportional font! I've written a few comments on HN about the problems with column alignment and the advantages of an alignment-free style. Here are a couple of previous discussions with specific examples: https://news.ycombinator.com/item?id=10206860 https://news.ycombinator.com/item?id=10206860 https://news.ycombinator.com/item?id=9469713 https://news.ycombinator.com/item?id=9469713
- masukomi 11y agoThis is so very wrong.... > tabs are bad, because if you let people choose their own tab widths then it becomes impossible to align anything anymore. In what world is that true. If you indent your code with tabs, and I indent your code with tabs and I show my tabs as 4 chars wide and you show yours and 8 EVERYTHING IS STILL ALIGNED. It's just greater or lesser levels of indentation. You only have problems when you MIX tabs and spaces. Be consistent in your use and you won't have problems. > It makes it impossible to align anything which is != a tab width. Yes. So? Don't mix tabs and spaces. > Tabs aren't "designed" for indenting text: they're a hangover from typewriters, where they were literally metal tabs used for creating tables. What is a table? It's a tool FOR ALIGNING MULTIPLE COLUMNS OF TEXT... so yes, they ARE designed for indenting text. For a more fully thought out argument on why it's a dick move to use spaces as pretend tabs: http://weblog.masukomi.org/2010/07/26/why-you-should-never-indent-code-with-spaces/ http://weblog.masukomi.org/2010/07/26/why-you-should-never-i...
- aubergene 11y agoI agree, particularly with Elastic Tabstops http://nickgravgaard.com/elastic-tabstops/ http://nickgravgaard.com/elastic-tabstops/
- DrJokepu 11y agoOh god, not that one again. I have no idea which one I'm using and quite frankly, I don't want to know either.
- MPiccinato 11y agoOut of curiosity does anyone know the default for Xcode? Looking at my settings it has indent using 4 spaces and I don't remember ever setting it.
- int_handler 11y agoAlso, the examples on the page all seem to use 4 spaces instead of a tab.
- Mayzie 11y agoIt is correct. Only fools presenting flawed arguments opine for spaces rather than tabs for indentation. That's right, indentation, NOT alignment. Spaces are for alignment, tabs for indentation. Not tabs for indentation and alignment, not spaces for indentation and alignment, but rather tabs for indentation and spaces for alignment.
- tempodox 11y ago> Fewer debates about aesthetics Are you kidding? You just added one. De gustibus non est disputandum.
- Zikes 11y agoDictatorial decree is a valid solution to some debates, especially the neverending ones. I actually think it's one of Golang's best features, having a built-in "go fmt" tool that automatically applies an opinionated style guide, which makes it so every single Go repo I find in the wild has code that's formatted in the manner to which I'm most accustomed.
- melling 11y agoYes, Swift should do the same thing. Format on save. So much time is wasted on style guides.
- coldcode 11y agoIf only Apple cared enough in XCode to provide one.
- themartorana 11y agoYup. Amazing in Go. At first I was all (Python-esque) spaces, not tabs! Not only have I come over to the tabs side, but... IT NO LONGER MATERS! `gofmt`being run on save changes the game, and ensures consistency without beating people about the head with it. I wish more language had this feature.
- vemv 11y ago> Not even leading indentation on blank lines. Ugh. I wonder how is this a real issue? Non-indented blank lines ARE an issue though - they make code editing more cumbersome, requiring pressing the tab key far more often. Also, tabs are for bros.
- cyphar 11y agoIf your editor doesn't do smart tabbing, I'm sorry for you. Not to mention that random trailing whitespace is just annoying.
- to3m 11y agoPart of the problem is that Xcode (you know, the editor pretty much everybody will be using to write Swift) leaves blank lines indented. So to leave a blank line that doesn't have indentation, you press Return, and you get an indented blank line; you then need to press Backspace some appropriate number of times to get the cursor back to column 0; then you press Return. (In Xcode's defence, the cursor is, then, at least indented.) If you don't use Xcode, you might not realise how crap Xcode is.
- jashephe 11y agoWhile this is the default behavior, you can, at least in Xcode 7.2, check "Automatically trim trailing whitespace" and "Including whitespace-only lines" under "Text Editing" in Xcode's preferences.
- memco 11y agoIt might help you to use the OSX keyboard shortcut cmd + shift + [left, delete]. The left variant will move you to the beginning of a line, delete will clear all preceding characters and put the cursor on column 0.
- jankins 11y agoI'm on Xvim so I'd prefer to have non-indented blank lines for navigation with { and }. But I wouldn't suggest this guideline for projects where multiple devs since Xcode defaults to indenting blank lines. IMO you want to set yourself up for success as far as consistency goes, and that means sticking to defaults where possible.
- potatosareok 11y agoWhat does Github write that uses Swift? I thought they didn't have an iOS app and I figured their desktop app was some node-webkit or electron based thing based on it being 27mb download.
- Dru89 11y agoThis actually left me more curious than any of the rules in the style guide.
- joeblau 11y agoSwift is on track to be a general purpose language not only targeted towards iOS, OSX, tvOS, and Watch. There are already web servers written in Swift[1] so using Swift for backend work is increasingly more viable. They may have internal plans to implement some new products in Swift, but who knows. [1] - https://github.com/izqui/Taylor https://github.com/izqui/Taylor
- tombell 11y agoThey may also have internal apps written using Swift which are not public. However it's also nice to have a public style guide.
- mikhailt 11y agoMy feeling is that they want a guide up now from the beginning before they proceed with creating Swift apps. With Swift now open source and is an increasingly popular language to write brand new apps on Apple's platforms which consistent of iOS, OS X, watchOS, and tvOS.
- mayoff 11y agoThe desktop app contains lots of Objective-C and Swift code. You can check it using `otool -o` on the binary to dump the __OBJC segment, or by opening it in Hopper.
- gregdoesit 11y agoThe thing that strikes me is how much shorter a Swift style guide is, compared to an Objective C one. E.g. https://github.com/raywenderlich/objective-c-style-guide https://github.com/raywenderlich/objective-c-style-guide This fact tells a lot about the langage. The language is pretty strict as to what it allows to write and reduces the need of a style guide to specifying best practices.
- glhaynes 11y agoI'm not so sure about "Make liberal use of vertical whitespace to divide code into logical chunks." — it's my tendency to add a lot of vertical whitespace as I write code, but the more I go back and remove it, the more I'm convinced that it wasn't really helpful and that whatever little help it did provide was far outweighed by how much less context could be seen on the screen at one time. I think of it kind of like prose: we organize related sentences into paragraphs and then have little or no extra vertical whitespace between paragraphs. If you ever find yourself thinking you need some extra level of hierarchy division within a paragraph, you're likely really just needing a new paragraph.
- dionidium 11y agoExactly right. The smallest unit of logical division in code is the function. It's not meaningless whitespace. Some programmers seem to like whitespace, but it's a purely aesthetic preference. No more or less.