Clean Code Ruby – Variables

Tram Ho

Introduce

This article is based on the technical principles of programming from Robert C. Martin’s book Clean Code , adapted for Ruby. This is not a mandatory writing guide. It is a guide to producing readable, reusable and refactable software in Ruby.

Not all guidelines in this document must be strictly adhered to, and disseminated globally. These are guidelines and nothing else, but they are drawn from the years of collective experience of Clean Code authors.

Our software engineering profession is just over 50 years old and we are still learning a lot. When software architecture is as old as real-life architecture, perhaps then we will have harder rules to follow. For now, let these tutorials serve as the foundation for evaluating the quality of Ruby code that you and your team generate.

One more thing: knowing these will not immediately help you become a better software developer and working with them for many years does not mean that you will not make mistakes. Each piece of code begins as a first draft, like a wet clay shaped into its final form. Finally, we eliminate imperfections when we consider it with our colleagues. Don’t satisfy yourself by the first drafts that need improvement. Instead, keep improving them.

Variables

Use meaningful and pronounced variable names.

Bad:

Good:

Use the same vocabulary for the same variable type

Choose a word for the whole concept and just use it.

Bad:

Good:

Use searchable and constant names

We will read more code than we write. It is important that the code we write is readable and searchable. Without naming the last variables that make sense, we would hurt the reader of the code. So make your variable name searchable.

Also, instead of hard-coded values ​​and using “magic numbers”, create constants.

Bad:

Good:

Use explanatory meaningful variables

Bad:

Good:

Avoid the use of meaningless mapping variables

Obviously better than the default.

Bad:

Good:

Do not add unnecessary content

Do not repeat class / object names in variables.

Bad:

Good:

Use default arguments instead of conditions

The default argument is usually cleaner than the condition. Please note that if you use them, your method will only provide default values ​​for unknown arguments. Other “falsy” values ​​like false and nil will not be replaced with the default values. Bad:

Good:

next

Clean Code Ruby – Methods (comming soon)

References

https://github.com/uohzxela/clean-code-ruby#variables

Share the news now

Source : Viblo