text
stringlengths
0
444
* https://github.com/CQBinh/ruby-style-guide/blob/master/README-viVN.md[Vietnamese]
NOTE: These translations are not maintained by our editor team, so their quality
and level of completeness may vary. The translated versions of the guide often
lag behind the upstream English version.
== Source Code Layout
[quote, Jerry Coffin (on indentation)]
____
Nearly everybody is convinced that every style but their own is
ugly and unreadable. Leave out the "but their own" and they're
probably right...
____
=== Source Encoding [[utf-8]]
Use `UTF-8` as the source file encoding.
TIP: UTF-8 has been the default source file encoding since Ruby 2.0.
=== Tabs or Spaces? [[tabs-or-spaces]]
Use only spaces for indentation. No hard tabs.
=== Indentation [[spaces-indentation]]
Use two *spaces* per indentation level (aka soft tabs).
[source,ruby]
----
# bad - four spaces
def some_method
do_something
end
# good
def some_method
do_something
end
----
=== Maximum Line Length [[max-line-length]]
Limit lines to 80 characters.
TIP: Most editors and IDEs have configuration options to help you with that.
They would typically highlight lines that exceed the length limit.
.Why Bother with 80 characters in a World of Modern Widescreen Displays?
****
A lot of people these days feel that a maximum line length of 80 characters is
just a remnant of the past and makes little sense today. After all - modern
displays can easily fit 200+ characters on a single line. Still, there are some
important benefits to be gained from sticking to shorter lines of code.
First, and foremost - numerous studies have shown that humans read much faster
vertically and very long lines of text impede the reading process. As noted
earlier, one of the guiding principles of this style guide is to optimize the
code we write for human consumption.
Additionally, limiting the required editor window width makes it possible to
have several files open side-by-side, and works well when using code review
tools that present the two versions in adjacent columns.
The default wrapping in most tools disrupts the visual structure of the code,
making it more difficult to understand. The limits are chosen to avoid wrapping
in editors with the window width set to 80, even if the tool places a marker
glyph in the final column when wrapping lines. Some web based tools may not
offer dynamic line wrapping at all.
Some teams strongly prefer a longer line length. For code maintained exclusively
or primarily by a team that can reach agreement on this issue, it is okay to
increase the line length limit up to 100 characters, or all the way up
to 120 characters. Please, restrain the urge to go beyond 120 characters.
****
=== No Trailing Whitespace [[no-trailing-whitespace]]
Avoid trailing whitespace.
TIP: Most editors and IDEs have configuration options to visualize trailing whitespace and
to remove it automatically on save.
=== Line Endings [[crlf]]
Use Unix-style line endings.footnote:[*BSD/Solaris/Linux/macOS users are covered by default, Windows users have to be extra careful.]
[TIP]
====
If you're using Git you might want to add the following configuration setting to protect your project from Windows line endings creeping in:
[source,bash]
----
$ git config --global core.autocrlf true
----
====
=== Should I Terminate Files with a Newline? [[newline-eof]]