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 ...
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-&...
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:
...
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 ...
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 ...
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 ...
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
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.
...
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 ...
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 ...
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 ...
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. ...
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 ...
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 ...
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, ...
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 ...
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 ...
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 ...
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. ...
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", ...
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 ...
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 ...
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....
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.
...
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 ...
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 ...
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 ...
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
>&...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
coding-style × 1050coding-standards × 148
java × 103
c++ × 98
code-quality × 93
python × 76
programming-practices × 76
c# × 73
clean-code × 69
design × 53
c × 53
object-oriented × 45
javascript × 45
naming × 45
readability × 37
php × 27
design-patterns × 26
language-agnostic × 24
comments × 22
conditions × 19
refactoring × 18
code-reviews × 18
architecture × 17
exceptions × 16
functions × 16