What are some good C # programming methods to make the code more understandable?

I think that making the code self-evident, without having to comment on everything around the place, is a big advantage. But could you suggest methods and techniques how to reduce the amount of code, make it more understandable and understandable.

Also, what do you think are good methods for shortening large if statements and nested for loops and other structures that are sometimes difficult to understand at a glance.

Here are some of the things that I thought would become more understandable and understandable for my C # application:

  • Convert foreach loops to LINQ statements.
  • Using an anonymous function to reduce the number of event handlers.

Book suggestions that cover these topics will also be appreciated.

+4
source share
5 answers

I recommend you take a look at the Clean Code by Robert C Martin. This is a whole book devoted to writing readable code. Highly recommended if you ask me.

Resharper is also very useful, as it has many suggestions for naming, reducing nesting, etc.

+9
source

In addition to syntactic / structural considerations, consistency is extremely important - code styles and formatting preferences change, which is good, but you should standardize as much as you spare in the project to avoid having to rebuild yourself when you read the code.

+1
source

Using methods with named parameters with default values ​​can help clear overloads, which often leads to less code. I do it myself. Also simplifies reading and using the interface.

If the foreach or another loop just talks about the contents of the elements of the loop, and not in the general method, I often reorganize the body of the loop into a new method. This makes reading the first method easier.

Reversing an if statement to reduce nesting often makes the code easier to read.

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

This will get rid of the brackets and 1-2 lines.

If the method becomes too large, reorganize it into smaller methods.

Make method and variable names self-evident.

+1
source

Use descriptive variable and function names.

Divide large functions into sub-functions to group together statements that belong to each other, this can be useful for reusing code.

Try to keep the functions as flat as possible and embed the nested functions in separate functions, so each nesting level can get its own descriptive name for the function.

I try to avoid nesting multiple ifs for the same function. If you have any, if with large parts, try reorganizing the parts into your own functions. Thus, if it will be much easier to understand, and any explanation may be in the names of true / false functions.

Not always converted to linq, large linq instructions are often more difficult to read than a regular loop with if constructs and possible variables.

Use variables for temporary data instead of wrapping statements around statements. This improves readability, gives you the ability to name temporary vars for explanation, and also simplifies debugging, since the string will precisely determine the operator, not the collection o of nested statements.

There is a pretty good book that I read about it, entitled "Clean Code" from Prentice Hall, which is much deeper in that.

+1
source

The main thing that I turn to for readable code are the names of the variables that make it obvious what the names of the variables and methods that make it obvious that this method makes.

If your if statements and for loops are too large, reorganize their insides into new methods with reasonable names.

0
source

All Articles