แบ่งปัน Code Kata สำหรับปูพื้นฐานภาษา C#
เมื่อปีที่ผ่านมาจนถึงตอนนี้ก็ใช้เวลาฝึกฝนและปูพื้นฐานเกี่ยวกับภาษา C# เพื่อเตรียมตัวสำหรับการเข้า project ใหม่ ซึ่งเราก็ได้ทำความเข้าใจเกี่ยวกับ intent, feature ของภาษาที่น่าสนใจ วิธีการเรียนรู้ภาษาใหม่ของแต่ละคนก็แตกต่างกันไป โดยส่วนตัวเราเลือกใช้การทำ Code Kata มาดูกัน
Code Kata
Code Kata ก็คือการทำโจทย์ปัญหาหรือแบบฝึกหัดที่จะช่วยทั้งด้านฝึกฝน เคาะสนิม และเรียนรู้ทักษะการเขียน code ผ่านการฝึกฝนซ้ำ ๆ โดยเน้นไปที่การเรียนรู้มากกว่าทำโจทย์ให้เสร็จสมบูรณ์ 100% ซึ่ง Code Kata ก็มีหลากหลายรูปแบบ เช่น
- Problem solving ผ่าน platform อย่าง LeetCode และ HackerRank
- Refactoring โดยมี constraints ต่าง ๆ เช่น focus ที่การทำตาม principles หรือ rules บางอย่าง
- Test-driven development
- Pair programming
Kata ที่เราใช้ในการฝึก C# คือ Refactoring และ Test-driven development สำหรับใครที่สนใจหรือกำลังปูพ้นฐานอยู่เหมือนกันก็ลองไป fork GitHub repository นี้มาดูได้ครับ โดยเนื้อหาที่ครอบคลุมมีดังนี้
- Number
- String and Char
- Array
- Variables and Parameters
- Classes
- Inheritance
- Interfaces
- Object types
- Nested types
- Enums
- Generics
- Delegates
- Anonymous method
- Closure
- Events
- Try statements
- Disposable
- Enumeration
- Nullable types
- Implicit and Explicit Conversions
- Extension method
- Anonymous types
- LINQ (Language-Integrated Query)
- Operators
- Collections
Feature ใหม่ที่(เพิ่ง)เคยเจอ
- ดึง default value ของแต่ละ type (
nullสำหรับobject,0สำหรับ numeric types,falseสำหรับ boolean) ด้วยdefaultkeyword - Pass variable ที่เป็น value type ไปพร้อมกับ reference ด้วย
refหรือoutkeyword - Pass parameter ที่มี length ได้ไม่จำกัดด้วย
paramskeyword - Indexer สำหรับการทำ indexing บน class หรือ struct คล้าย ๆ arrays เพื่อ encapsulate collection ของ objects และ access ผ่าน index แทน
- ถ้าจะให้ method สามารถ override โดย derived class ได้ ต้องระบุด้วย
virtualkeyword ถ้าไม่อยากให้ override ก็ไม่ต้องระบุหรือใช้sealedkeyword - ใช้
askeyword เพื่อทำ safe cast กล่าวคือถ้า cast ไม่ได้ก็จะ returnnullแทนที่จะ throw Exception - ใช้
newmodifier บน method declaration เพื่อซ่อน method (คนละอย่างกับoverride) ที่ inherited มาจาก base class - ใช้
structkeyword ในการกำหนด definition ของ value types - ใช้
delegatemodifier ในการทำ function pointer ส่วนใหญ่ไว้ใช้ทำ event handling กับ callback mechanisms EventHandlertype สำหรับ handle event ในการเขียนแนว event-driven โดยสามารถ bind และ unbind events ด้วย+=และ-=- ใช้
yieldkeyword ในการ iterate collection โดยไม่ต้องสร้าง collection ใหม่ใน memory - ทำ implicit conversion ด้วย
public static implicit operator - เพิ่ม the functionality ของ types ที่มีอยู่แล้วโดยไม่ต้องไปแตะ type definition ด้วย Extension methods (
thismodifier ใน method argument)
ทั้งหมดนี้คือ feature ใหม่ที่เราไม่เคยเห็นในภาษาอื่นมาก่อน แต่อยากย้ำว่า Code Kata ที่เราฝึกมันหยุดที่ปี 2019 นะครับ ถ้าอยากจะเรียนรู้ feature ตั้งแต่นั้นจนถึงใหม่ล่าสุดก็คงต้องหา Code Kata จากแหล่งอื่นมาประกอบด้วยครับ