<p>I'm not talking about how to indent here. I'm looking for suggestions about the best way of organizing the chunks of code in a source file.</p>

<p>Do you arrange methods alphabetically? In the order you wrote them? Thematically? In some kind of 'didactic' order?</p>

<p>What organizing principles do you follow? Why?</p>

[{'answer_id': 73081, 'author': 'Marc Gear', 'author_id': 6563, 'author_profile': 'https://Stackoverflow.com/users/6563', 'pm_score': 3, 'selected': False, 'text': "<p>i normally order by the following</p>\n\n<ol>\n<li>constructors</li>\n<li>destructors</li>\n<li>getters</li>\n<li>setters</li>\n<li>any 'magic' methods </li>\n<li>methods for changing the persisted state of reciever (save() etc)</li>\n<li>behaviors</li>\n<li>public helper methods</li>\n<li>private/protected helper methods</li>\n<li>anything else (although if there is anything else its normally a sign that some refactoring is necessary)</li>\n</ol>\n"}, {'answer_id': 73089, 'author': 'Fire Lancer', 'author_id': 6266, 'author_profile': 'https://Stackoverflow.com/users/6266', 'pm_score': 0, 'selected': False, 'text': '<p>I group them based on what there doing, and then in the order I wrote them (alphabetically would probs be better though)</p>\n\n<p>eg in texture.cpp I have:</p>\n\n<pre><code>//====(DE)CONSTRUCTOR====\n...\n//====LOAD FUNCTIONS====\n...\n//====SAVE FUNCTIONS====\n...\n//====RESOURCE MANGEMENT FUNCTIONS====\n//(preventing multiple copies being loaded etc)\n...\n//====UTILL FUNCTIONS====\n//getting texture details, etc\n...\n//====OVERLOADED OPERTORS====\n....\n</code></pre>\n'}, {'answer_id': 73099, 'author': 'betelgeuce', 'author_id': 366182, 'author_profile': 'https://Stackoverflow.com/users/366182', 'pm_score': 0, 'selected': False, 'text': '<p>Pretty much use this approach for anything I am coding in. Good structure and well commented code makes good reading</p>\n\n<ul>\n<li>Global Variables  </li>\n<li>Functions</li>\n<li>Main Body/Method</li>\n</ul>\n'}, {'answer_id': 73112, 'author': 'Allan Wind', 'author_id': 9706, 'author_profile': 'https://Stackoverflow.com/users/9706', 'pm_score': 0, 'selected': False, 'text': '<p>public, protected and then private and within each section alphabetically although I often list constructor first and deconstructor last.</p>\n\n<p>/Allan</p>\n'}, {'answer_id': 73115, 'author': '17 of 26', 'author_id': 2284, 'author_profile': 'https://Stackoverflow.com/users/2284', 'pm_score': 0, 'selected': False, 'text': "<p>I tend to group things thematically for lack of a better word.</p>\n\n<p>For example, if I had a public method that used two private methods in the course of doing its work then I would group all three together in the implementation file since odds are good that if you're going to be looking at one of them then you'll need to look at one of the others.</p>\n\n<p>I also always group get/set methods for a particular class member.</p>\n\n<p>It's really personal preference, especially with modern IDEs since there are a lot of features that allow you to automatically jump to locations in the code.</p>\n"}, {'answer_id': 73118, 'author': 'David Smart', 'author_id': 9623, 'author_profile': 'https://Stackoverflow.com/users/9623', 'pm_score': 1, 'selected': False, 'text': '<p>I tend to group methods that relate to each other.  Use of a good IDE removes much of this concern.  Alphabetizing methods seems like a waste of effort to me.</p>\n'}, {'answer_id': 73121, 'author': 'jtimberman', 'author_id': 7672, 'author_profile': 'https://Stackoverflow.com/users/7672', 'pm_score': 0, 'selected': False, 'text': "<p>I like to keep things simple, so I don't stuff a bunch of methods in a class. Within a class, I usually have the most commonly used (or modified-by-me ;-)) methods listed first. As far as specific code organization goes, each set of methods belongs to a class, so it organizes by itself.</p>\n\n<p>I make use of my editor's search feature, and code folding to navigate through large source files. Similarly, I make use of search features to find things in other contexts too. A grand organization scheme never suited me, so I rely on the power of search in all things, not just code.</p>\n"}, {'answer_id': 73133, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>Interesting point.  I hadn't really thought about this.</p>\n\n<p>I tend to put frequently-accessed functions at the top (utility functions, and such), as they're most likely need tweaking.</p>\n\n<p>I don't think organization is particularly important, as I can find any function quickly.  I don't scroll through my file to find a function; I search for it.</p>\n\n<p>In C++, I do expect that the functions in the .cpp file are in the same order in which they're declared in the .h file.  Which is usually constructors, followed by destructors, followed by primary/central functionality functions, followed by utility functions.</p>\n"}, {'answer_id': 73195, 'author': 'jb.', 'author_id': 7918, 'author_profile': 'https://Stackoverflow.com/users/7918', 'pm_score': 2, 'selected': False, 'text': "<p>I tend to use following pattern:</p>\n\n<ul>\n<li>public static final variables</li>\n<li>static functions, static blocks </li>\n<li>variables </li>\n<li>constructors </li>\n<li>functions that do something related to logic</li>\n<li>getters and setters (are uninteresing mostly so there is no need to read them)</li>\n</ul>\n\n<p>I'm have no pattern of including local classes, and mostly I put them on top of first method that uses them. </p>\n\n<p>I don't like separating methods depending on access level. If some public method uses some private method they will be close to one another. </p>\n"}, {'answer_id': 74316, 'author': 'Kristian', 'author_id': 12911, 'author_profile': 'https://Stackoverflow.com/users/12911', 'pm_score': 0, 'selected': False, 'text': "<p>I mostly write C code and I tend to order by dependency. If possible I try to match my source code file with me header files, but generally it's if void a() uses int b(char *foo), than int b(char *foo) comes first.</p>\n\n<p>Saves me from adding entries to the header file for local functions.</p>\n\n<p>For the rest it's mainly alphabetic actually, makes searching easier.</p>\n"}, {'answer_id': 74515, 'author': 'user8806', 'author_id': 8806, 'author_profile': 'https://Stackoverflow.com/users/8806', 'pm_score': 0, 'selected': False, 'text': '<p>I have all the private fields, then the public, then the constructors, then the main, then the methods that main calls, in the order they are called.</p>\n'}]
['https://Stackoverflow.com/questions/73045', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/423836/']