To Stack Overflow or Not To Stack Overflow

Your search should not end there

Subscribe to my newsletter and never miss my upcoming articles

Let me paint a picture for you to get started.

Imagine yourself as a backend developer, working on a Spring Boot service.

It is a typical day at work and you are working on a feature. The feature involves creating two database entities called Parent and Child.

The requirement is as follows:

  • Parent can have multiple child entires.

  • Ability to create Parent .

  • Ability to create Child .

  • When you delete Parent, all the Child entries should also be deleted.

Pretty simple, regular, standard stuff.

Developer’s Typical Day

You started implementing it, you have created entities and tables. The ability to create Parent and Child is also done. You create a repository to handle creating and finding the entities from the database.

When you start implementing the delete functionality of the parent, you start looking into various options that Spring supports. You start with a Google search with “Spring data remove child entity with parent entity”

You get a solution on StackOverflow. You jump to the answer which is a one-line addition.

@OneToMany(mappedBy="class", 
cascade= {CascadeType.PERSIST, CascadeType.REMOVE}, orphanRemoval=true)
Set<Child> childList;

This does the job and you are able to delete Child whenever a Parent is deleted.

Quite neat and quick.

You go home feeling happy for making good progress on the feature. Its a happy day.

The Problem

Now if you find yourself copying that snippet of code into your project and moving on, you are doing yourself a huge disservice. While you may have gotten past your blocker, you have passed up a big learning opportunity while also performing a dishonest and irresponsible coding practice.

Let’s start with the most obvious problem:

You haven’t really learned anything!

You’ve just stumbled across a great opportunity to do some self-learning and fill in one of your knowledge gaps, improving your skills as a programmer. Instead, all you did was hit ‘ctrl-v’, and you will probably get stuck again and again on that same problem in the future.

If you find yourself going back to the same Stack Overflow link 3 or 4 times, you should really be asking yourself if it’s to your benefit to keep plagiarizing that same snippet rather than learning how to solve the problem on your own.

This is why so many developers would be able to answer the question.

What happens when you put that annotation?

But would fail to answer the question.

How does that annotation work?

So What Should You Do?

When you find a solution on the Stack Overflow thread. Start by reading it thoroughly. Read the entire question and look at the code. Also read all the comments on the thread so you can see the solution in context and ensure that it really is what you’re looking for.

After that, it’s time to go through the code line by line, one character at a time, and make sure you understand every piece of it. If there is anything in the code you don’t understand, you should open another tab and start researching until you do get it.

Read the official documentation. It helps you learn and also helps you understand alternative solutions. There might be a better option for your situation. For instance, the docs for the problem discussed above gives you detailed information about what else is possible with the annotation.

Benefits

By fully understanding the code, you will know if it’s really the solution you want, and which parts of it you need to keep. There’s a good chance the solution offered has some extra pieces you don’t need.

It is even possible to come up with a better approach since you understand how it works. And if you do come up with a better solution, do not forget to answer that on StackOverflow.

Yes, it can be tedious and slow-going, but it’s also a great way to learn, and if you make this a habit, your arsenal of skills as a developer will grow quickly.

Conclusion

Being a developer gives you a lot of power. It is because we are constantly creating stuff for other people to use. We are surrounded by software from all sides and this makes our job very critical. Making sure that we only put code that we understand is immensely important. I hope this helps you become a better developer.

“I’m not a great programmer; I’m just a good programmer with great habits.” ― Kent Beck

No Comments Yet