How to Write Clean Code
How to Write Clean Code
You know that you should write clean code, but how can you make sure you do it right?
Join the DZone community and get the full member experience.Join For Free
You've been hearing a lot about agile software development, get started with the eBook: Agile Product Development from 321 Gang.
As software developers, writing code is definitely a priority for us. Just like architects, whose task is to design beautiful buildings, our job is quite similar.
We're hired to create automation solutions to problems that exist in different industries. Sometimes, people only tend to evaluate the final product, which is the app/software/solution we present them. However, there is much more to this story. The foundation of the final product is the code. And, when the code is well written, everything flows much better.
So, how can you possibly write better and cleaner code? The cleaner your code, the better you'll be seen as a professional and the easier it will be for you to make adjustments, correct bugs, and implement new features in the future.
Are you interested in writing cleaner code? Watch this video and find out!
John Sonmez: Hey, what's up, John Sonmez from simpleprogrammer.com. I got this question about becoming better at writing clean code. I could definitely help you with that. This is something that I focused on a lot when I was a software developer and I think it's really important.
This email is from Pratham I believe it is. He says, "Hi John, I'm a huge fan of your YouTube videos. I'm an Android programmer and do freelancing stuff. I recently was working on a project where I got stuck into an implementing functionality. After a lot of research, I got to know that Google sample has that functionality covered in one of their open source apps." He says, "While browsing through their code I was pretty much amazed by their level of abstraction and the way they arrange things. I felt like I am not even close to that. I would love to know how I can bring myself to that level. Currently, I am not totally naïve as I was one year before, but what I think I lack the most is code abstraction, isolation, unit testing. Do you have any suggestions how can I prove and unlock "ZEN MODE" level?"
There's a couple of things here I think that would help. First of all, I'm going to recommend two books. You may have already them but for those of you that haven't the first one is Code Complete by Steve McConnell and that is a really good book. Get that book and read it, definitely. That will help you to write better code, clean code. Then surprisingly the second book is called Clean Code and it's by Robert Martin who actually wrote the foreword for my book and has been a—what I consider a mentor to me, really good book. Both of those books, if you read those books and you start implementing what they talk about in those books you're going to become better at writing code.
My ability to write code after I read those books increased five-six fold easily. I started writing a lot cleaner code, a lot of cleaner abstractions, all the stuff that you're talking about, that kind of code that you look at that code as a software developer and you're like, "Oh, that's like poetry. I could read this with a nice cigar and a Scotch." I'm exaggerating a little bit, but anyway, that kind of coding comes from — it came from me at least from reading those books and realizing that — seeing that kind of code.
That brings me to the other point which is you're doing the right thing already. You look at this code, you look at someone else's code who's written really good and clean code and elegant code and these abstractions, study it. Try to implement some of it. Try to modify it. If you've got the source code add to it. Pretend like you're working on another feature in that code base and actually work in that code base and use it and then start—when you work on your own projects start applying those things.
The other thing that I would say is to associate yourself with developers that are already writing good code. If you can pair program, this is one of the fastest ways to learn how to write good code is pair program with someone who is already writing good code. Find someone, offer to help them for free or to do some work with them, or if you have a developer in your organization that is already at that level say, "Can I sit down and pair program with you?" or even, "Can I just watch you and watch what you're doing?" If you can just shadow someone and look over their shoulder and see what they're doing. Sometimes the best way to gain expertise like super fast, you don't always have the opportunity, but if I could watch someone, like if I wanted to learn some new skill I guarantee you like the number one way that I would do it is I'd go find the expert and I'd say, "Can I pay you to watch you do what you're doing?" or "Can I help you in some way? All I want is just to be able to watch you."
Seeing that can make a huge difference. You see someone writing good code and if you can even ask a few questions, if you can say, "Oh, that's interesting. Why did you do that?" Not questioning it as it's wrong, but what was the thought process. You want to get an expert's thought process. When you get their thought process then you can start to adapt that thought process. We're very good at mimicking. We have these actually—these cells in our brain where we can mimic behavior when we see it. That's one of the things that we're really good at as humans. So if you see someone's behavior and you understand their thought pattern you can mimic that and you can emulate that exactly.
Anyway, I think those are probably the best ways that you can obviously—and it's going to come in time as well. As long as you're working on the crap, as long as you're trying to get better you're going to get better, but definitely look at good examples. Pair up with people that you can and read those 2 books that I recommended and that will help you to write better, cleaner code. All right.
Well, that's all I've got for that for today and if you have a question for me, email me at firstname.lastname@example.org. I will talk to you next time. Take care.
Published at DZone with permission of John Sonmez , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.