71 votes

What's the correct way to do pair programming?

If pair programming makes you feel like you're an air traffic controller trying to talk down an air plane piloted by a fidgety 12 year old you're doing it wrong. The reason that's wrong is because you ...
candied_orange's user avatar
66 votes
Accepted

Is setRGBColor(32,128,192) a valid use of magic number?

Any kind of GUI framework I have seen which uses RGB colors has a datatype for that. So I would recommend to define a constant like RGBColor DARKER_BLUE(32,128,192); and use it accordingly label-&...
Doc Brown's user avatar
  • 203k
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
  • 4,724
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
54 votes
Accepted

Which language's style guidelines should be used when writing code that is supposed to be called from another language?

In general: when you write some library in language 1, you use the conventions from language 1, and use an alias mechanism to provide an API to map the function names to the case conventions of ...
Doc Brown's user avatar
  • 203k
49 votes

How do you manage your own comments on a foreign codebase?

Simply get into the habit of writing good comments - short, neutral, objective and non-redundant information about why a bit of code is doing what it's doing - and then the authors will probably be ...
Kilian Foth's user avatar
33 votes
Accepted

How to avoid repeating "a==b" when comparing "condition a" and then "condition b" and then...?

Is there any design pattern NO. design patterns are solutions on a higher level of abstraction, often language agnostic. This question, however, is about programming idioms. Is there any method to ...
Doc Brown's user avatar
  • 203k
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,561
26 votes

How to avoid repeating "a==b" when comparing "condition a" and then "condition b" and then...?

Using the Comparator API, it is very much possible to implement the problem using very little repetition. If you don't need bestIndex, you can the use Streams to find the maximum or minimum element. ...
corvus_192'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
  • 47.6k
23 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
19 votes

How do you manage your own comments on a foreign codebase?

Use a junk drawer. It is perfectly ok to mess with code locally and never push your changes to anyone else. I’ve done this for years. There is nothing special about code that means you can’t keep ...
candied_orange's user avatar
19 votes

What's the correct way to do pair programming?

The common pattern is that more senior devs generally end up handholding more junior devs throughout the whole process First of all, pair programming does not inherently entail tutoring or mentoring. ...
Flater's user avatar
  • 47.6k
19 votes

Is setRGBColor(32,128,192) a valid use of magic number?

Your change gives you zero improvement. Anyone working with RGB colors knows what the numbers mean. Something small means the color component is almost absent, 255 means the color component is as ...
gnasher729's user avatar
  • 43.4k
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
  • 203k
14 votes

When are try/exceptions not an anti-pattern?

Alternative perspective - focus on the exceptions and what they represent to the exception's source, rather than try..except. Eric Lippert in his Blog describes 4 broad categories of Exceptions, ...
Ben Cottrell's user avatar
  • 11.7k
13 votes
Accepted

Dealing with global variables required by badly-written library

Assuming that you cannot change the library, and you have to use it, you need to have these global variables somewhere in your code. I’d recommend implementing an interface module that the rest of ...
gnasher729's user avatar
  • 43.4k
12 votes
Accepted

Is "avoid misuse in other languages" a valid reason to avoid myString=="abc" in c++?

By the reasoning that similar syntax behaves differently in C++ and Java, you should also forbid the use of new in both your C++ and your Java code. Because you might just call new in C++ and forget ...
Bart van Ingen Schenau's user avatar
12 votes

How to avoid repeating "a==b" when comparing "condition a" and then "condition b" and then...?

Doc Brown's guidance is very good (as usual) and I don't want to reiterate what he wrote. However, as a basic programming skill, you should understand how to reduce such a statement. As others have ...
JimmyJames's user avatar
  • 25.9k
11 votes

How do you manage your own comments on a foreign codebase?

(All assuming you're using git; most of this should have parallels in any other reputable modern VCS) I once wrote a git pre-commit hook to fail if it found the text NO-COMMIT in the modified files. ...
MattPutnam's user avatar
11 votes

Which language's style guidelines should be used when writing code that is supposed to be called from another language?

I believe you should follow the conventions of the language you're writing in, with few exceptions, for these reasons: You say that the function is "supposed to be called from language 2", ...
Corrodias's user avatar
  • 219
10 votes

When are try/exceptions not an anti-pattern?

Try/except is not an antipattern, although it is probably an antipattern to turn an error condition into a boolean. But this happens in both examples, so that is a separate question. Both status codes ...
JacquesB's user avatar
  • 57.7k
9 votes

Which language's style guidelines should be used when writing code that is supposed to be called from another language?

There is no definitive answer. Coding style is subjective. However, I think it would be a sane general rule, that when you choose a coding style, you choose it for the readers of your code. Who would ...
Jo Totland's user avatar
9 votes
Accepted

Should I separate special cases from for loop deliberately, or let a for loop handles special cases naturally?

You should write code that is easily understandable. Do simple things, not tricky things. Use existing functionality. For example, your particular use case is let lastSelectedIndex = OPTION_ARRAY....
Sebastian Redl's user avatar
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
8 votes

Is setRGBColor(32,128,192) a valid use of magic number?

The issue is not with a magic number, the issue is with a magic value. You're fixating on the numeric types but that's not the core issue here. Focus on RGB(32,128,192) instead of the individual ...
Flater's user avatar
  • 47.6k
7 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
6 votes

How do you manage your own comments on a foreign codebase?

Many modern IDEs have a concept of bookmarks or similar, where you can annotate code / lines with extra information without altering the source code. That being said, if the codebase you downloaded is ...
Christian's user avatar
  • 200
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

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