66
votes
Accepted
In C, if I am passing a file descriptor to a function, should I close it in the function or in the place where I called the function?
In most languages, including C, we usually produce the
most maintainable code by dealing with acquire / free
at the same level:
fd = open(...)
consume(fd);
close(fd);
This applies to ...
57
votes
Best practice for redundant conditions in if-elif-else statements
Even Case 1 can have unexpected behaviour. The value of n that worries me is NaN.
n = float('nan')
if n == 0:
doThis() # Not triggered
elif n < 0:
doThat() # Not triggered
elif n > 0:
...
53
votes
Is it logical to not use inheritance because the function is critical?
This is a “protect the idiots” rule. I’ve seen it in many forms. Once entrenched it’s very difficult to overturn. I’ve seen rules like this forbid code that is identical to code in the language ...
33
votes
How to be (more) critical during Code Reviews?
Look for anti-patterns.
Lint the code if it has not already been done.
If something looks really strange, talk to the developer before proceeding--maybe they are not ready to have their pull request ...
29
votes
Accepted
How to "Tell, don't ask" when 2 objects involves in the condition and the decision at the same time?
You wrote
I know the following is bad
and already here is a misconception: thinking religiously in terms of good and bad about this. My interpretation of Tell-Don't-Ask is: when you see a code ...
28
votes
Are "need to call objects in parent object" and "avoid circular dependency" reasons to avoid "Tell, don't ask"?
Ultimately, you're pushing all the program logic into class Manager. Everything else is only a dumb interface. As the program develops, Manager gets bigger and bigger, until it's an unmaintainable ...
26
votes
Is it logical to not use inheritance because the function is critical?
This is difficult to tell without analyzing the code. I can't provide you an authoritative answer, but perhaps some guidance on how to make this decision will do.
In a situation like this, the first ...
25
votes
Accepted
Using `any` to indicate a wildcard value
Instead of using a sentinel value like any or None with special-cased logic, you could create a type the implements the desired "everything goes" behavior using the same interface as the ...
25
votes
In C, if I am passing a file descriptor to a function, should I close it in the function or in the place where I called the function?
Open file descriptors, like allocated memory, are resources¹ which should be clearly owned by a block of code or by some other owned resource.
In C², we need to be absolutely explicit about the ...
24
votes
Accepted
Best practice for redundant conditions in if-elif-else statements
Short answer
I would argue that there is no singular correct answer to this question.
It depends on what you want to have happen when one of the above cases changes. Are you assuming that the last ...
20
votes
How do you honour the principle to only do "one thing" in a method in reactive streams?
What you're asking how to do is called decomposing. "Do one thing" never meant your function couldn't be decomposed into more things. Every function can be decomposed into more things. No, &...
17
votes
How do you honour the principle to only do "one thing" in a method in reactive streams?
“Do only one thing” may include “do everything to display the user’s information on the screen and allow editing it”.
Taking SRP too literal is a recipe for disaster.
16
votes
In C, if I am passing a file descriptor to a function, should I close it in the function or in the place where I called the function?
Currently, you have two answers giving you good advice in general. Nevertheless neither of them refers to your specific example, where pipe allocates two file descriptors, which are usually intended ...
15
votes
Is it logical to not use inheritance because the function is critical?
it's important for that function to be correct, so we want to ensure that the developer of a new sub-class goes through the steps to make sure they have the right logic.
I would question the core ...
14
votes
How to "Tell, don't ask" when 2 objects involves in the condition and the decision at the same time?
Tell don't ask is a principle aiming at guidance for effectively encapsulating internal details of objects. It therewith:
encourages better abstraction, by placing inside the object the logic that ...
13
votes
How to be (more) critical during Code Reviews?
A code review needs to find and point out any major flaws:
Does the code use the wrong approach to solve the problem? Do the design patterns fit?
Does the code actually solve the problem? Does it ...
10
votes
How to be (more) critical during Code Reviews?
I would suggest:
Have more than one person review a PR - you can compare the issues that you found to what someone else (preferably a senior engineer) found. Talk to the other reviewer to understand ...
10
votes
Accepted
How do you honour the principle to only do "one thing" in a method in reactive streams?
In general, do not follow any principles suggested without a rationale (and Uncle Bob is not rational).
To follow the principle blindly, you can introduce a layer of abstraction - a dispatcher.
It ...
9
votes
Are "need to call objects in parent object" and "avoid circular dependency" reasons to avoid "Tell, don't ask"?
The point is to reduce how much Manager knows. Also, SystemMoniter should only know its things. it shouldn't need to know about Leds, Speakers, or the Screen. And actually, neither should the Manager.
...
9
votes
Is it logical to not use inheritance because the function is critical?
As far as I can see this problem will not automatically be solved by switching from inheritance to composition. When I understand this correctly, in your system, each new subclass (or new module of a ...
8
votes
Common practice where to place "is" word while naming predicate function: at the beginning or in the middle?
This inconsistency is a combination of things:
Language designers are not required to adopt naming conventions from other languages when inventing a new programming language.
Naming conventions have ...
8
votes
When to prefer print over logging?
Printing and logging serve different purposes.
The purpose of logging tools is that they provide you with information after the fact about what a piece of software was doing. These tools are designed ...
8
votes
Is the inability to find code by searching for a class name a reason to avoid using auto in c++ variable declarations?
It's certainly a valid reason.
Whether that's enough of a reason to avoid auto in your situation - that depends on whether there are superior reasons for using it. Presumably you can find such ...
7
votes
How to be (more) critical during Code Reviews?
A few points that have not been covered yet:
Is it maintainable and extensible (e.g., uses interfaces, collects configuration values in a visible spot instead of scattering them);
Is it testable (e.g....
7
votes
Code quality: expressiveness vs. conciseness
contract
Variant1 is not terrific.
It offers poor maintainability, and likely
wouldn't get past Code Review unchanged.
int? checkLoginDay(int lastDay) {
We started with a verb, good. But what's the
...
7
votes
Accepted
Considering IDE and text editor features when choosing a coding style
"Type hints" are a very weak argument for "var", because they appear only when one places the mouse cursor over the var keyword. But when reading code, any professional developer ...
6
votes
Using `any` to indicate a wildcard value
I made the types Anything and Something for this purpose. Anything compares True with any other value, while Something compares True on anything that is not None:
>>> Anything == 42
True
>&...
6
votes
How to be (more) critical during Code Reviews?
How to be (more) critical during Code Reviews of team members?
Don't be critical of team members. A code review is about code. Not about who wrote it.
I'm active/highly involved with the code, but ...
6
votes
Is it logical to not use inheritance because the function is critical?
“Base class with a ton of sub-classes” is not typical… and many functions in the base class is even more troublesome.
Once you’re at that point, it is somewhat reasonable to fear that engineers will ...
6
votes
Is the inability to find code by searching for a class name a reason to avoid using auto in c++ variable declarations?
No, I don't think this is a valid reason to avoid all usage of auto in general. It may be a valid reason for certain cases (though both of your examples look somewhat debatable to me).
Let me first ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
coding-style × 1062coding-standards × 150
java × 105
c++ × 99
code-quality × 95
python × 76
programming-practices × 76
c# × 73
clean-code × 71
design × 55
c × 53
object-oriented × 46
naming × 46
javascript × 45
readability × 39
design-patterns × 27
php × 27
language-agnostic × 24
comments × 22
refactoring × 19
code-reviews × 19
conditions × 19
architecture × 18
exceptions × 17
functions × 17