简体   繁体   中英

Variable declaration style in C#

Which one is recommended while writing Clean Code in C# or it doesn't matter?

a)

int count = 0, sum = 0;

b)

int count = 0;
int sum = 0;

I personally write code with b) style. I almost forgot that we can declare variables like a) style. So is there any coding style about that in Clean Code

I used both of them.

If I am declaring the variables only used for specific portion of the code for example inside a method, I am using like option a because space saving and do not disturb the flow of the code.

If I am declaring the variables in global, I am using like option b, beacuse I would like to see each and every variables and their purposes are not related.

There is no problem using a style but I think the best choice for clean code would be style b because of better readability.

It's cleaner when you declare the variables in separate lines even if they are of the same type, it is easier to read and understand your code.

and it's easy to add or remove variables in future.

There could be a situation when it's convenient to declare the variables on the same line if they are very closely related like int height = 0, width = 0; .

But again its opinion based and what standard is followed in your team/organization.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM