Minecolonies & Clean Code Adventures #1

in #utopian-io7 years ago (edited)

Hey everyone, after some execiting weeks for minecolonies I decided to calm down for a bit and to focus on some work on our code quality.
The moment before I started working on this we had over 1000 codacycodacy issues ranging from style to performance and possibly buggy code.


Code quality, what's that?

It's actually not as easy to define what code quality is. I'd measure the code quality is something we can measure in quantitative and qualitiative measures.

Quantitative:

  • Amount of documentation.
  • The complexity of the code.
  • How much of the code is following certain quality standards (linter, codacy, sonar)

Qualitative:

  • How easy is it for new users to get started with the code?

Which brings us straight to the next point.


Why should I care?

In general, if you code something, no one will be able to fix, maintain or work on top, your code can be thrown away since it automatically becomes useless.

While this is quite important within a company which always has to be able to main their code and, even after a worker left the company, has to take care of a certain project. This is even more important in open source projects.

If what you code isn't well documented and doesn't follow quality standards, most probably, after you stopped maintaining it, it will slowly decline into insignificance since no one will be willing to bring your project forward.

This means, if you don't take care of your code, most probably no one else will and all your work, eventually, was for nothing.

Therefore, if you have a project, and you put a lot of work in it, try to put that extra bit of effort in it, so that all your initial work is not for nothing further down the road.


What are example issues?

Example issues, can be found using either codacy or sonar:

https://www.codacy.com/app/Minecolonies/minecolonies/dashboard

https://sonar.minecolonies.com/

Nevertheless, I will extract some important issues right here:

First of all: Consistency

It doesn't matter how you want the brackets in java:

The java default:

if (something) {
    //do something else
}

The traditional:

if (something) 
{
    //do something else
}

My OCD let's me default to the second option, but it actually doesn't matter which you prefer, as long as you're consistent about it throughout all your code.

It seems like too many things are happening within this method:

If your methods are really long it will be really hard to find out what it's doing and to find potential bug. Break it down into smaller more specific methods.

If your classes import too many other classes, possibly too much is going on inside that class, and it might be worth to check if it doesn't make sense to break it down into more specialized classes as I did in:

https://steemit.com/utopian-io/@raycoms/minecolonies-and-core-rework-refactoring-madness

Just dropping numbers all over the code will be hard to maintain since it might be difficult to find out what they actually mean. Make constants out of them with a name which describes their purpose.

All that space wasted just to say:

return walkToBuilding() || !retrieveToolInHut(toolType, minimalLevel)

Those are just some of the issues which make your code harder to read or even error prone or also sometimes less efficient.


What did I do?

First of all I ran a formatter over all our code to add the final modifier to all our not further used variables.
I consider this a matter of taste fix, but, in my opinion it makes it way easier for new users to enter in your code when they have to think to remove a final when they're going to change a variable which avoids possible bugs from happening early on.

Like that 200 times all over our code.

Renamed quite a lot of variables to avoid confusion between class name and variable.

Added proper test names.

Removed around 10 not used variables.

And added StringBuilders instead of manual concatenation.

I worked on around 10 additional issues, but that'd be out of the scope of this post.
I hope I was able to introduce you guys a bit more about code quality and why it really matters.

And, I'll see you the next time I cleaned parts of our code.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

I really like how this post focus on development issues and explains to us all your opinion on best practices for code quality.

You're an example for us all. I'm learning a lot and that's great.

You can contact us on Discord.
[utopian-moderator]

Always trying to keep my posts informative also for people who do not necessarily use our modification but might read this post out of interest of how it is done and how to run a project like that =)

Hey @helo, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

Thanks for the article. I really appreciate people that consider source quality, clarity and optimisations. I'm a funny chap, but with Java, I must have the opening { on the same line as the if statement or command using it. So that I don't double up on the number of lines at the start of a method for example. I planned to write some 'best practices' that I've learnt in my 23 years of professional programming and apply them to the new Godot Engine tutorial that I'm going to write. Please drop by in a few weeks if you are interested. Thanks for this.

Hey @raycoms I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • You are generating more rewards than average for this category. Super!;)
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Like your contribution, upvote.

thanks, happy today

Hey @raycoms, you have dedicated your time in the development of minecolonies which is awesome. Your contribution in the community is huge and I appreciate it a lot. You are doing a great job. Keep up the good work.