[RUBY] "Let's write versatile code that is easy to use over time" (nth time)

Hello, this is Tahara to work cheerfully, Inc. Best Teacher.

One day

Senior "Can you make dummy data for the campaign? With the rake task" I "Hi!"

I "Gonyo Gonyo ... OK"

  task campaign: :environment do
    Campaign.create(
      title: "Dummy campaign", 
      message: "Conditions Participate in the campaign from January 1st (Friday) to January 31st (Sunday), 2021",
      duration: Time.zone.parse('2021-01-01 00:00:00')..Time.zone.parse('2021-01-31 23:59:59'))
  end

I "I'm done!"

Senior " ...... " Senior " ...... " Senior "You can only use this once ..." Senior "Let's improve the code to be versatile and easy to use over time!" Senior "For example, define it with a variable first, and the range is from the beginning of the month to the end of the month at the time of execution, etc." Senior "And the task name is easy to understand, let's make it dummy_campaign "(I added it by pointing out in the comment) I "Hi!"

  task dummy_campaign: :environment do
    start_day = Time.current.beginning_of_month
    end_day = Time.current.end_of_month
    str_start_day = start_day.strftime("%Y/%m/%d/%a")
    str_end_day = end_day.strftime("%Y/%m/%d/%a")
  
    Campaign.create(
      title: "Dummy campaign", 
      message: "conditions#{str_start_day}〜#{str_end_day}Participate in the campaign by",
      duration: start_day..end_day)
   end

Senior "..., hmm, that's good!" Senior "If you do this, you can use it immediately. You don't have to rewrite it every time, so it will be kind to people who will use it in the future." I "I learned!"

Learn

Let's write the code considering the versatility and convenience when someone other than yourself sees the code over time! I want to study hard as an engineer and grow up!

2020/1/8 Important postscript ver1 (Thanks to @nirasan for pointing out in the comments!)

However, depending on the requirements, the code before modification may not be a problem, so ...

If possible, keep in mind that the value can be static or dynamic, and before implementing it, ask "what do you want to use it for?" And "how do you want to use it?" I think it is better to work in the flow of understanding and implementing according to the requirements.

I would like to proceed with the work in these steps.

This comment cleared up the feeling of haze that I had felt somewhere in the course of my work. I also want to improve the technology for hearing requirements. I'm glad I output it to Qiita!

2020/1/9 Important postscript ver2 (@dyoshikawa pointed out in the comments. Thank you!

"Easy to read by another person" (readability) "Easy to use over time" (universality) You may have confused the two. I thought that the points made by my seniors and the improvement of the author were exclusively the latter.

The problem with the unfixed code (in this context) was not that it wasn't readable, but that it wasn't versatile. In response to your indication ... ・ Change the title to make it easier to understand the intent of the article "Let's write code that is easy for others to see over time." ↓ "Write versatile code that is easy to use over time"

・ Some text changes The we. I will try to learn the difference between the readability and versatility of the code. I'm glad I output it!

Postscript End

Promotion

We provide services that provide opportunities to improve your ability to "write" and "speak" English online. We are looking for an iOS engineer, so please consider it if you like.

Recruitment of iOS engineers who want to work while learning English --Qiita Jobs https://jobs.qiita.com/employers/266/postings/1102

(End)

Recommended Posts

"Let's write versatile code that is easy to use over time" (nth time)
Let's write a code that is easy to maintain (Part 2) Name
Write code that is easy to maintain (Part 1)
Write code that is easy to maintain (Part 4)
Write code that is easy to maintain (Part 3)
Write code that is difficult to test
Function is very easy to use
Think of test code that is easy to understand through Comparator testing
Code that is difficult to debug and parse
How to write code that thinks object-oriented Ruby
Use stream to verify that SimpleDateFormat is thread unsafe
Java 14 new features that could be used to write code
[Java] Code that is hard to notice but terribly slow
Is it easy for the user to use when implementing general-purpose functions? Let's be aware of
How to write good code
Easy to use array.map (&: method)
How to identify the path that is easy to make a mistake