For Ex, if I use uiRecordApi and import getrecord but never used that, why is this a bad practise? Does it affect performance, script load times etc?
1 Answer
lightning/uiRecordApi
is a bad example, because in the environments it is supported, it is already loaded by the runtime, even if you never import it, because the runtime itself uses it. However, you are correct in thinking that an unused import may increase resource usage, which will primarily affect load times. More bandwidth will be used, more memory will be used, and more CPU time will be used to parse more code that will never be used. However, in the event of a single, unused import, it's not likely that any of those resources will be affected considerably. After all, most components are tiny, and components only load once no matter how many times they are used. An unused import is likely not going to be a problem.
The reason why many programmers would consider it a bad practice is due to the philosophy of programming. Programmers often self-describe as impatient and lazy. While these are bad traits in the general sense, it reflects the programmer's desire to write efficient code; it is good to be an impatient and lazy programmer. Since they are impatient, they tend to write code that builds/loads/runs/deploys as fast as possible. And since they are lazy, they tend to write code that uses the fewest lines of code while still being legible. Unused imports violates this philosophy because it is wasted code, and there is a non-zero penalty for adding in, or leaving, unused imports in the source code.
There are a number of rules or aphorisms that reflect the nature of programming philosophy. They are fittingly written as acronyms, since programmers don't want to have to write them out every time (the lazy aspect of programming philosophy). Generally, these aphorisms are considered good advice for anyone who wants to become a good programmer. I consider the rules important when writing any code, but given the relatively resource-limited nature of Salesforce, I would say that the rules are particularly important here.
The two most relevant rules for your question are YAGNI and KISS. KISS, or "Keep It Simple, Stupid", simply states that code should be as complicated as necessary, and no more. While KISS is less relevant than YAGNI, I do tend to interpret the rule as "the code should contain no more lines of code than necessary to complete the task." YAGNI, which stands for "You Ain't Gonna Need It", means you shouldn't write code you won't use, and shouldn't keep code you don't intend to use.
Whenever you're writing new code or maintaining existing code, you should always consider at least YAGNI. Ask yourself if there's any chance you might use that import later. If not, YAGNI. And if you don't need it, don't keep it around. We call unused or inefficient code "bloat", and the opposite of that is "lean" code. Code without any unnecessary "weight" attached to it. Lean code is always desirable; once you get out of the habit by making exceptions, you might go down the path of allowing more and more bloat over time. For this reason, I don't make exceptions in my own code, and I recommend that others don't, either.