I started programming nearly 31 years ago, where memory and CPU are scarce. Maybe this is why I agree with your father.
I do believe a good coder needs to worry about optimisation in every line of code. But I would add to this sentence: as much as it worths and always trying to favor readability over optimisation in places where computations are not critical.
For example, in another thread, a guy asked about the code below, which solution he would choose: CPU or Memory.
public class Main {
public static int totalRevenue;
public static int totalProfit;
public static int option1(int numSold, int price, int cost) {
// Option 1 (memory)
totalRevenue += numSold * price;
totalProfit += numSold * price - numSold * cost;
return numSold * price;
}
public static int option2(int numSold, int price, int cost) {
// Option 2 (time)
int saleRevenue = numSold * price;
totalRevenue += saleRevenue;
totalProfit += saleRevenue - numSold * cost;
return saleRevenue;
}
}
This code generates the following bytecode:
public class Main {
public static int totalRevenue;
public static int totalProfit;
public Main();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static int option1(int, int, int);
Code:
0: getstatic #2 // Field totalRevenue:I
3: iload_0
4: iload_1
5: imul
6: iadd
7: putstatic #2 // Field totalRevenue:I
10: getstatic #3 // Field totalProfit:I
13: iload_0
14: iload_1
15: imul
16: iload_0
17: iload_2
18: imul
19: isub
20: iadd
21: putstatic #3 // Field totalProfit:I
24: iload_0
25: iload_1
26: imul
27: ireturn
public static int option2(int, int, int);
Code:
0: iload_0
1: iload_1
2: imul
3: istore_3
4: getstatic #2 // Field totalRevenue:I
7: iload_3
8: iadd
9: putstatic #2 // Field totalRevenue:I
12: getstatic #3 // Field totalProfit:I
15: iload_3
16: iload_0
17: iload_2
18: imul
19: isub
20: iadd
21: putstatic #3 // Field totalProfit:I
24: iload_3
25: ireturn
}
For the sake of understandability, 'iload_' load data from stack and 'istore_' save data into stack.
It's easy to notice that you really saved one memory allocation in one solution spending more CPU cycles. That kind of question may arise even for experienced programmers.
But there is a catch in this example: stack memory is pre-allocated in blocks, not at byte level. Each time a thread runs, it will allocate a certain amount of memory to use in stack operations. So unless you are lucky enough to have these 4 bytes overflow your stack (and requiring you to setup a larger stack size), you will be ending up spending the same memory amount to run both examples. But in the second one, you are spending more cycles doing math calculations and your code is much less readable.
I brought it here to give a sample that discussing about optimisations in small portions of code is not evil. Some guy closed that question pointing to this thread. But this thread don't answer that specific question. It only discuss about "don't worry about optimisation". And due to this stupid mantra, people feel that is right not go to deep understanding.
There is a reasonable answer to that question, and maybe one can even give a better answer about the internals. Once learned, a good programmer will be able to repeat that pattern instinctively, and will become a better programmer. Programmers must have critical thinking about their code and must try to understand, as much as possible, how machine will process it.
I'm not advocating here that we all need to optimize little things. Remember that I recommended to favor readability over micro-optimisations.
I just believe that repeating the mantra "don't worry about optimisation" don't make things better. It only make programers lazy. Programmers must be instigated to know how things work behind the scenes. This way people can pursue to write better code, and wisely choose when not to optimise.
coder does not consider performance in their code even at the micro level, they are not good programmers
is very different from micro-optimizing. It's just good coding.