views:

141

answers:

5

I think making code self-explanatory, without need for comments all around the place, is a big advantage. But could you suggest methods and techniques how to reduce the amount of code, make it more readable and understandable.

Also what do you think are the good techniques for reducing big if statements and nested for loops and other structures which sometimes are hard to understand at the first glance.

Here are few of the things I thought would my C# application more readable and self-explanatory:

  • Converting foreach loops into LINQ statements.
  • Using anonymous function to reduce amount of event handlers.

Any suggestion and/or comments are very welcome. Also suggestions about the books which covers these topics would be appreciated.

A: 

Aside from syntactical/structural considerations, consistency is extremely important - code styles and formatting preferences vary, which is fine, but within a project you should standardise as much as posibble to avoid having to keep readjusting yourself as you read through the code.

Andy
A: 

The main one I go for for readable code is variable names that make it obvious what is in the variable and method names that make it obvious what the method is doing.

If your if statements and for loops are too big then refactor their insides into new metods with sensible names.

Chris
+9  A: 

I recommend you take a look at Clean Code by Robert C Martin. It's an entire book dedicated to writing readable code. Highly recommended if you ask me.

Resharper is also very useful as it has numerous suggestions for naming, reducing nesting and the like.

Brian Rasmussen
+1 Excellent book
M.H
This book looks very like what I am looking for!
Vitalij
I use Resharper all the time, it is very handy. I only wonder why Microsoft hasn't put most of the Resharper features in VS by default.
Vitalij
+1  A: 

Use methods with named parameters with default values can help clean out overloads, which often will result in less code. I do that alot myself. Also makes an interface easier to read and use.

If a foreach or other loop only says something about the content of the loop items and not the method in general, I often refactor the loop body to a new method. This makes the first method easier to read.

Reversing an if-statement to reduce nesting can often make your code easier to read.

if (!something) return;
// more code here

This gets rid of the brackets and 1-2 lines.

If a method gets too big, refactor it into smaller methods.

Make the method and variable names selfexplainatory.

Paw Baltzersen
+1  A: 

Use descriptive variable and function names.

Break up large functions into subfunctions so that you group together statements that belong together, this can be good for code reuse to.

Try to keep functions as flat as possible and push nesting into separate functions, that way each nesting level can get its own descriptive function name.

I try to avoid nesting multiple ifs and for in the same function. If you have an if with big parts, try to refactor the parts to their own functions. That way the if will be much easier to understand and any explanation can be in the true/false function names.

Do not always convert to linq, large linq statements is often more difficult to read then a normal loop with if constructs and possible variables.

Do use variables for temporary data instead of wrapping statements around statements. This both enhance readability, gives you the opportunity to name the temp vars to explain and also makes debugging easier as the row will pinpoint an exact statement instead of a collection o nested statements.

There is a pretty good book I read about it called "Clean Code" from Prentice Hall that goes much deeper into this.

David Mårtensson