This site lists the protips that we shared with students during our courses
As an instructor we sometimes get questions like
for
-loops, or, for C# is it always better to use LINQ over for
-loops)if
-statements?These are tricky questions to answer in short fashion. Whatever I write below will (and rightly so) render a long array of follow-up questions. And you will probably start all questions with “but what if … ” or “but that doesn’t really work for” etc.
Because, as with many things that are crafts or possible to solve in many ways (such as software development), it depends. On a lot of things.
Consider the question:
Can you please teach me about system architecture and system design?
That is a relevant question, but sadly the answer is should be No
. The main reason for this is that designing a system (“architect-ing” a system) is a skill that requires some seniority. Not that older peopler are smarter (cough cough) but designing a system has a lot of trade-offs that needs to be considered in each individual case. You can also cause a lot of extra work for yourself by doing it wrong from the get go.
There’s nothing strange with that, and says nothing about your own competence - it’s just that some things that you do requires experience. Imagine an accountant on their first day on their job being asked to setup a good accounting instruction for the company. That would be extremely hard to do, because the poor (junior) accountant don’t have a lot of experience in what works well in certain situations.
The problem is that the question is both a general question that requires specific knowledge to the problem at hand to answer, but also making the distinction for when something is better or worse, is an expert judgment call. It can only be done with experience. Before you have that experience it can be hard to even understand what a recommendation means, or why one is better than the other.
And, even worse, you might follow an advice without understanding why and then end up in cargo culting (funny term - look it up). Which basically means that you follow advice blindly and always:
for
-loopsI would however never resort to black and white distinctions like never
or always
. (I hope y’all noticed the first never
in the sentence above…even writing about this is hard and have it depends
painted all over).
Ok - let my now try to answer one example above and see if I can make myself any clearer… (hold me. I’m scared)
is it always better to use array-methods over
for
-loops, or, for C# is it always better to use LINQ overfor
-loops)
There’s absolutely nothing wrong with for
-loops to iterate over elements, and in many cases they are even to prefer over other methods.
And the same can be said about Linq and array methods. They are also good.
Here are my advices:
for
-loop has several nestings … it’s probably better with an array method linq
if
-statment … an if
-statement might be better.
-Don’t mix unless you absolutely have to. Don’t have a method mixing two different approaches
-Practice both ways (linq and not linq for example) so that you are well familiar with them.What I did in my answer above is that let a prinicple guide me. if
vs Linq is a question about a practice that tells us HOW. But it’s more important to be guided through a WHY (here represented by the principle of making code readable and easy to understand).
I’ve found this approach to be good guiding star for me when making decisions of which approach to use, before I have enough experience to make the judgement call by heart.
I hope you find this useful, even though it’s very abstract.
A common question that we get from developers at the end of the bootcamp is:
But it begs the question “So, I’m smoked?! Where should start?” and that is the reason for this (VERY) long blog post.
I refuse to think that I couldn’t give you some good pointers and help you to “fall in the pit of success”.