Skip to main content
New
Stack Overflow Jobs powered by Indeed: A job site that puts thousands of tech jobs at your fingertips (U.S. only). Search jobs
140 votes

Before OOP, were data structure members left public?

OOP did not invent encapsulation and is not synonymous with encapsulation. Many OOP languages do not have C++/Java style access modifiers. Many non-OOP languages have various techniques available to ...
amon's user avatar
  • 134k
111 votes

Why do so many standards for JSON API response formats contain a "success" property in the response body instead of just using HTTP status codes?

Many people take HTTP status code as “successful communication with the server”. Now if a customer wants to buy a US$200 item and has only US$100 in their account, the JSON response will be “failure, ...
gnasher729's user avatar
  • 45.7k
94 votes
Accepted

Why is using MySQL for a dictionary website a bad idea?

I can't tell you why it's a bad idea. I can tell you a bunch of reasons why a relational database is a good idea though. Remember that not everyone consults a dictionary for a definition. More times ...
Greg Burghardt's user avatar
85 votes
Accepted

How should I store "unknown" and "missing" values in a variable, while still retaining the difference between "unknown" and "missing"?

The common way to do this, at least with functional languages is to use a discriminated union. This is then a value that is one of a valid int, a value that denotes "missing" or a value that denotes "...
David Arno's user avatar
  • 39.3k
67 votes

Why do many functions that return structures in C, actually return pointers to structures?

There are several practical reasons why functions like fopen return pointers to instead of instances of struct types: You want to hide the representation of the struct type from the user; You're ...
John Bode's user avatar
  • 10.8k
65 votes
Accepted

Why are data classes considered a code smell?

There is absolutely nothing wrong with having pure data objects. The author of the piece quite frankly doesn't know what he's talking about. Such thinking stems from an old, failed, idea that "true ...
David Arno's user avatar
  • 39.3k
59 votes
Accepted

What is the most efficient way to store a numeric range?

Just count the number of possible ranges. There are 256 ranges with lower bound 0 (0-0, 0-1, ... 0-254, 0-255), 255 ranges with lower bound 1, ... and finally 1 range with lower bound 255 (255-255). ...
Glorfindel's user avatar
  • 3,147
58 votes

How should I store "unknown" and "missing" values in a variable, while still retaining the difference between "unknown" and "missing"?

If you do not already know what a monad is, today would be a great day to learn. I have a gentle introduction for OO programmers here: https://ericlippert.com/2013/02/21/monads-part-one/ Your ...
Eric Lippert's user avatar
  • 46.2k
58 votes
Accepted

Why do so many standards for JSON API response formats contain a "success" property in the response body instead of just using HTTP status codes?

A few potential reasons why you may wish to do this are: the fact that some HTTP clients treat anything other than 2xx as an "exception" to be thrown, which can hide differences between ...
AIWalker's user avatar
  • 1,307
43 votes

Can a C struct behave like it had a function?

Structs can hold function pointers, but those are really only needed for virtual methods. Non-virtual methods in object-oriented C are usually done by passing the struct as the first argument to a ...
Karl Bielefeldt's user avatar
40 votes

Why do many functions that return structures in C, actually return pointers to structures?

There are two ways of "returning a structure." You can return a copy of the data, or you can return a reference (pointer) to it. It's generally preferred to return (and pass around in general) a ...
Mason Wheeler's user avatar
37 votes
Accepted

What difference is there between using a struct and a std::pair?

Choice 1 is ok for small "used only once" things. Essentially std::pair is still a struct. As stated by this comment choice 1 will lead to really ugly code somewhere down the rabbit hole like thing....
risingDarkness's user avatar
37 votes
Accepted

How to represent a set in JSON?

Well, you can't. As you said, you can represent arrays and dictionaries. You have two choices. Represent the set as an array. Advantage: Converting from set to array and back is usually easy. ...
gnasher729's user avatar
  • 45.7k
36 votes

How is polymorphism used in the real world?

Stream is great example of polymorphism. Stream represents a "sequence of bytes that can be read or written". But this sequence can come from file, memory, or many kinds of network connections. Or it ...
Euphoric's user avatar
  • 37.4k
34 votes

Is there a common term for a fixed-length, fifo, "push through" array or list?

This sounds a lot like a circular buffer or ring buffer. It meets most of the criteria: It has a fixed size of items. They don't have to be a specific value at the start, but they could be ...
Thomas Owens's user avatar
  • 82.8k
33 votes

Methods to increase the amount of data sent in a packet

The main method to reduce your packet size is to use a binary encoding of your data. If your sensors produce the data in a binary format, note the specifications of the format, such as the number of ...
Bart van Ingen Schenau's user avatar
31 votes

Before OOP, were data structure members left public?

First, being procedural versus object oriented has nothing to do with public vs private. Plenty of object oriented languages have no notion of access control. Secondly, in "C" - which most people ...
Lewis Pringle's user avatar
29 votes
Accepted

Is it good practice to wrap a related set of properties into its own struct/class?

You will either need zero of a thing, one of a thing, or an arbitrary number of the thing. I'm shocked that your design predicts that the number of needed links will always be three and that you ...
candied_orange's user avatar
27 votes

Why is using MySQL for a dictionary website a bad idea?

If you go with the key-value store (which offers you a more impoverished programming model) and it turns out you need more structure (in your case, say, adding a third language), or you need to do ...
Erik Eidt's user avatar
  • 34.1k
23 votes
Accepted

Is there a tree data structure with multiple root nodes?

You are looking for a kind of Directed Acyclic Graph (DAG). These graphs do not have a root node. However, the nodes have a partial order. I.e. when we look at two nodes we can sometimes tell which ...
amon's user avatar
  • 134k
22 votes
Accepted

Can a C struct behave like it had a function?

Your function should look like this. void replace(struct string * s, int i, char c); This accepts a pointer to the object to operate on as the first parameter. In C++, this is known as the this-...
5gon12eder's user avatar
  • 7,196
21 votes

EAV - is it really bad in all scenarios?

6 years later Now that JSON in Postgres is here, we have another option, for those who are using Postgres. If you only want to attach some extra data to a product, then your needs are fairly simple. ...
Dan Ross's user avatar
  • 369
20 votes

Is there a common term for a fixed-length, fifo, "push through" array or list?

Like the other answer suggests, a fixed-length fifo queue is a ring buffer, although usually writing past the capacity will be prevented, instead of overwriting the next element. in fact, it's not ...
Bergi's user avatar
  • 1,333
20 votes

How to ensure data consistency in system with multiple databases?

There are no easy solutions. Distributed consistency is fundamentally difficult, with some hard limits to what is possible. See the CAP theorem and Fallacies of distributed computing. And doing ...
JonasH's user avatar
  • 5,504
19 votes
Accepted

What makes some things easier to parse than others?

This topic is very complex. You can google for parser algorithms and you'll get plenty of detailed material. In general: The fewer ambiguities must be resolved, the faster the parsing process. The ...
marstato's user avatar
  • 4,608
19 votes
Accepted

Is storing a list of strings in single database field a bad idea? Why?

The datamodel isn't normalised; to be so it would need a separate table as you say. In that regard, it's not particularly good datamodelling practice. Whether it was done for a good reason or not is ...
Alex's user avatar
  • 3,912
18 votes
Accepted

Why do we need stacks and queues?

Stacks and queues are ways of working with contiguous memory. Linked lists are not. Now sure, any problem that you can solve with one structure you could solve with another and slap enough ...
candied_orange's user avatar
18 votes

What difference is there between using a struct and a std::pair?

Performance: It depends. In your particular case there will be no performance difference because the two will be similarly laid out in memory. In a very specific case (if you were using an empty ...
Matthieu M.'s user avatar
18 votes

How should I store "unknown" and "missing" values in a variable, while still retaining the difference between "unknown" and "missing"?

I think that in this case a variation on a Null Object Pattern would be useful: public class Measurement { private int value; private bool isUnknown = false; private bool isMissing = ...
Maciej Stachowski's user avatar
18 votes

Why do so many standards for JSON API response formats contain a "success" property in the response body instead of just using HTTP status codes?

There are inherent shortcomings in trying to fit a nuanced, complete API into the limitations of HTTP. The above examples provide some good points to why that's the case. Here's another scenario we ...
user412514's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible