paint-brush
Code Smell 285 - วิธีแก้ไขฟังก์ชันที่ไม่จำเป็นโดย@mcsee
290 การอ่าน

Code Smell 285 - วิธีแก้ไขฟังก์ชันที่ไม่จำเป็น

โดย Maximiliano Contieri
Maximiliano Contieri HackerNoon profile picture

Maximiliano Contieri

@mcsee

I’m a sr software engineer specialized in Clean Code, Design...

3 นาที read2025/01/11
Read on Terminal Reader
Read this story in a terminal
Print this story
tldt arrow
th-flagTH
อ่านเรื่องนี้เป็นภาษาไทย!
en-flagEN
Read this story in the original language, English!
bn-flagBN
এই গল্পটি বাংলায় পড়ুন!
es-flagES
Lee esta historia en Español!
ja-flagJA
この物語を日本語で読んでください!
sr-flagSR
Прочитајте ову причу на српском!
sw-flagSW
Soma hadithi hii kwa kiswahili!
ka-flagKA
წაიკითხეთ ეს ამბავი ქართულად!
kk-flagKK
Бұл оқиғаны қазақша оқыңыз!
sq-flagSQ
Lexojeni këtë histori në shqip!
hu-flagHU
Olvasd el ezt a történetet magyarul!
ms-flagMS
Baca cerita ini dalam bahasa Melayu!
af-flagAF
Lees hierdie storie in Afrikaans!
TH

นานเกินไป; อ่าน

ฟังก์ชั่นที่มีชื่อไม่ชัดเจนนั้นซ่อนเจตนาและทำให้ผู้อ่านสับสน ควรใช้ชื่อที่บรรยายถึงการกระทำ
featured image - Code Smell 285 - วิธีแก้ไขฟังก์ชันที่ไม่จำเป็น
Maximiliano Contieri HackerNoon profile picture
Maximiliano Contieri

Maximiliano Contieri

@mcsee

I’m a sr software engineer specialized in Clean Code, Design and TDD Book "Clean Code Cookbook" 500+ articles written

0-item

STORY’S CREDIBILITY

Original Reporting

Original Reporting

This story contains new, firsthand information uncovered by the writer.

ต้องจำเป็น!!

สรุป: ฟังก์ชันที่มีชื่อไม่ชัดเจนนั้นซ่อนเจตนาและทำให้ผู้อ่านสับสน ควรใช้ชื่อที่อธิบายและเน้นการกระทำ

ปัญหา

  • วัตถุประสงค์การใช้งานไม่ชัดเจน
  • เพิ่มภาระทางปัญญา
  • บริบทที่ทำให้เข้าใจผิด
  • ลดความสามารถในการอ่านได้
  • ความร่วมมือที่ยากลำบาก
  • ฟังก์ชันที่ซ่อนอยู่

โซลูชั่น

  1. ใช้คำกริยาที่เน้นการกระทำ
  2. ตั้ง ชื่อ ให้สื่อความหมาย
  3. สะท้อนถึงวัตถุประสงค์ของการทำงาน
  4. หลีกเลี่ยงคำศัพท์ทั่วไป
  5. ให้บริบทที่มีความหมาย
  6. แสดงความรับผิดชอบอย่างชัดเจน
  7. การจับคู่การกระทำกับผลลัพธ์

การรีแฟกเตอร์

https://hackernoon.com/improving-the-code-one-line-at-a-time

บริบท

ฟังก์ชันที่มีชื่อด้วยเงื่อนไขทั่วไปจะบังคับให้ผู้อ่านต้องลงลึกในการใช้งานเพื่อทำความเข้าใจพฤติกรรมของฟังก์ชันเหล่านั้น


การทำเช่นนี้เป็นการเสียเวลาและเพิ่มโอกาสเกิดข้อผิดพลาด


การตั้งชื่อกลาย เป็นสิ่งสำคัญยิ่งขึ้นเมื่อทำงานกับฟังก์ชันแบบสแตนด์อโลน โดยที่ชื่อคลาสไม่ได้ให้บริบทเพิ่มเติม


ประเด็นนี้เกี่ยวข้องโดยตรงกับ หลักการบอกอย่าถาม


แทนที่จะเปิดเผยพฤติกรรมที่คลุมเครือซึ่งบังคับให้ผู้เรียกอนุมานฟังก์ชัน ชื่อที่จำเป็นจะสื่อถึงการดำเนินการที่ชัดเจน โดยช่วยแนะนำผู้อ่านโดยไม่จำเป็นต้องตรวจสอบโค้ด


เมื่อคุณตั้งชื่อฟังก์ชันอย่างมีคำอธิบาย คุณสามารถลดการคาดเดาที่ไม่จำเป็นได้และสอดคล้องกับหลักการนี้

โค้ดตัวอย่าง

ผิด

 public String dateFormatting(Date date) { return new SimpleDateFormat("yyyy-MM-dd").format(date); } public void load() { System.out.println("Loading..."); }

ขวา

 public String formatDate(Date date) { return new SimpleDateFormat("yyyy-MM-dd").format(date); } public void loadUserPreferences() { System.out.println("Loading user preferences..."); }

การตรวจจับ

  • [x] คู่มือ

คุณสามารถตรวจจับกลิ่นนี้ได้โดยการตรวจสอบชื่อฟังก์ชันที่ใช้คำคลุมเครือ เช่น do , run , process , load เป็นต้น


ลินเตอร์อัตโนมัติสามารถทำเครื่องหมายรูปแบบเหล่านี้หรือเน้นฟังก์ชันที่มีชื่อทั่วไปมากเกินไป

แท็ก

  • การตั้งชื่อ

ระดับ

  • [x] ผู้เริ่มต้น

เหตุใดการแบ่งแบบ Bijection จึงมีความสำคัญ

ชื่อฟังก์ชันควรสร้าง ความสอดคล้องแบบหนึ่งต่อหนึ่ง ที่ชัดเจนระหว่างชื่อและฟังก์ชันการทำงาน


การทำลาย Bijection นี้บังคับให้ผู้พัฒนาต้องตรวจสอบรายละเอียดของโค้ดเพื่อดูบริบท ทำให้การดีบัก การตรวจสอบ และการขยายผลดำเนินไปช้าลง

เจเนอเรชั่น AI

บางครั้งเครื่องมือ AI สร้างชื่อฟังก์ชันทั่วไปโดยไม่เข้าใจโดเมนของคุณ


เมื่อใช้ AI ให้ระบุว่าชื่อฟังก์ชันจะต้องอธิบายและเน้นการกระทำ

การตรวจจับด้วย AI

โมเดล AI ช่วยตรวจจับชื่อที่คลุมเครือได้โดยการเปรียบเทียบลายเซ็นฟังก์ชันกับแนวทางปฏิบัติที่ดีที่สุดในการตั้งชื่อที่กำหนดไว้ล่วงหน้า


การผสมผสาน AI เข้ากับการตรวจสอบโค้ดด้วยตนเองทำให้ได้ผลลัพธ์ที่ดีที่สุด

ลองดูสิ!

จำไว้ว่า: ผู้ช่วย AI ทำผิดพลาดมากมาย

โดยไม่มีคำแนะนำที่เหมาะสม

พร้อมคำแนะนำเฉพาะ

แชทGPT

แชทGPT

คล็อด

คล็อด

ความสับสน

ความสับสน

ผู้ช่วยนักบิน

ผู้ช่วยนักบิน

ราศีเมถุน

ราศีเมถุน

บทสรุป

ชื่อฟังก์ชันไม่ใช่แค่ป้ายกำกับ แต่เป็นสัญญากับผู้อ่าน


การใช้ชื่อที่ไม่ชัดเจนถือ เป็นการทำลายสัญญานี้และทำให้เกิดความสับสน


ชื่อที่บรรยายและเน้นการกระทำจะทำให้การสื่อสารง่ายขึ้นและทำให้โค้ดของคุณง่ายต่อการดูแลรักษาและขยายเพิ่มเติม

ความสัมพันธ์


article preview
HACKERNOON

How to Find The Stinky Parts of Your Code (Part VII) | HackerNoon

We see several symptoms and situations that make us doubt the quality of our development.


article preview
HACKERNOON

How to Find the Stinky Parts of Your Code [Part XXXI] | HackerNoon

Beginners are afraid to remove code. And many seniors too.


article preview
HACKERNOON

How to Find The Stinky Parts of Your Code (Part VIII) | HackerNoon

Yet more code smells? Plenty of!


article preview
HACKERNOON

How to Find the Stinky Parts of Your Code [Part XXXV] | HackerNoon

Most of these smells are just hints of something that might be wrong. Therefore, they are not required to be fixed per se… (You should look into it, though.)

ดูเพิ่มเติม


article preview
HACKERNOON

What Exactly Is A Name: The Quest [Part I] | HackerNoon

We all agree: a good name is always the most important thing. Let’s find them.


article preview
HACKERNOON

What Exactly Is A Name: Rehab [Part II] | HackerNoon

We all agree: a good name is always the most important thing. Let’s find them.

การปฏิเสธความรับผิดชอบ

Code Smells เป็น ความคิดเห็น ของฉัน

เครดิต

ภาพถ่ายโดย britishlibrary บน Unsplash


ชื่อฟังก์ชันควรเป็นกริยาหรือวลีกริยา และต้องมีความหมาย

โรเบิร์ต ซี. มาร์ติน


article preview
HACKERNOON

400+ Thought-Provoking Software Engineering Quotes | HackerNoon

The most complete curated collection of software and programming quotes


บทความนี้เป็นส่วนหนึ่งของซีรีส์ CodeSmell


article preview
HACKERNOON

How to Find the Stinky Parts of Your Code (Part I) | HackerNoon

The code smells bad. Let’s see how to change the aromas. In this series, we will see several symptoms and situations that make us doubt the quality of our developments. We will present possible solutions. Most of these smells are just hints of something that might be wrong. They are not rigid rules.


L O A D I N G
. . . comments & more!

About Author

Maximiliano Contieri HackerNoon profile picture
Maximiliano Contieri@mcsee
I’m a sr software engineer specialized in Clean Code, Design and TDD Book "Clean Code Cookbook" 500+ articles written

แขวนแท็ก

บทความนี้ถูกนำเสนอใน...

Read on Terminal Reader
Read this story in a terminal
 Terminal
Read this story w/o Javascript
Read this story w/o Javascript
 Lite
X REMOVE AD