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

เสน่ห์ของ ScrumMaster

เสน่ห์ของ ScrumMaster

บางครั้งผมจะได้ยินคำถามเวลาไปงานแลกเปลี่ยนความรู้กับวง ScrumMaster ว่า ทำไงดี ทีมไม่เปิดใจให้ เข้าถึงทีมยาก ตอนผมนั่งในวงนั้น ผมก็ไม่รู้จะตอบยังไง เพราะผมไม่มีปัญหานี้ วันก่อน มีเพื่อนร่วมงานให้ feedback ผมว่า ผมมีพลังพิเศษ ทำให้คนไว้ใจ อยากเข้ามาปรึ

By Chokchai
ความหมายของที่ซ่อนอยู่ใน Just In Time

ความหมายของที่ซ่อนอยู่ใน Just In Time

วันหนึ่ง มีเด็กน้อยคนหนึ่ง กำลังจะขึ้นเครื่องบิน เด็กน้อยร้องไห้กระจองอแง อยู่ตรง security check เพราะว่าปลาทองที่เค้าจับมาได้ 2 ตัวนั้น ไม่สามารถเอาขึ้นเครื่องได้ เพราะมีน้ำอยู่ ยิ่งเด็กร้องนาน พ่อแม่ก็ยิ่งเครียด แล้วก็

By Chokchai
Boring tasks -> Purpose

Boring tasks -> Purpose

วันก่อน มีเพื่อน ScrumMaster โทรมาให้ช่วยคิดท่า retro สำหรับเติมไฟให้กับทีม (ใครอยากรู้บ้างยกมือขึ้น ^-^/) ก็เลยแชร์ที่นึกออกเร็ว ๆ ไปสองท่า หนึ่งในนั้นคือการโยงจาก boring tasks ไป purpose เริ่มจากให้สมาชิกแต่ละคนเอากระดาษ A4 แนวนอนมาแผ่นนึ

By Chokchai
ทำไมคนมีลูกถึกกันจัง?

ทำไมคนมีลูกถึกกันจัง?

ย้อนกลับไปปี 2013 มันเป็นเวลา 3 เดือนก่อนที่ผมจะตัดสินใจด้วยกันกับภรรยาว่าจะมีลูก แม่ยายคุยกับเราสองคนว่า… แม่ยาย: อายุก็เยอะขึ้นเรื่อยๆแล้วนะ ถ้าจะมี ก็ต้องมีตอนนี้แหละ ช้ากว่านี้ พอแก่ตัวไป มันจะมีผลกับพั

By Chokchai