clean code – the meaning of names and functions

Tram Ho

I. Introduction

  • If not counting 4 years of college, now I have coded nearly 2 years. In 2 years, I can say I have learned many languages ​​(ruby, php, nodejs, reactjs, …)
  • At some point I feel like I’m just switching from one language to another but improving my skills as a programmer does not.
  • At that time, I wanted to become a better programmer, no longer thinking like a university student. So I went to find documents to read, but also miraculously, just thinking about the previous day, the next day the company taught a course on “Clean Code: A Handbook of Agile Software Craftsmanship”. So batting butt to enroll =))
  • What I share here is just what I remember and referenced so you want to dig later, just read this book.
  • Well, and this does not mean that it will be completely correct, you should not apply it all the time, be flexible because depending on different contexts, you will apply different ways to create the best value.
  • “There is no right or wrong, it is just right or not right.” This is the sentence I summarize when I have the most sense and best interest =)).

II. Meaningful names

  1. What is meaningful first?
    • I don’t know about you, but when I was a student, I had a very typical writing style, where did you comment that =)), it was like this:
    • Well, sometimes there are no comments. V, so when I read the code, I did not understand what I was writing at all.
    • So meaningful is defined as helping make variables, functions or classes more meaningful without necessarily commenting
    • For example:

  2. Of course used must be reasonable
    • How long does it take you to distinguish these 2 variables?

    • So let’s put the difference on top or bottom to make it easy to distinguish

  3. Class names
    • The name of the class should be a noun or noun phrase such as: Customer, WikiPage, Account, …
    • A class name should not contain verbs inside
  4. Method names
    • Methods should have a verb name or verb phrase like postPayment, deletePage or save.
    • When constructors are overloaded, use the self-created static functions and describe the arguments to be passed

  5. Choose 1 word for each concept
    • This is quite important. When you start having a really big code, you and the team are inconsistent with the concept.
    • In the end, the redundant code in each class is different
    • Think about it, when you are calling an api that has your object but you get only a few properties. And so you have to find out why it is so if you are not attached to this concept
    • So only the party chooses one word for each concept

III. Functions

  • Word of mouth: “The first rule of functions is that they should be small. The second rule of functions is that they should be smaller than that ”
  • The bottom function is really short
  • Apply the upper mouth sentence
  1. Do each thing (do one thing)
    • The implication is that for each if, else block should be used
    • That is, the statements should be combined into a function and then called in if, so it can describe what the function does but is also easy to see in if, else
    • The function should not be too big to hold nested structures
  2. Work from top to bottom (The stepdown rule)
    • Is from the boys to the big boys, with increasing levels
    • To include the setups and teardowns, we include setups, then we include the test page content, and then we include the teardowns.
    • To include the setups, we include the suite setup if this is a suite, then we include the regular setup.
    • To include the suite setup, we search the parent hierarchy for the “SuiteSetUp” page and add an include statement with the path of that page.
    • To search the parent. . .
    • The paragraph I translated is lost or should be left alone =))
  3. Use switches instead of if else in some cases
    • For example, we have the problem: There are 2 kids who are playing rock paper scissors, A boy wants to analyze when B is out of what A should be.
    • If we use if else we will have a code like this
    • It looks nice, too: v but it’s not repeating a lot of code, this is called resource overhead
    • So switch to using switch

IV. Conclusion

This is just one of the great examples in the book, I think you should find and read this book to be able to improve your coding ability.

Chia sẻ bài viết ngay

Nguồn bài viết : Viblo