Skip to main content
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 ...
J_H's user avatar
  • 7,620
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: ...
Oscar Cunningham's user avatar
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 ...
candied_orange's user avatar
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 ...
mikequentel's user avatar
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 ...
Doc Brown's user avatar
  • 212k
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 ...
Simon B's user avatar
  • 9,646
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 ...
Greg Burghardt's user avatar
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 ...
Brian61354270's user avatar
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 ...
Toby Speight's user avatar
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 ...
Flater's user avatar
  • 54.9k
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, &...
candied_orange's user avatar
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.
gnasher729's user avatar
  • 46.7k
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 ...
Doc Brown's user avatar
  • 212k
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 ...
DavidT's user avatar
  • 3,629
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 ...
Christophe's user avatar
  • 79.9k
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 ...
Bergi's user avatar
  • 1,337
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 ...
DavidT's user avatar
  • 3,629
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 ...
Basilevs's user avatar
  • 2,895
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. ...
Mooing Duck's user avatar
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 ...
Doc Brown's user avatar
  • 212k
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 ...
Greg Burghardt's user avatar
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 ...
Bart van Ingen Schenau's user avatar
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 ...
Kilian Foth's user avatar
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....
Peter - Reinstate Monica's user avatar
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 ...
J_H's user avatar
  • 7,620
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 ...
Doc Brown's user avatar
  • 212k
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 >&...
Martin Vilcans's user avatar
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 ...
candied_orange's user avatar
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 ...
Telastyn's user avatar
  • 110k
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 ...
Doc Brown's user avatar
  • 212k

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