
Swift Style Guide by GitHub - hacker_user
https://github.com/github/swift-style-guide
======
potatosareok
What 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.

~~~
joeblau
Swift 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)

------
rsendv
Lost me at "Tabs, not spaces." :p

~~~
tjallingt
Tabs 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 :)

~~~
jahewson
Ah, 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.

~~~
masukomi
This 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-i...](http://weblog.masukomi.org/2010/07/26/why-you-should-never-indent-
code-with-spaces/)

------
gregdoesit
The 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
I'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
Exactly 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.

------
tempodox
> _Fewer debates about aesthetics_

Are you kidding? You just added one. De gustibus non est disputandum.

~~~
Zikes
Dictatorial 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
Yes, Swift should do the same thing. Format on save. So much time is wasted on
style guides.

~~~
themartorana
Yup. 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
> 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
If your editor doesn't do smart tabbing, I'm sorry for you. Not to mention
that random trailing whitespace is just annoying.

~~~
to3m
Part 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
While 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.

