GitLab Code Style and Conventions
General
The GitLab Code Style and Conventions aim to ensure consistency and readability across the project. These guidelines cover naming conventions, code formatting, and commenting practices.
Naming Conventions
Class and Module Names
Class and module names should be written in UpperCamelCase.
class User
# ...
end
Method and Function Names
Method and function names should be written in snake_case.
def get_user
# ...
end
Variable Names
Variable names should be written in snake_case.
user_name = "John Doe"
Constants
Constants should be written in all_uppercase_with_underscores.
API_KEY = "YOUR_API_KEY"
File Names
File names should be written in snake_case.
user.rb
Code Formatting
Indentation
Use two spaces for indentation.
def get_user
user = User.new
user.name = "John Doe"
return user
end
Line Length
Lines should not exceed 120 characters.
Braces
Braces should be placed on the same line as the statement.
if user.active?
# ...
end
Comments
Purpose of Comments
Comments should explain why the code does what it does, not how it does it.
# This comment explains why we're checking for an active user.
if user.active?
# ...
end
Comment Style
Comments should be concise and easy to understand.
# This is a good comment.
# This is a bad comment.
Code Examples
User Class Example
class User
attr_accessor :name, :email
def initialize(name, email)
@name = name
@email = email
end
def get_full_name
"#{@name} #{@email}"
end
end
user = User.new("John Doe", "[email protected]")
puts user.get_full_name