3

Keeping a code clean and organized is important for future maintenance, specially for companies where programmers cycle (or get fired, hopefully not).

I have been looking around on Google and ive found few things that could help, like: naming the variables and methods properly etc... However, dispite the fact that I try to apply most of these rules properly, I still have troubles organizing my code properly, and I do comment everything so that the maintenance becomes easier.

I get lost in complex code where there are many nested statements like: if inside if inside for, etc...

To solve those problems I could do it with Netbeans, like this:

Stack overflow 1 Stack overflow 2

enter image description here

Netbeans allows compacting the code through an <editor-fold> tag. It is ok for now but the problem comes where others have to modify this code, because that xml tag is IDE dependant.

The question is: whats the correct way or pattern, to make code easy for understanding and clean at the same time, not IDE dependant.

Alpha2k
  • 2,212
  • 7
  • 38
  • 65
  • Document well and follow the conventions of Java. Pretty much it. You can add some comments to the code if it's not self-explaining. – Bubletan Apr 19 '15 at 17:46
  • This is a pretty broad question, and isn't really a specific programming problem. Maybe it'll fit best on [Programmer's Stack Exchange](http://programmers.stackexchange.com/)? Not sure.. Either way, please check out [on-topic](http://stackoverflow.com/help/on-topic). A good way to keep your code clean and organize is to abide by the [SOLID principles](http://en.m.wikipedia.org/wiki/SOLID_%28object-oriented_design%29) – Vince Apr 19 '15 at 17:54
  • Read: http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882 and you'll get many ideas regarding what you can do to simplify complex code logic (many nested if/else - as you described). – Nir Alfasi Apr 19 '15 at 18:04
  • @VinceEmigh this question is a poor fit for Programmers - it would be quickly voted down and closed over there, see http://meta.programmers.stackexchange.com/questions/6483/why-was-my-question-closed-or-down-voted/6490#6490 Recommended reading: **[What goes on Programmers.SE? A guide for Stack Overflow](http://meta.programmers.stackexchange.com/q/7182/31260)** – gnat Apr 19 '15 at 20:18
  • Just **do not** do this. Keep you methods below 20-30 lines and you won't feel any need for folding inside of them. The only plausible thing with complex code is to avoid it. "Extract method" is your best friend. – maaartinus Apr 19 '15 at 20:32

2 Answers2

4

Where to start

I'm pretty sure you don't apply all best practices if you have many difficult to understand fragments similar to:

if inside if inside for

A very good place to start with clean code is reading a book related to the topic. Although books are sometimes criticized, it allows to cover many aspects at once and make the knowledge possessed on tutorials, blog notes, SO more systematic. Personally, I can recommend Clean Code by Robert C. Martin.

If you are familiar with a theory, practice a lot. Write a code and improve it. Again and again. It is also useful to find somebody, who is more experienced and ask him about more difficult cases.

I suggested below three points that are very important from my point of view.

Self-documenting code

First of all, I would reduce the amount of comments to minimum, avoid dashed lines, "editor-folds" and so one. If you need to split your code with separators - the class is probably too long. Please, read more about good comments and self-documenting code here.

Unconditional programming

Many times it is a good idea to replace if-else and switch instructions with some OOP principles. This answer contains a couple of useful links.

Package/Class/Method size

Reorganize all of the routines, that are too big to easily understand. Split long methods into smaller, redesign God classes using OOP paradigms. The rule applies also to projects - you can split it into modules or subprojects and reuse. Single responsibility principle might be helpful - some examples.

Community
  • 1
  • 1
Artur Malinowski
  • 1,124
  • 10
  • 30
1

A good code is pretty much its own documentation.

Java documentation provides lots of details on how to write code in a better way.

My suggestions are:

Use proper naming conventions:

In your code i can see some code smells, the variables test and test2 can be the big cause of trouble for other programmers.

Try making modules:

Making modules can also make code easy to understand. For example creating possible methods out of long code and naming them properly. My teacher once told me: "if you cannot name a method or think of any appropriate name for your method it means you are doing it wrong"

Comments are thought to be important but my opinion is good written code do not rely on comments. You can add them to explain what is complex.

Proper Indentation is also important.

Subhan
  • 1,544
  • 3
  • 25
  • 58