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
    Object-Oriented Programming in WordPress: ScopeWordpress oop
    In continuing our discussion of object-oriented programming in WordPress, we need to begin talking about the idea of scope. In short, this refers to the idea as to how classes can control how their attributes and functions are accessed (or whether or not they can even be accessed). This is yet another core idea of object-oriented programming after which we should be in good shape to begin working on an actual WordPress plugin.Read More…
  • Code
    Object-Oriented Programming in WordPress: Functions and AttributesWordpress oop
    As we continue our discussion of object-oriented programming within the context of WordPress, it's time for us to begin talking about actual features of object-oriented programming. Though we've already covered classes, we needed to explore some foundational programming constructs before coming back around to some of the core object-oriented concepts. In this article, we're going to begin talking about functions and attributes: two foundational aspects of object-oriented programming.Read More…
  • 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: 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…