readability - Obfuscated C# Code -


Years ago, there was a competition to see who could produce the most vague C code, and some Results are dramatically unreadable. It was the same that you can actually screw things with the preprocessor.

However, many new features of C # provide an amazing opportunity to obscure the code. I was thinking that anyone has any opinion on finding the right balance between the code and the compatibility of the code. Let me give an example for discussion, the function of filling the item in a ListView. (Yes, I know that you can do this with data binding, but go with me here.)

The control is two columns that the stroke person {the name of the public string; Public string address; };

A clear and straightforward way to do this:

  Fill private zeros (person [people]) {foreach (people in person) {string [] column = New string [2]; Column [0] = person Name; Column [1] = person.address; ListViewItem Item = New ListViewItem (Column); ListView1.items.Add (item); }}  

Explicit and simple to understand.

I can write it like this:

  Fill private zeros (person [people]) {foreach (people in person) {string [] column = New string [] {person. Name, person.address}; ListViewItem Item = New ListViewItem (Column); ListView1.items.Add (item); }}  

Or even:

  Fill private zeros (person [people]) {foreach (different person in people) // Note Use the typed word here {ListView1.items.Add (New list view item (new string [] {person.name, person.address}); Finally, I can write it like this:  
  Fill private zeros (person [people]) {Array.ForH (People, Item = & Gt; List View1.items. Add (New ListViewItem (new string [] {person.name, person.address})}  

Uses different new features of each language to greater or less extent. How can you find a balance between comprehension and clarity? Should we evaporate in our annual C # competition?

Always in favor of maintenance:

Do you know what is difficult? Write code, which can read and keep others. Any idiot can write code that compiles and retains It is impossible.

Edit:

"Any silly computer can write codes, good programmers write The code that humans can understand. "

  • Martin Fowler, Refactoring: Improving the Des Ex Fire Od

Thanks for getting the above quotation. Apologies to Fowler for killing his quotation; I knew that before I had read, I just do not remember.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -