<p>How do you write code that is easily read by other people and who have had no hand in writing any part of it?</p>

[{'answer_id': 39380, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 2, 'selected': False, 'text': "<p>Keep code nice, clear and simple. Don't comment what you're doing when it's obvious (for instance I know what a foreach or if does, I don't normally need an explanation).</p>\n\n<p>Code tricks (such as auto properties) that make simple things take up fewer lines are good too.</p>\n"}, {'answer_id': 39382, 'author': 'Christian Hagelid', 'author_id': 202, 'author_profile': 'https://Stackoverflow.com/users/202', 'pm_score': 0, 'selected': False, 'text': '<p>Probably the most important point is to keep your syntax consistent. I would also have a look at the design guidelines for the language you are writing in.</p>\n'}, {'answer_id': 39386, 'author': 'rjzii', 'author_id': 1185, 'author_profile': 'https://Stackoverflow.com/users/1185', 'pm_score': 3, 'selected': False, 'text': "<p>The best way to ensure that others can read your code is to make sure that it is clear and concise. Namely,</p>\n\n<ul>\n<li>Use self documenting names for variables, functions, and classes.</li>\n<li>Comment complex algorithms so that the reader doesn't have to spend to long figuring out what it does.</li>\n<li>Ensure that tabbing and line breaks are constant throughout the code.</li>\n</ul>\n\n<p>Beyond that you start to get in to the areas that might be a bit subjective, most people should agree on these items.</p>\n"}, {'answer_id': 39390, 'author': 'kemiller2002', 'author_id': 1942, 'author_profile': 'https://Stackoverflow.com/users/1942', 'pm_score': 1, 'selected': False, 'text': "<ol>\n<li>Document the code as to why it does what it does.  </li>\n<li>Make sure that all variables functions etc. are named consistently and descriptively</li>\n<li>Use white space to group logical portions of code together, so it flows while reading.</li>\n<li>Place the functions/methods etc. in a logical order.</li>\n<li>(this one is my personal preference) Make sure that code can easily be read on the screen without having to scroll horizontally (some people say vertically too, but this doesn't seem to bother me).</li>\n</ol>\n"}, {'answer_id': 39394, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 3, 'selected': False, 'text': '<p>This question is subjective, and should be avoided on StackOverflow, as per the <a href="https://stackoverflow.com/faq">FAQ</a></p>\n\n<blockquote>\n  <p><strong>What kind of questions should I not\n  ask here?</strong></p>\n  \n  <p>Avoid asking questions that are\n  <strong>subjective</strong>, argumentative, or require\n  extended discussion. This is a place\n  for questions that can be answered! </p>\n</blockquote>\n\n<h2>The short answer would be:</h2>\n\n<ul>\n<li><p>Avoid excessive commenting:</p>\n\n<pre><code>// add one to the count:\ni++;\n</code></pre></li>\n<li><p>Use good variable and method names:</p>\n\n<pre><code>int x = i + j;\nint runSum = prevSum += newValue;\n</code></pre></li>\n<li><p>Use coding shorthand where available:</p>\n\n<pre><code>if (x == y)\n{\n  z = a;\n}\nelse\n{\n  z = b;\n}\nz = (x == y) ? a : b;\n</code></pre></li>\n</ul>\n'}, {'answer_id': 39405, 'author': 'Robin Barnes', 'author_id': 1349865, 'author_profile': 'https://Stackoverflow.com/users/1349865', 'pm_score': 2, 'selected': False, 'text': '<p>Buy &amp; read <a href="https://rads.stackoverflow.com/amzn/click/com/0735619670" rel="nofollow noreferrer" rel="nofollow noreferrer">Code Complete 2</a>. There\'s loads of stuff in there about writing easy to read / maintain code.</p>\n'}, {'answer_id': 39465, 'author': 'Marcio Aguiar', 'author_id': 4213, 'author_profile': 'https://Stackoverflow.com/users/4213', 'pm_score': 2, 'selected': False, 'text': '<p>I don\'t think it\'s a subjective question, but it\'s too broad! It\'s not just about commenting and giving good variables names. It deals with how humans comprehends code. So your system must be implemented in a way that the reader can easily construct a mental model of its design in two way:</p>\n\n<ul>\n<li><p>Top-down: assuming the user knows the system domain, he tends to make assumptions on how it would be implemented, so he\'ll scan the system packages and classes looking for entities he can identify. Giving good names to your classes and properly modularizing it would help very much.</p></li>\n<li><p>Bottom-up: once the user reaches a portion of code he\'ll start navigation from there, building chunks of knowledge. If your system has low cohesion and lots of implicit dependencies the user will be lost.</p></li>\n</ul>\n\n<p>Kent Beck adopts three principles: Communication, Simplicity and Flexibility. Of course, sometimes you\'ll have to trade simplicity for flexibility, and vice-versa.</p>\n\n<p>This could go on and on. The answer to this question fits in a large book. As @rmbarnes suggested, buy and read Code Complete 2. I also suggest <a href="https://rads.stackoverflow.com/amzn/click/com/0321413091" rel="nofollow noreferrer" rel="nofollow noreferrer">Implementation Patterns</a> by Kent Beck - its highly related to your question.</p>\n'}, {'answer_id': 39480, 'author': 'Dewm Solo', 'author_id': 4225, 'author_profile': 'https://Stackoverflow.com/users/4225', 'pm_score': 0, 'selected': False, 'text': '<p>From being a developer with several years under the belt, this used to be a real question for me. I couldn\'t even say how many hours I passed thinking about this and trying different things in my code. The above answers are very nice too. I just want to add a thing or two.</p>\n\n<ul>\n<li><p>We each have different things that make our reading different than the others. Something that you find easy to read, might really be hard for somebody else to read.</p></li>\n<li><p>Cleanliness of your code is a very important aspect. Soon as it gets too cramped just forget about it.</p></li>\n<li><p>Most important: You are you own teacher. No matter what style you follow, you will want to change a thing or two based on your experience. As months pass and you have to go back to your old for fixes or documentation, you will have the "I can\'t believe I wrote code that reads like that" effect. Take notes of what was bugging you with the code readability and make sure not to write like that again.</p></li>\n</ul>\n'}, {'answer_id': 39501, 'author': 'Thomas Owens', 'author_id': 572, 'author_profile': 'https://Stackoverflow.com/users/572', 'pm_score': 1, 'selected': False, 'text': '<p>Since everyone else said pretty much what I\'m thinking when I read this question, I\'ll just share two books related to this subject that you might be interested in reading. These books use open source code examples to explain how to read and write high quality code. In addition to Code Complete, I think they are valuable resources when you want to write good code in any language.</p>\n\n<ul>\n<li><a href="https://rads.stackoverflow.com/amzn/click/com/0201799405" rel="nofollow noreferrer" rel="nofollow noreferrer">Code Reading: The Open Source Perspective</a></li>\n<li><a href="https://rads.stackoverflow.com/amzn/click/com/0321166078" rel="nofollow noreferrer" rel="nofollow noreferrer">Code Quality: The Open Source Perspective</a></li>\n</ul>\n'}, {'answer_id': 39650, 'author': 'David Basarab', 'author_id': 2469, 'author_profile': 'https://Stackoverflow.com/users/2469', 'pm_score': 0, 'selected': False, 'text': "<p>I am most likely in the minority, but I don't mind whitespace.  I LOVE WHITESPACE.  Since the compiler takes it out and HD space being so cheap I like to have white space in my code.  </p>\n\n<p>For example I like:</p>\n\n<pre><code>        int total = 10;\n        int sum = 0;\n\n        for (int i = 0; i &lt; total; i++)\n        {\n            sum += i;\n        }\n\n        // Next coding statement is a space below the bracket\n        return sum;\n</code></pre>\n\n<p>I do not like:</p>\n\n<pre><code>        int total = 10;int sum = 0;\n        for (int i = 0; i &lt; total; i++)\n        {\n            sum += i;\n        }\n        return sum;\n</code></pre>\n\n<p>What I also put in Brackets even though technically they are not needed.   The best example is the if statement.  I find it greatly helps readability.</p>\n\n<pre><code>if(true)\n    // some action\n\nif(true)\n{\n   // Some action\n}\n</code></pre>\n\n<p>The best code to me, is one that as simple as possible.  With the least comments as possible, and most importantly <strong><em>works</em></strong>.</p>\n"}, {'answer_id': 417185, 'author': 'Tom Moseley', 'author_id': 51185, 'author_profile': 'https://Stackoverflow.com/users/51185', 'pm_score': 1, 'selected': False, 'text': '<p>My rules:</p>\n\n<ol>\n<li>Give everything a meaningful name, and call it what it is. Avoid using "x" and "y" for variables. </li>\n<li>Don\'t abbreviate ANYTHING. I don\'t care how long the variable name is, don\'t abbreviate, even with comments. Interpretation of abbreviations is subjective. Does Cmp mean computer? Computer? Company? Compliment? Make it a strong rule, no exceptions, and its easy to follow.</li>\n<li>Don\'t put multiple statements on the same line. Each line performs a single action.</li>\n<li>Avoid Hungarian Notation like the plague. Or is it ntHungarian?</li>\n<li>Use brackets even for single-line (if, for) substructures. Indentation differences are too easy to lose.</li>\n</ol>\n'}, {'answer_id': 417330, 'author': 'borq', 'author_id': 42541, 'author_profile': 'https://Stackoverflow.com/users/42541', 'pm_score': 3, 'selected': False, 'text': '<p>You may want to take a look at <a href="https://rads.stackoverflow.com/amzn/click/com/0132350882" rel="noreferrer" rel="nofollow noreferrer">Clean Code</a> by Robert C. Martin. It offers up a lot of useful practices for ensuring your code is readable.</p>\n\n<p>Additionally, if your code is supported by a number of unit tests that thoroughly test your code, it offers a way for your user to understand the code by looking at what the tests are doing. You will also find that if you follow the Test Driven Development process, and you write tests for each bit of functionality, your functions tend to be small, do one thing only and do it well, and tend to flow more like a story than simply a large complex web of "stuff".</p>\n\n<p>Tests tend to stay up-to-date more than comments. I often ignore comments anymore due to simple fact that they become obsolete very quickly.</p>\n'}, {'answer_id': 417390, 'author': 'omermuhammed', 'author_id': 33121, 'author_profile': 'https://Stackoverflow.com/users/33121', 'pm_score': 1, 'selected': False, 'text': '<p>A lot of good answers here, I would like to add something from the perspective of an engineer who likes the big picture. I frequently found that getting a high level overview, in terms of class diagram or a package level overview (diagram/comments etc), heck if nothing exists a 10 line header comments in a file to help me a lot. We can use Doxygen/Javadocs to generate them, or spend 10-15 minutes to just jot down something in comments section.</p>\n\n<p>They dont have to be 100% accurate, and I doubt the overall structure of classes/packages will change without a complete rewrite.</p>\n\n<p>I personally found this kind of big picture overview very helpful and am sure there are others who feel the same.</p>\n'}]
['https://Stackoverflow.com/questions/39374', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4021/']