Function to Function Pointer refactoring

Last week I had some time to work on some C legacy code again and write some unit tests. I've been playing with a refactoring that I've used in the past and now use it all the time when working in C. The refactoring is called "function to function pointer" and is fairly trivial to do and save in legacy code. It gives a lot of flexibility.

The steps are:

  1. Change function prototype to function pointer in the header (just adding (*name)). Make sure it's extern
  2. Rename the function in the .c file to name_impl, or something non conflicting.
  3. At the end of the .c file. Initialize the function pointer with the _impl one.

That's it. Just takes a few seconds and it allows you to dynamically replace the function in your unit tests. It makes unit testing code in C a lot easier. (there are also some variants, but this is the easiest and most basic version)

Read more

ในวันที่เพื่อนผิดข้อตกลง

ในวันที่เพื่อนผิดข้อตกลง

หนังสือ Teamwork is an individual skill ของ Christopher Avery เป็นคลังสมบัติจริง ๆ มีหลายหลักการที่ผมเก็บสะสมมาล้วนถูกรวบรวมอยู่ในเล่มนี้ หนังสือเล่มนี้เน้นการสร้างทีมในฝัน แทนที่จะตามหาทีมในฝัน ไม่ว่าเพื่อนร่วมทีมจะเป็นยังไง หนังสื

By Chokchai
Prolonged fasting 2025

Prolonged fasting 2025

ผมตื่นมาเช้านี้ รู้สึกเลยว่าหมดแรง ลองพยายามกำมือแน่น ๆ แต่กำไม่ไหวเลย เรี่ยวแรงมันน้อยไปหมด ลุกขึ้นช้า ๆ หลังปรับความดันได้ ก็ลองไปชั่งน้ำหนัก ลงไปอีก 0.35 กิโลจากเมื่อวาน ลองวัดความดันดู ความดันปรกติ 115/75 ค่อย ๆ พาตั

By Chokchai
ชีวิต

ชีวิต

ผมกำลังพยายามทำ prolonged fasting (อดอาหาร 72 ชั่วโมง) ครั้งแรกในชีวิต ณ ตอนนี้เป็นชั่วโมงที่ 34 เมื่อเช้าตอนออกไปวิ่ง 5 ก.ม. ระหว่างวิ่งอยู่ก็นึกขึ้นมาได้ว่าตอนถือศีลอด (ตามหลักศาสนาอิสลาม) ซึ่งอดตั้งแต่พระอาทิตย์ขึ้นจนพระอาทิตย์ตกดิน ผมเคยคิ

By Chokchai
ประโยชน์ของการอดอาหารนาน ๆ

ประโยชน์ของการอดอาหารนาน ๆ

Prolonged fasting หลายคนอาจจะเคยได้ยินว่าการอดอาหารนาน ๆ (prolonged fasting) ช่วยให้เราเด็กลง ซึ่งฟังดูเหลือเชื่อมาก วันก่อนน้องเขยผมเล่าให้ฟังว่าหลังจากอดอาหารนาน ๆ เกิดประโยชน์อะไรขึ้นกับร่างกายบ้าง ทำให้ผมเข้าใจกลไกที่ทำให้เด็กลงมากขึ้น เลยอยากเอามาแบ่งปัน ก่อนหน้านี้ผมเคยเล่

By Chokchai