35
votes
Calculate evapotranspiration
I wonder if there is a need for a class here? It seems class Hargreaves has no responsibilities and is solely used to compute the evapotranspiration value based on ...
24
votes
Accepted
Simple Container Class
Advice for learning
Don’t do this.
The container you are trying to implement is std::vector. You don’t call it that, and you never say explicitly that ...
17
votes
Accepted
TString - An implementation of std::string
Justify text. (.rjust, .ljust, center)
That is becoming a "fat interface". These functions should be generic algorithms that can work with any string class, including yours, ...
16
votes
ReversibleList: a list that is "reversed" in O(1) time
Bug
You have a bug!
a[2:5] = ReversibleList([1, 2, 3])
print(a) # [5, 4, 3, 2, 1, 0]
ReversibleList([1, 2, 3]) is constructing ...
16
votes
ReversibleList: a list that is "reversed" in O(1) time
I don't really like the direction toggling stuff. Mostly on the grounds of disliking state if possible. You could end up with the list pointing in the opposite direction to the one intended.
What I ...
12
votes
Accepted
Setters dependent on other instance variables in Java
Allowed?
Yes. There seems to be nothing technically wrong with your code.
Good practice?
No.
Specifically, in your example code setPddocument is not actually a ...
11
votes
High performance physics simulation - core class design
Missing #includes
You are using std::vector, but did not #include <vector>. The code ...
10
votes
Date class in C++
Not bulletproof
Maximum amount of days in a month depends on both month and a year. It doesn't seem like the class cares about it. The rest of the answer might not be interesting to you.
Operations ...
10
votes
Calculate evapotranspiration
To add to Peter's excellent answer, please also consider the likely audience of your code. I do not know who Hargreaves is, (or even that it's a name, looking only at the source), and practically ...
10
votes
Using MergeSort to sort data structured in Person class based on object attributes (DRY)
You can reduce the amount of code:
Throw away get_ methods, just use .name not .get_name()
...
10
votes
A simple Python class designed to facilitate investment portfolio analysis
time representation
class Portfolio:
...
start_date: str
end_date: str
I'm sad those aren't
datetimes,
or at least
dates.
Then we get validation (no ...
9
votes
Accepted
Generic Doubly Linked-list implementation in C++
Identifiers that start with a double underscore, or a single underscore followed by a capital letter, are reserved for use by the implementation. Your use of __T ...
9
votes
Accepted
Creates class and makes animals, then print bios
Thank you for sharing.
There's some good things going on in this code.
constructor
...
9
votes
Accepted
Project Euler #54: class for poker hands
I encourage you to read the other answers, they have good advice. I'll focus on using classes.
What is a type?
That's a fairly abstract question, I recognize, but it's a crucial part of the ...
8
votes
Accepted
Simple Multiclass C++ Project
Don't use std::endl
Use "\n" instead of std::endl. The latter is equivalent to <...
8
votes
Accepted
File editing class
"<object_name> = EditFile(<file_name>)"
is not a particularly useful __repr__. Note the guidance in the data model ...
8
votes
Accepted
Snail matrix in Java
Expose behavior / Don't manipulate the internal state directly
Object-orientation is about exposing behavior (not data), defining methods that are tailored for the problem at hand, that are meant to ...
8
votes
What is the best way to set a class value to a variable in Python if it exists in a dictionary?
Correctness
remote_str is defined at wrong indentation level, it will fail with an UnboundLocalError for ...
8
votes
Creates class and makes animals, then print bios
DRY
There is a lot of repeated code in the bio function.
You repeat the age check twice and the print 4 times with few ...
8
votes
Creates class and makes animals, then print bios
In addition to what the others have said, it would make your class more flexible if Animal.bio returns a string rather than printing one itself.
You can still:
<...
8
votes
Project Euler #54: class for poker hands
Naming
Most of the functions and variables have descriptive, meaningful names.
However, the function vals_and_suits is a little vague. It looks like
it sets values,...
7
votes
Date class in C++
Creating a proper date class is actually fairly complicated, and depends how far you're willing to go to make an interface that adheres to the pit of success principle.
As Scott Meyers warns, ...
7
votes
Accepted
chess game Class
General Observations
Unlike some more modern languages such as C# and Java, a C++ class is generally implemented as 2 files: a header file generally with the file extension ...
7
votes
Accepted
Address class in PHP 7.4 with types
I saw your post yesterday before it was deleted. I didn’t have much to suggest but after seeing this code there are a few things I would change.
The suggestions by YourCommonSense are great:
Specific ...
7
votes
Classes for the blog (PHP)
I really hope that you are trolling us, but if you are not trolling and this is a real code, then I must say that it should absolutely never be used. This might have been acceptable 25 years ago, but ...
7
votes
Calculate evapotranspiration
As someone who has to read/write science code, please please include units in your constants, and in the output from your function. They are in the equations, but not the code.
I still wish that we ...
7
votes
Accepted
PHP HTML form Class - so far so good?
I have not conducted a deep review of your scripting, but I have a few pieces of superficial advice:
Study PSR-12 coding standards and obey every rule. This will make your code easier for you and ...
7
votes
Accepted
What is the best way to set a class value to a variable in Python if it exists in a dictionary?
tl;dr: It should be a
@dataclass.
code which sets values in a location class from values from JSON data.
The vocabulary word you're looking for is "object attribute".
The code sets ...
7
votes
Simple Container Class
Use std::copy instead
You don't need to implement CopyData. You can use std::copy to copy ...
7
votes
Creates class and makes animals, then print bios
The other answers are fairly complete, but one addition that might be helpful, assuming you don't plan on adding any other descriptions of the animal, would be the ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
classes × 444object-oriented × 136
php × 94
python × 92
c++ × 86
c# × 56
java × 51
beginner × 37
python-3.x × 27
javascript × 21
design-patterns × 20
performance × 16
game × 15
pdo × 15
playing-cards × 14
inheritance × 14
template × 13
android × 11
php5 × 11
ruby × 9
vba × 9
image × 9
tkinter × 8
static × 8
algorithm × 7