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.
...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
4
votes
Accepted
Set id in constructor, or generate it when asked for?
I would go with lazy loading. Generate the id when requested
...
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 ...
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 ...
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 ...
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, ...
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 ...
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 ...
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 ...
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 ...
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:
...
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.
...
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 ...
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 ...
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.
...
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 <...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
constructor × 152c++ × 34
object-oriented × 34
java × 31
c# × 25
javascript × 15
python × 14
php × 14
beginner × 11
c++11 × 9
array × 7
inheritance × 7
python-3.x × 5
memory-management × 5
ruby × 4
datetime × 4
unit-testing × 4
validation × 4
classes × 4
dependency-injection × 4
design-patterns × 3
mysql × 3
reinventing-the-wheel × 3
comparative-review × 3
error-handling × 3