Part of the culture of B&G is mentoring people new to software development, and it always takes me back to my days in college when nothing made sense or clicked like it should have. I could see it then in me and my friends, and I could see it in the students I was helping out as a TA. It took lots of practice for me to stop blindly using things the professors mentioned in class, and actually start reaching in my toolbox for the right tool based on real understanding of the problem and solution.
This process is not unique to me or to software development. There’s been a lot of talk about Malcolm Gladwell’s 10,000 hours rule, and deliberate practice. There are also a bunch of books that address the journey from novice to master. The journey takes time, and lots of struggling with little things, until eventually things start to click.
Although I cannot speed up that process for you, what I can do is provide a tip that I hope you follow in your journey from novice to master. The tip itself is simple: Reach for the keyboard last. In other words, when presented with a coding problem, the code is the last thing you should think about. Code is the byproduct of the solution, not the actual solution itself. That might be a bit counterintuitive, especially if your task is to write software, and you will struggle with following this rule, but with enough practice and being mindful about this step, it will become easier to follow as you progress in your career.
To illustrate this idea, let’s take a look at it in action. We’ll try to solve one of the code katas provided by http://codewars.com, which presents the following challenge:
Some numbers have funny properties. For example:
89 --> 8¹ + 9² = 89 * 1
695 --> 6² + 9³ + 5⁴= 1390 = 695 * 2
46288 --> 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51
Given a positive integer n written as abcd... (a, b, c, d... being digits) and a positive integer p we want to find a positive integer k, if it exists, such as the sum of the digits of n taken to the successive powers of p is equal to k * n. In other words:
Is there an integer k such as : (a ^ p + b ^ (p+1) + c ^(p+2) + d ^ (p+3) + ...) = n * k
If it is the case we will return k, if not return -1.
Note: n, p will always be given as strictly positive integers.
dig_pow(89, 1) should return 1 since 8¹ + 9² = 89 = 89 * 1
dig_pow(92, 1) should return -1 since there is no k such as 9¹ + 2² equals 92 * k
dig_pow(695, 2) should return 2 since 6² + 9³ + 5⁴= 1390 = 695 * 2
dig_pow(46288, 3) should return 51 since 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51
If I ask you to solve this problem and you commence by pasting some code or typing in your favorite code editor, you’re doing it wrong. It tells me that your focus is on lines of code instead of working software. And your focus should not just be on any working software, but on software that actually solves the intended problem.
This code challenge is interesting because I happen to think the description of the problem is cryptic. It needs a couple of read-throughs before really grasping what is being asked of us, and what are the expectations. In a case like this, the best approach is to dissect the requirements, so let’s do that:
- Given a positive integer
nand a positive integerp - Find
ksuch ask * n = (a ^ p + b ^ (p + 1) + c ^ (p + 2) ...wherea, b, and care the digits inn - Check if
kis an integer - If
kis an integer returnk, otherwise return-1
This should be a simpler way to explain the requirements. It takes all the words and strips them to the bare minimum while staying true to the original intent. It also structures the requirements in a chronological sense which makes it easier to then break it down into the steps that are required to get to the result. So let’s do that next, what are the steps required to complete this task?
step 1: take n and split it to the list of digits
step 2: go through the list of digits, one by one, d(i) where i is the index of the digit in the list starting at 0 from the left
step 3: raise d(i) to the power p + i and add it to the sum (sum += d(i) ^ (p + i))
step 4: determine the value of k such that k = sum / n
step 5: check if k is an integer
step 6: if k is an integer, return k. otherwise, return -1
Going from the original description to the steps description above is obvious, after the fact, but it requires an effort from someone who isn’t used to it. The thing to note is that the steps definition is still in plain english, with some mathematic notations. But up to this point, we have not discussed any code yet. That’s the key take away from this. The biggest effort a developer has to focus on is being able to take some requirements, whether well structured or not, and transform them into a set of understandable and replicable steps. The final step, translating those steps into code, is the easy part. Once you know what you’re building, what comes after is just transforming the english into syntax in whichever programming language you’re using. Google is great at that ;)
If we wanted to transform the steps above into Ruby code, we would simply take each step and write the corresponding Ruby code. So let’s do that together:
Step 1: digits = n.to_s.chars.map(&:to_i)
Step 2: digits.each_with_index { |d, i| }
Step 3: sum += d ** (p + i)
Step 4: k = sum / n
Step 5: k % 1 == 0
Step 6: k % 1 == 0 ? k : -1
If you’re thinking that this is too simple and too easy, well it is. It is so because we put in all the effort in the initial phase of translating the requirements into concise and understandable steps. Translating each step into code, is super easy. The only thing left to do is to put it all together:
def dig_pow n, p
sum = 0
n.to_s.chars.map(&:to_i).each_with_index { |d, i|
sum += d ** (p + i)
}
k = sum / n
k % 1 == 0 ? k : -1
end
At this point, you might not have the most optimal solution but you do have a correct one. You can refactor at this stage, assuming you’ve written tests to verify that your refactoring isn’t breaking the solution. As long as you’ve demonstrated that you understand the problem, and know how to get to the solution, the rest is implementation detail.
If, on the other hand, you started tackling this challenge by typing something like:
def dig_pow n, p
(1..p).each do
...
end
end
or
def dig_pow n, p
n >= 0 && p >= 0
k = n**p + n**(p+1) + n**(p+2)
end
That indicates to me that you are just guessing, you took a handful of spaghetti, threw it at the wall, and are hoping something sticks, without fully understanding what’s actually required and how to achieve. Yet, many developers start that way. I’ve done it too. We all have. We start by assuming we understand the problem and we start typing code thinking that the code will lead us to the solution. When in fact, typing should be the last thing we do in the process. Thinking being the first. Analyzing the second. Strategizing the third…
This was an example that might feel useless to you for all intents and purposes. To me, this example highlights a key issue with software development, which is the skill of understanding business requirements and the needs of the user before attempting to write any line of code. So if you practice what we’ve discussed in this post with every coding challenge or task you’re presented with, you are setting yourself up for success when it comes to acquiring requirements from a client or a product owner.
I hope this helps and makes your journey on your way to mastery less painful :)