Do you write readable code?

Do you write readable code?

by Manuel Vilhena, 17 July, 2018

Or better yet, what is readable code?

If you google how to write proper code, you’ll find a lot of answers and all of them will probably be correct, even if different. That’s because there isn’t a miracle formula: what some consider readable, others may not. There’s a lot that goes into proper coding, structure, naming, style, comments… everything plays a role in to how readable your code is going to be, and everyone has their own rules and preferences for it. Therefore, I propose something different: write whatever feels natural to you.

Creating code that feels natural is not really a science, there are no rules for it. Most of what goes into making good code comes from experience. Take my case as an example: I’ve been working as a developer for 8 years, most of that time was spent developing and maintaining large bank applications. One of the most important things when working on large applications is your ability to troubleshoot issues and understand the relationship between different code components. Applications this large have hundreds of thousands of lines of code, spread across hundreds of classes and methods. To troubleshoot something like this you need to be able to look at the code and, in 15 to 30 seconds, understand what a specific method or class does. That’s it for me, that’s what natural is. When I finish working, I go trough my code, and I think to myself, will I be able to understand this in 6 months? If not, then it probably needs some comments. Is it too confusing, too crowded? Then let’s do some refactoring. Is it easy on the eye? If not, then let’s group some code, add some spaces, change the line length, do whatever I feel would make it easy to read.

Guidelines, rules and colleagues change all the time and what someone considered proper code yesterday, today they might have a different idea about it. If you follow someone else’s notion of what’s right, you’d be spending a lot of time re-learning how to write code and not enough creating awesome cat-related websites doing what you like!

So, going back to my original question, is my code readable? Well, for me YES, it feels natural.

Here’s a short code snippet that I removed all the comments, especially for you. Does it look readable? Can you as a C# developer look at it and in 15 seconds figure out what it does?

 

by Luís, Sharepoint Developer @ Lisbon Nearshore