What is the best way to organize the code?

I'm not talking about how to retreat here. I am looking for suggestions on the best way to organize code snippets in the source file.

Do you arrange methods alphabetically? In the order in which you wrote them? Thematically? In some kind of "didactic" order?

What organizational principles do you follow? Why?

+6
language-agnostic
source share
11 answers

i are usually ordered as follows

  • Constructors
  • destructors
  • earners
  • setters
  • any "magic" methods.
  • methods for changing the persisting state of the receiver (save (), etc.)
  • conduct
  • public helper methods
  • private / protected helper methods
  • anything else (although if there is something else, this is usually a sign that some refactoring is needed)
+5
source share

I am using the following pattern:

  • public static final variables
  • static functions, static blocks
  • variables
  • constructors
  • that do something related to logic
  • getters and setters (in most cases uninteresting, so there is no need to read them).

I don't have a scheme for including local classes, and basically I put them on top of the first method that uses them.

I do not like separation methods depending on access level. If any public method uses a private method, they will be close to each other.

+3
source share

I tend to group methods that relate to each other. Using a good IDE resolves most of this problem. Alphabetic methods seem like a waste of effort for me.

+1
source share

I group them based on what they are doing there, and then in the order I wrote them (in alphabetical order, it will be better)

for example in texture.cpp I have:

//====(DE)CONSTRUCTOR==== ... //====LOAD FUNCTIONS==== ... //====SAVE FUNCTIONS==== ... //====RESOURCE MANGEMENT FUNCTIONS==== //(preventing multiple copies being loaded etc) ... //====UTILL FUNCTIONS==== //getting texture details, etc ... //====OVERLOADED OPERTORS==== .... 
0
source share

Use this approach to a large extent for everything that I code. Good structure and well-commented code makes a good read.

  • Global variables
  • Functions
  • Primary organ / method
0
source share

public, protected, and then private and inside each section in alphabetical order, although I often list the constructor first and the last deconstructor.

/ Allan

0
source share

I tend to group things thematically for lack of a better word.

For example, if I had an open method that used two private methods during the execution of its work, I would group all three together in the implementation file, since the probability is good, if you are going to look at one of them then you will need to look at one from others.

I also always group get / set methods for a specific member of the class.

This is truly a personal preference, especially with modern IDEs, as there are many features that allow you to automatically jump to places in the code.

0
source share

I like to keep things simple, so I don’t find a bunch of methods in the class. In a class, I usually use the most commonly used (or modified by me) methods) listed first. As for the specific organization of the code, each set of methods belongs to a class, so it organizes itself.

I use the search function in the editor and code folding to navigate the large source files. In the same way, I use search functions to find things in other contexts. The grand organization scheme never suited me, so I rely on the power of searching in all things, not just the code.

0
source share

An interesting point. I didn’t even think about it.

I prefer to add frequently used functions at the top (utility functions, etc.), since they most likely need to be configured.

I don’t think organization is especially important since I can quickly find any function. I am not browsing my file to find a function; I am looking for him.

In C ++, I expect the functions in the .cpp file to be in the same order in which they are declared in the .h file. Usually these are constructors, followed by destructors, followed by basic / central functional functions, and then utility functions.

0
source share

I mainly write C code, and I try to sort by dependency. If possible, I try to match my source file with my header files, but this is usually the case if void a () uses int b (char * foo) before typing int b (char * foo).

Retains the ability to add entries to the header file for local functions.

Otherwise, it is mostly alphabetic, simplifies the search.

0
source share

I have all the private fields, then the publication, then the constructors, and then the main, and then the methods, which are mainly called in the order in which they are called.

0
source share

All Articles