Skip to main content
12 votes

Clearing up property and field confusion in C#?

There is a special use case when the (slightly modified) second approach could be beneficial. Namely when you want to create immutable objects. ...
Peter Csala's user avatar
  • 10.8k
9 votes
Accepted

Classes representing 2D points and pixels

A fine example how modern C# can make your life so much easier! But first things first: Errors and maybe errors Your GetHashCode() is realy weak! Just take two example points (1|1) and (0|32) and ...
Matthias's user avatar
  • 106
7 votes
Accepted

OOP modelling of a car agency

(Since you're still a student, I might go over the top with some explanations.) Code Style Always format your code. Always. (CTRL+SHIFT+F in eclipse and CTRL+ALT+L in intellij). There's also the ...
slowy's user avatar
  • 1,916
6 votes
Accepted

Implementing a c++ Kafka producer based on librdkafka

It would be more useful (and typical) for CodeReview if you'd post the entire code for the class, starting with the class definition and then the method definitions, rather than starting with one of ...
Quuxplusone's user avatar
  • 19.7k
5 votes
Accepted

Constructor function for persons with hobbies

Performance Strings are immutable, so using accumulator += stuffToAppend in a loop can traditionally impact performance. The problem is that we're creating a new ...
ggorlen's user avatar
  • 4,167
5 votes
Accepted

C++ wrapper class for array of OpenGL buffer objects -- just the constructors

Some specific questions I have about this code/approach are, if applicable, OK. Error handling design decision: should the constructors throw, or should I leave error checking, if desired, to the ...
Loki Astari's user avatar
  • 97.7k
4 votes
Accepted

Request dispatcher for PHP JSON web service

Now, to my best knowledge, $this->request in PHP always points to the HTTP-request when a HTTP-request has just been made My guess is that you are thinking of the super-global ...
Sᴀᴍ Onᴇᴌᴀ's user avatar
4 votes
Accepted

Testing async method call from constructor

Trying to load the data asynchronously in the constructor is a bad idea and in my opinion goes against what the constructor is for. Constructors cannot be async, and asynchronous initialization can ...
Nkosi's user avatar
  • 3,296
4 votes

Character class with name, gender, and inventory list

This is a very straightforward implementation. Not much to improve. Especially copying the list passed in from the caller is a good safety measure, as it makes sure that, if the caller later modifies ...
Ralf Kleberhoff's user avatar
4 votes
Accepted

Representing destinations in a square

Tell your instructor to become familiar with the Standard Guidelines. You can find presentations on youtube where Stroustrup introduces this as a keynote speech, and many others speaking on them ...
JDługosz's user avatar
  • 11.7k
4 votes
Accepted

C++ - Astar search algorithm using user defined class

A std::vector<std::vector<int>> is an inefficient way to store a 2-dimensional array. Unfortunately, there is nothing in the standard C++ library that ...
G. Sliepen's user avatar
  • 69.2k
4 votes

Implementing a c++ Kafka producer based on librdkafka

I have already discussed this in a comment below Quuxplusone's answer, but I thought I would make this up into an supplementary answer. The constructor will leak ...
AlexV's user avatar
  • 7,363
4 votes

Super fancy list constructing in Python

I'm severely confused by array.move_down, because I don't see any array or anything moving anywhere. If you are doing a ...
kubanczyk's user avatar
  • 269
4 votes
Accepted

Particle Swarm Optimization algorithm

As a general rule, if the only thing your getters and setters do is contain a single statement, a return or assignment respectively, there is no need to make the variable private and it should be ...
Casey's user avatar
  • 778
4 votes
Accepted

Set id in constructor, or generate it when asked for?

I would go with lazy loading. Generate the id when requested ...
Marius's user avatar
  • 186
4 votes

Clearing up property and field confusion in C#?

The refactor with an explicit constructor is a drop-in replacement for the original code and would be the direction I would go. The code requiring object initialization requires a change to how ...
Wes H's user avatar
  • 373
4 votes
Accepted

Performing complex operation before calling the primary constructor in Kotlin

Here are two more options: Write a factory function that has the same name as the class. They do this a lot in the standard library, although mostly for interfaces. The default lint rules warn about ...
Tenfour04's user avatar
  • 823
4 votes
Accepted

A simple String class and its special member functions

Answer to your question is it true that in theory and in practice that when using move constructor and move assignment operator, the move-from object shall be invalid. No, the moved-from object must ...
G. Sliepen's user avatar
  • 69.2k
4 votes

A simple String class and its special member functions

Don't using namespace std;, as that can be harmful. Instead, write out std::uint32_t, ...
Toby Speight's user avatar
  • 88.3k
4 votes
Accepted

Using std::variant for constructor

It's not a good idea The problem of using a std::variant for the constructor is that it removes flexibility and forward-compatibility from your API. Consider adding ...
G. Sliepen's user avatar
  • 69.2k
3 votes

Healthy Characters

Instead of questioning whether it is a good practice, it would be more helpful to be aware of what your first code actually does. JLS §15.9.4 has this to say about the creation of an object: The new ...
Stingy's user avatar
  • 2,168
3 votes
Accepted

Simple addition calculator accepting addition expressions

RegExp to the rescue. First there is the problem for the review. The function is inconsistent and that makes it hard to know what it really must do. Consider the test below if given the following ...
Blindman67's user avatar
  • 22.9k
3 votes

Simple addition calculator accepting addition expressions

Instance method vs. class method vs. function: You declared a constructor function Calculator whose instances all come with their own individual ...
le_m's user avatar
  • 1,945
3 votes

Request dispatcher for PHP JSON web service

I don't recommend emitting these headers so early, and also not as a side-effect of a constructor: ...
200_success's user avatar
3 votes

C++ wrapper class to mimic a C array's brace initialization

I think you should make your interface constexpr Did you expect negative index? If not, try to use std::size_t instead of int. ...
Calak's user avatar
  • 2,411
3 votes

Represent a Class which holds translation Strings

You haven't given much context about how you intend to use this class, which is always important. In my experience (echoed in comments), there's not much use case for needing all translations of a ...
Pierre Menard's user avatar
3 votes
Accepted

Constructor for a packagetarget struct

packagetarget_close() First of all, you have a copy-and-paste error in packagetarget_close(), where you attempt to free ...
200_success's user avatar
3 votes
Accepted

Time interval class that enhances constructor argument handling of its parent class

If you don’t expect to be further subclassing MyInterval, you could just use a helper method for construction. ...
AJNeufeld's user avatar
  • 35.3k
3 votes
Accepted

Commands Object creation using chained Builder pattern

Is it more efficient for the user of this API? It looks nice for a single insert operation, but what if I need to insert many things, all with the same options? I would not want to have to repeat the <...
G. Sliepen's user avatar
  • 69.2k
3 votes

Performing complex operation before calling the primary constructor in Kotlin

The reason Kotlin enforces primary constructor to be called as the first thing, is to ensure that constructors don't hide lot of logic before the construction of the object. So where do the complex ...
Amit's user avatar
  • 133

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