-5

With few years of C programming, I noticed there are two major types of design choices for data organization. I offer an analogy below.

First:

  • height folder: Andy.txt, Bruce.txt, Chris.txt and Tina.txt

  • weight folder: Andy.txt, Bruce.txt, Chris.txt and Tina.txt

Second:

  • Andy folder: height.txt, weight.txt

  • Bruce folder: height.txt, weight.txt

  • Chris folder: height.txt, weight.txt

  • Tina folder: height.txt, weight.txt

If you were a hiring manager and someday Andy was gone, in first choice you need to delete Andy.txt two times.

In second choice, you only need one step to delete all info about Andy.

Back to software design, I intuitively believe second choice is the better one because this method achieves modularity.

But maybe the first one has its own place and I have not noticed yet.

What is this field of studying called? Is this related to software architecture? Is there a book discussing it? Is there an example code for first method written in C?

6
  • 2
    see What is the problem with "Pros and Cons"?
    – gnat
    Aug 11, 2020 at 10:41
  • 6
    What if one day, you don't need the info about weight anymore? With the first option, you can delete everything in one step, while in the second, you need to do it in four. So, you see, it depends :D Aug 11, 2020 at 14:16
  • 2
    See also: the expression problem. You have different types of expressions and also different things you can do with them. Some approaches make it easy to add new types (like having the types as folders); other approaches make it easy to add new operations (like having the operations as folders) but none do both simultaneously.
    – user253751
    Aug 11, 2020 at 14:20
  • 1
    These aren't really two different design philosophies; it's the same design philosophy (nested folders with files) but the labels just swapped around. There is no philosophical principle that distinguishes "weight" from "Andy" as a label.
    – trent
    Aug 11, 2020 at 17:35
  • 1
    @AndyLin - that excerpt is actually exactly about the expression problem; Bob Martin writes a bit more about it here. If you are interested in that topic, see this as well. Aug 12, 2020 at 14:33

1 Answer 1

7

No approach can be said to be "better" without the context for which it is chosen. If you have a problem to solve, be it a business requirement or whatever, you choose the approach that suits the problem.

In your example, the two approaches are either a folder per employee, or a folder per data type. The second, as you said, makes managing employees easier: when an employee leaves, simply delete the folder. But what if the problem you're trying to solve isn't an employee management solution, but to get statistical information about employees? If you need the average height of all employees, you'll need to go over every folder, open the height.txt in each one, and sum them up. For one problem, the first approach is simpler. For a different problem, the second is.

Row vs. Column storage

You can think of your two examples as row-based storage vs column-based storage. Given this table representing your data:

        Height    Weight
Andy     xxx       yyy
Bruce    xxx       yyy
Chris    xxx       yyy
Tina     xxx       yyy

Your second approach stores each row sequentially, making it easy to retrieve all fields for a given employee. The first stores each column sequentially, making it easy (for the DB, meaning fast for the user) to aggregate the information in that column.

You can find some data storage solutions, such as Apache Parquet, described as "columnar data stores". Traditional SQL dbs, like MySQL, are usually row-based (see here).

Choose the approach that fits the problem

Each approach has its uses. I gave examples from database storage products, but the same differences in approach can affect your own code and choice of architecture - do you store a dictionary of Employee objects and add/remove to that collection when you add a new user, or do you store a vector of EmployeeHeight? Depends on what you need it for. To evaluate the best one, don't think in terms of "what's the best approach", but "what approach solves the problem I'm trying to solve". And sometimes you'll find it makes sense to do both - yes, even if it means duplicating your storage, if it makes access faster and easier.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.