Open Mike

Open Mike: Code Organization


Do you order your functions and variables by their purpose, or by an unchanging set of rules? This is Open Mike, a series of discussion posts to throw the cat amongst the pigeons. These posts are all about you — we want to hear your opinions, ideas, and thoughts. This one’s as straightforward as they come; one question, one poll, but a potentially huge discussion. Let’s hear what you have to say about code organization.

There are two approaches to ordering the properties and methods in your classes:

The Methodological Approach

You could have a strict set of rules, like:

  • Imports are arranged alphabetically.
  • Public consts come immediately after the class name, then private consts.
  • After consts come variables: public, protected, private, then internal; all arranged alphabetically.
  • Constructor function comes after the variables.
  • Public functions come after the constructor, then protected, private, and internal; again, all in alphabetical order.

These rules don't take into account what any of the variables or functions do; they just provide you with a simple way to make sure that everything's in its place. You can easily find any code you're looking for, though a decent code editor would make that easy anyway.

The Functional Approach

On the other side of the spectrum, you could group your functions and variables by their purpose. Perhaps all the code related to initializing a class goes at the top, while the code for deconstructing it goes near the bottom.

This makes code easier to read through, though it can be harder to find a specific function in a rush. Some would argue that this is unnecessary, because a class should only contain the code required for a single purpose anyway.

What's Your Approach?

So, how do you arrange your code? Methodically, functionally, or some mix of the two? Take part in our Facebook poll and let us know :)

Related Posts
  • Code
    Mobile Development
    C++ Succinctly: C++ Language Usages and IdiomsPreview image@2x
    We’ve already covered the RAII idiom earlier in this series. Some language usages and programming idioms in C++ might seem foreign or pointless at first glance, but they do have a purpose. In this article, we will explore a few of these odd usages and idioms to understand where they came from and why they are used.Read More…
  • Code
    Mobile Development
    C++ Succinctly: Casting in C++Preview image@2x
    There are five different ways of casting variables. There is overlap between them, especially with the C-style cast and all other casts, but each has its use. It is good to learn them all, so you can use the best cast for your particular need rather than using any cast that happens to work.Read More…
  • Code
    Mobile Development
    C++ Succinctly: Resources Acquisition Is InitializationPreview image@2x
    This article is all about RAII or Resource Acquisition Is Initialization, a design pattern using C++ code to eliminate resource leaks.Read More…
  • Code
    Mobile Development
    C++ Succinctly: Constructors, Destructors, and OperatorsPreview image@2x
    In this article, we discuss constructors, destructors, and operators in C++. Constructors are a bit more complex in C++ than they are in C#, but, after reading this article, you should have a good understanding of C++ constructors.Read More…
  • Code
    Mobile Development
    C++ Succinctly: Functions and ClassesPreview image@2x
    In this article, we discuss functions and classes in C++. You'll learn the difference between declaration and definition and we'll take a look at inheritance, abstract classes, and precompiled header files.Read More…
  • Code
    iOS SDK
    Objective-C Succinctly: Methods0e5ds8 preview image@2x
    In this chapter, we'll explore Objective-C methods in much more detail than we have in previous chapters. This includes an in-depth discussion of instance methods, class methods, important built-in methods, inheritance, naming conventions, and common design patterns.Read More…