The 8-Hour Debug: How a Silly Mistake Cost Me a Day

mmvergara - Jun 1 - - Dev Community

The Function

function getEnrollmentByStudentIdAndCourseId(
  studentId: string,
  courseId: string
) {
  // fetch logic
  return result;
}
Enter fullscreen mode Exit fullscreen mode

The Mistake

Believe it or not this function caused me a day of debugging. Because of the way I was calling it like this.

const studentId = "ID";
const courseId = "ID";

const enrollment = getEnrollmentByStudentIdAndCourseId(courseId, studentId);
Enter fullscreen mode Exit fullscreen mode

I was passing the arguments in the wrong order. The function signature was getEnrollmentByStudentIdAndCourseId(studentId, courseId) but I was calling it with getEnrollmentByStudentIdAndCourseId(courseId, studentId).

The reason why i took so long debugging it was because function was still returning a result even though the arguments were in the wrong order, but it was the wrong result. I think this is ultimately a mistake i made by writing a bad prisma query that caused it.

The Adjustment

make the argument passing more explicit by using object

type Args = {
  courseId: string;
  studentId: string;
};

function getEnrollmentByStudentIdAndCourseId({ courseId, studentId }: Args) {
  // fetch logic
  return result;
}
Enter fullscreen mode Exit fullscreen mode

Ever since then, when I have a function that takes multiple arguments of the same type like IDs, I started to use object destructuring to make the argument passing more. And also like... made better prisma queries.

I hope this helps you avoid the same mistake I made. Happy coding!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .