Tuesday, December 31, 2019

Solving inheritance ambiguity with composition

Let's say you have the following C++ classes, making use of the observer pattern:

In implementation of class A, you call Notify(T) of CObservable.

When you compile, you will get a message that there is ambiguity in calling Notify(), because the compiler does not know if it should call Notify(T) with class T1 as a parameter or T2.

The solution is to remove CObservable from inherited classes and introduce a field, i.e. using composition over inheritance. You also need to have a registerT2Observer() method that in turn calls the register method of CObservable:

Letters to a novice programmer

Dear novice, this time it has been a short while, isn't it? When you have a dll that you call from both a Java app and C++ app with the same inputs but the outputs differ, do not suspect that there might be something wrong with the operating system or JNI. Your problems will always be due to something you did wrong.

You should write some debug code inside the C++ dll that prints dll inputs to a file. Run the dll from Java and C++ and compare dll inputs. I guarantee you that you will see a difference there because most probably you did something wrong before passing the inputs to dll like using a wrong index or making an erroneous unit conversion.

Another tip: When comparing files, never trust your eyes, always use a comparison tool like Total Commander's "Compare by content" or Beyond Compare.

Thursday, December 26, 2019

Letters to a novice programmer

Dear novice, it has been a while since my last letter. Today I want to stress the importance of revisiting your code every couple of months or so and asking yourself "could I achieve the same functionality with less code?". In other words, be your own garbage collector. If you do this a couple of times, your code might graduate from being miserable to being tolerable. And who knows, one day it might even attain the coveted status of being "elegant"?

Today's example concerns avoiding unnecessary local variables to reduce clutter:

Wednesday, December 25, 2019

Lessons from teaching my kid math for 10 months with Khan Academy

I have been teaching my 7 year old child mathematics with Khan Academy since February 2019. Everyday at about 19:00, we have a 15 minute session. Right now we are at 85% of 4th grade. During this time, I discovered that the difficulty of math does not lie in concepts, but in the high degree of attention it requires. Contrast a reading assignment with a math problem; In reading, you might omit a few letters, even words and still get the essence of the text and be able to answer questions correctly. With a math problem, if you misread even one symbol, you will be completely wrong. In this respect, math resembles software development.

During our problem solving sessions, I saw again and again that my son had no problem with the subject matter but was usually missing a bit and getting the answer wrong. When I helped him just a little by telling him to be more careful, he was able to solve most of the problems.

I suspect many students who are bad at math struggle due to a lack of attention. Unfortunately, improving attention requires one on one care, which the education system cannot provide. Parents have to help their kids.

Other lessons learnt:
  1. It is frustrating to witness that your child forgets the math rule you explained a minute ago. Be patient and repeat as much as necessary.
  2. Only advance to the next subject when your child can solve all of the end of chapter questions without any assistance from you. You might be dismayed by how many times he might have to repeat that test. As an example, my child solved all 18 questions of Understanding Decimals unit test in one sitting on his 13th try. It took him almost two weeks of frustration and tears! This sharpened his attention and cured his false sense of self-confidence(!)
  3. Every couple of months, revisit older subjects by re-solving their unit tests.
  4. My son is impatient and wants to solve problems quickly in his head. This works for simple operations but once you get to problems involving multiple steps like 12*17, you have to force him to use pencil and paper and show each step clearly to you.
  5. Since every child is different (and also rapidly changes with time), progress and tactics will be different. You have to constantly observe and adapt to the learning/teaching challenges you and your child will face.
  6. Persistence is key and the hardest part. It is easy to teach a couple of times, but most parents struggle to do it consistently.
How am I able to keep him marching on through this difficult journey? First, I have earned his respect and trust by being useful to him in subjects he cares about like video games, internet and sports. Second, I do not refrain from punishing him by reducing his video game time if he gets out of line.

Music: Chumbawamba - Tubthumping