Link to pdf version on UVa OJ
This seems to be an easy problem. What could be important in solving this problem? The code seems straight forward, right? We check every single number and if it doesn't have repeating digits we increase the result by one.
The important point in solving this problem is to think modular, if you can think in terms of functions for such small codes, then your mind is structured enough to think this way for larger codes and programs.
Let's look at both and compare them.
This is the bad version of coding a solution.
Everything is in a loop, how can you explain this code to someone else? to your teammate. Can you understand this easily if you come back and read it 2 months later? Of course NOT.
We can easily extract two different functions out of this code. In main the only thing we do is reading input and preparing it in such a way that another function can work on it.
int countNumbersWithNoRepeatingDigits(int a, int b)
This function has the responsibility to receive two numbers and calculate all the numbers in this range (inclusive) that doesn't have repeating digits.
So now the only thing we do in main is this:
We read input and delegate the task to another function (separating concerns).
How does this function itself look like? There must be a loop that goes from a to b, and increases the result if that number doesn't have repeating digits. It seems we can define another function to do that for us, and inside function countNumbersWithNoRepeatingDigits just do the loop.
You see? A simple function that you can explain to anyone in less than 1 minute, and if you come back 2 months later you would be able to read it and understand it easily.
Then remains this last function that is responsible to check whether a number have repeating digits or not? The only concern of this function is this.
At the end we may pay a little cost for the function calls overhead (as you see in my UVa submissions), but that's negligible to the quality and maintainability of the code.
The bottom (first) submission is using functions as you see the running time is 0.010,
the top (second) submission is not using any functions and does everything in main function.
Keep breaking down your code into smaller functions! :)
1 - Link to the modular version on github
2 - Link to the bad and unreadable code on github
This seems to be an easy problem. What could be important in solving this problem? The code seems straight forward, right? We check every single number and if it doesn't have repeating digits we increase the result by one.
The important point in solving this problem is to think modular, if you can think in terms of functions for such small codes, then your mind is structured enough to think this way for larger codes and programs.
Let's look at both and compare them.
This is the bad version of coding a solution.
Everything is in a loop, how can you explain this code to someone else? to your teammate. Can you understand this easily if you come back and read it 2 months later? Of course NOT.
We can easily extract two different functions out of this code. In main the only thing we do is reading input and preparing it in such a way that another function can work on it.
int countNumbersWithNoRepeatingDigits(int a, int b)
This function has the responsibility to receive two numbers and calculate all the numbers in this range (inclusive) that doesn't have repeating digits.
So now the only thing we do in main is this:
We read input and delegate the task to another function (separating concerns).
How does this function itself look like? There must be a loop that goes from a to b, and increases the result if that number doesn't have repeating digits. It seems we can define another function to do that for us, and inside function countNumbersWithNoRepeatingDigits just do the loop.
You see? A simple function that you can explain to anyone in less than 1 minute, and if you come back 2 months later you would be able to read it and understand it easily.
Then remains this last function that is responsible to check whether a number have repeating digits or not? The only concern of this function is this.
At the end we may pay a little cost for the function calls overhead (as you see in my UVa submissions), but that's negligible to the quality and maintainability of the code.
The bottom (first) submission is using functions as you see the running time is 0.010,
the top (second) submission is not using any functions and does everything in main function.
Keep breaking down your code into smaller functions! :)
1 - Link to the modular version on github
2 - Link to the bad and unreadable code on github
No comments:
Post a Comment