Hazard 1: 90% of everything is crap
This is part 1 of my guide to writing better code
90% of everything is crap. That is true, whether you are talking about physics, chemistry, evolutionary psychology, sociology, medicine—you name it—rock music, country western. 90% of everything is crap.
You have become stuck in this hazard if you're learning about a new programming practice/approach and you:
- Read articles that explain how the practice can be implemented but not why, where and when it should be used.
- Look at the practice in isolation without weighing it up against alternative approaches
- Read articles that are written by people who don't understand the subject much more than you do
- Pay attention to someone just because they are experienced
Only a small minority of articles about global variables say they are bad practice
When learning about programming concepts there is a lot of information out there. A per the rule, 90% of it is crap. My major gripe with programming focused articles is that they teach how and then stop. Here's how to use inheritance. Here's how to use dependency injection.
Programming articles almost always focus entirely on how. They show you how to use a fancy new tool or programming practice and that's it. What most tutorials and articles omit is the more important aspects of why you should use it over alternatives, when and where the practice is beneficial.
If you've been programming more than a year or so you probably already know that global variables are deemed bad practice by almost every developer.
It's surprising then, how comparatively little information about global variables mentions that. I looked through the first 100 articles about global variables that came up on Google (ignoring forum posts and question/answer sites, focusing on articles written solely about global variables). Only 16 of the 100 articles mentioned that they are considered bad practice and suggested avoiding their use.
Of those 16, 11 provided examples of how to solve the a problem with global variables then showed solving the same problem using an alternative (recommended) approach.
Only 16% of articles talking specifically demonstrating global variables even mentioned that they are considered bad practice and should be avoided? If that's the situation for a practice as derided as global variables then what chance does anyone looking up other more nuanced or more advanced practice have about finding out when/if the practice should be used?
So what should you look for and avoid when learning a new programming practice?
Chris (as Luke Skywalker): You don't believe in the force, do you?
Peter (as Han Solo): Oh, you mean that thing you just learned about three hours ago, and am now judging me for not believing in?
I've nothing against new programmers and people trying to learn. Everyone has to start somewhere! However, there is a trend I've noticed over the last few years of people at a very junior level posting articles (and videos, oh so many videos) explaining the thing they obviously just learned that morning. I first noticed it on Linux sysadmin sites where people would post how-to articles about common Linux commands. However, I've increasingly noticed it in infecting programming topics as well.
You search for something and some novice developer's blog or site with the word "tutorial" in the domain, spewing hundreds of articles a day for clicks comes up telling you how to create functions in PHP. It's rarely an advanced topic because the author only learned of it the morning they wrote the article on their heavily ad-ridden site to get your traffic. It's usually badly worded, out of date, flat out wrong or missing some fundamental detail, but there it is, somehow on the first page of google.
I could use this opportunity to bash W3Schools but I won't. Instead, I'll point to StackOverflow. The place where newbies upvote terribly inefficient answers because the code works as-is. The answer becomes the top answer and nobody ever learns the proper way to do something.
Ok, I'm being harsh on StackOverflow here, but I'm sure you've all seen answers like that. Take a look at the second top answer here Got a notice about mysql being deprecated? Just suppress the error! At least the selected answer is fairly relevant (though should really mention difference between PDO and MySQL).
This is a problem, especially if you're just starting out yourself. How do you know when an answer that is working is a bad solution to the problem you have?
I just told you to avoid newbies and now I'm telling you to avoid experts?
Sort of. What I will stress is that doesn't matter who someone is, it's what they're saying and how they justify it that counts. Just because someone works on a big framework or writes a popular blog doesn't make what they're saying any more correct than anyone else. They can usually express themselves well and have a good understanding of the concepts but aren't always right. Don't take people's reputation/seniority as a reason to take what they are saying any more seriously than anyone else. Look at their reasoning and justification for what they say.
I've been programming since 1998. I've worked professionally as a web developer for well over 10 years, I teach at a university, I've written a book on learning PHP and I'm doing a PhD in programming practices. None of this makes anything I say any more correct than anyone else.. The same is true of everyone. So what should you look for?
What to look for when learning
Beyond the very basics (ifs, loops, functions, etc) when you look something up, you are looking for a solution to a problem. For example, how can I make the database connection accessible in this method?. Whatever your problem is, there is more than one way to solve it.
Knowing that there is more than one solution to your problem whenever you land on a page that tries to teach you how to solve it, ask the following questions:
- Does the article provide a complete example of using the practice?
- Does the article discuss any negative aspects of the practice?
- Does the article show how the problem can be solved using different approaches?
- Does the article weigh up the pros/cons of each of those approaches?
- Is there a recommendation about when/where/if the practice is appropriate to use?
If it only gives you a simple example of using the practice, move on and find a more comprehensive article.
For example, consider two articles covering the same topic:
- Here's how to use the Singleton pattern
- Here's how to use the Singleton Pattern, the problems it introduces, how you can solve the same problem with Dependency Injection and why dependency injection is the preferred approach
The author of the second article has clearly put thought into when the practice should (if ever) be used. The author of the first has simply parroted a how-to guide. Unfortunately, those articles which explain how but miss out why and when outnumber the more comprehensive articles at least ten to one.
Anyone who landed on the first article would go away and play with their new toy creating singletons all over the place. However, people who looked at the second article would understand that a singleton is deemed bad practice and know to avoid it.
Someone who read the first article will eventually run into the pitfalls created by singletons and do further research, finding out what the reader of the second article already knew.
You want to know the airbag is faulty before you buy the car or you won't find out until you crash.
I recommend paying more attention to articles that compare the concept you're trying to learn to alternative approaches. Not only will you see two different solutions to your problem and be better informed but you'll see why one method is preferable over another.
Two excellent examples of authors who take the second approach are Misko Hevery and Yegor Bugayenko. They both have all kinds of impressive credentials and a hell of a lot of experience. But it's not relevant at all to what they are saying. Their articles cover topics in depth and they show different approaches to a problem and explain why one approach is better than another. Their experience is what makes it possible for them to do this, but they don't use their authority to make a point.
Find the 10% that isn't crap, I just told you how to spot it. Learn when and where a practice is applicable rather than just how to use it. Don't get excited when you learn a new practice, it's probably not as useful as you think. After all, 90% of everything is crap, programming practices included.