165
votes
Programming cleanly when writing scientific code
This is a pretty common problem for scientists. I've seen it a lot, and it always stems by the fact that programming is something you pick on the side as a tool to do your job.
So your scripts are a ...
146
votes
Programming cleanly when writing scientific code
Physicist here. Been there.
I would argue that your problem is not about the choice of tools or
programming paradigms (unit testing, OOP, whatever). It's about the
attitude, the mindset. The fact the ...
83
votes
Programming cleanly when writing scientific code
Version control is probably going to give you the most bang for your buck. It isn't only for long-term storage, it's great for tracking your short-term experimentation and going back to the last ...
68
votes
Accepted
Is using lambdas to express intent not pythonic?
You're sort of approaching it like a mathematician, where the purpose of writing the supporting functions is to "prove your work." Software isn't generally read that way. The goal is usually ...
34
votes
Accepted
Does subclassing int to forbid negative integers break Liskov Substitution Principle?
This is not an LSP violation, because the object is immutable and doesn't overload any instance methods in an incompatible way.
The Liskov Substitution Principle is fulfilled if any properties about ...
30
votes
Accepted
Is it reasonable to use dictionaries instead of arguments?
Does this coding pattern have a name?
This is a refactoring called "Introduce Parameter Object". A dictionary is used here as a "poor man's DTO". Note there are other, less error ...
29
votes
Programming cleanly when writing scientific code
(aside from using version control, and making a new file for each new iteration and recording all of it in a text file somewhere, which will probably help the situation dramatically)
You would ...
28
votes
Does subclassing int to forbid negative integers break Liskov Substitution Principle?
On a first glance, it looks like the LSP will be violated, since replacing an int object by an PositiveInteger in a function which expects just an int gives the impression it could break the function'...
27
votes
Is using lambdas to express intent not pythonic?
Despite the Zen of Python, there is sometimes more than one obvious way to do it.
I agree that your preferred way to phrase this code has a certain functional elegance to it.
But it's also plain to ...
23
votes
Is using lambdas to express intent not pythonic?
"Pythonic" is not an objective standard. It really means "code that an experienced python programmer likes". Turns out "experienced python programmers" don't all ...
21
votes
Programming cleanly when writing scientific code
In graduate school, I wrote some algorithm-heavy code myself. It's a bit of a tough nut to crack. To put it coarsely, a lot of programming conventions are built around the idea of putting information ...
17
votes
Are Python mixins an anti-pattern?
The linter is not aware that you use a class as a mixin. Pylint is aware that you use a mixin if you add the suffix 'mixin' or 'Mixin' at the end of the class name, then the linter stops complaining.
...
17
votes
Programming cleanly when writing scientific code
So, my day job is in research data publication and preservation for the University of California system. A couple of folks have mentioned reproducibility, and I think that's really the core issue here:...
16
votes
Programming cleanly when writing scientific code
Does it really make sense to write code that is say OOP when some
standard stuff could have been done, would have been much faster to
write, and would have been a similar level of readability ...
16
votes
Programming cleanly when writing scientific code
I'd recommend to stick to the Unix principle: Keep It Simple, Stupid! (KISS)
Or, put another way: Do one thing at a time, and do it well.
What does that mean? Well, first of all, it means your ...
12
votes
Is it reasonable to use dictionaries instead of arguments?
The main problem with using dictionaries as arguments or return values is that they are not well defined. The keys and values of a dictionary, especially in loosely-typed languages, mean something in ...
11
votes
Programming cleanly when writing scientific code
I agree with the others that version control will solve many of your problems right away. Specifically:
No need to maintain a list of what changes have been made, or having lots of copies of a file, ...
11
votes
Accepted
Alternative to using regex in Python
"I have to use regex" and "What alternatives are there?" sounds contradictory...
With regular expressions as with any special-purpose tool, sure, there are alternatives to them. ...
10
votes
Why are Python sets and dictionaries not ordered by default?
Your premise is incorrect. As of Python 3.6, dicts remember their insertion order. This was an implementation detail, and was promoted to full language feature in 3.7. In 3.6, for the specific case ...
10
votes
Why are Python sets and dictionaries not ordered by default?
You are correct that item are store internally with some order, but this internal order is determined by the hash code of the key, which is what allows retrieval to be so fast. So if a set/dict should ...
9
votes
Programming cleanly when writing scientific code
Without going to the full-fledge version control + packaging + unit tests kind of mindset (which are good programming practices that you should try to achieve at some point), one intermediate solution ...
7
votes
Python import order, mixing from ... import ... and import ... (and import ... as ...)
Imports should be grouped in the following order:
Standard library imports.
Related third party imports.
Local application/library specific imports.
You should put a blank line between each group of ...
6
votes
Accepted
Why are Python sets and dictionaries not ordered by default?
The point is not that the overhead is particularly large, more that it is there at all.
Language features must always strike a balance of cost-effectiveness. Dictionaries are absolutely fundamental ...
6
votes
Programming cleanly when writing scientific code
The tools of the trade are usually invented to solve a need. If you have the need you use the tool, if not, you most likely don't have to.
Specifically, scientific programs are not the end target, ...
6
votes
Programming cleanly when writing scientific code
The top answers are already good, but I wanted to address some of your questions directly.
Is unit testing necessary for writing smaller pieces of code?
The size of the code is not directly related ...
6
votes
Programming cleanly when writing scientific code
Benefits of clean scientific code
... looking at programming books, they often seem to be addressed at larger projects.
... does it really make sense to write code that is say OOP when some standard ...
6
votes
What is the safe way of using keyword-only arguments?
Indeed, language features have pitfalls, costs, and benefits. The core point of programming language style guides (such as Google Style Guides) and JavaScript: The Good Parts is to avoid features or ...
6
votes
What is the difference between unit testing and handling exceptions
Exception handling and unit testing solve different problems:
Exception Handling: Allows a running program to recover from an exceptional condition (like expecting to be able to open a file but it is ...
6
votes
Does subclassing int to forbid negative integers break Liskov Substitution Principle?
Liskov Substitution Principle is not about implementation but about the promised contract.
Considering your simple class with only one function, you could use the following reasoning for normal ...
5
votes
Should I test the debug branch of my code in a unit test?
If understand correctly you're doing something like this:
public void SendFiles()
{
var files = _sendDirectory.ListFiles();
#IF DEBUG
RemoveTempTextFile();
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
python-3.x × 156python × 98
object-oriented × 13
design-patterns × 9
design × 8
api-design × 6
object-oriented-design × 5
coding-style × 5
django × 5
algorithms × 4
unit-testing × 4
inheritance × 4
optimization × 4
functions × 4
list × 4
api × 3
git × 3
naming × 3
code-quality × 3
language-design × 3
class-design × 3
logging × 3
configuration × 3
abstract-class × 3
dictionary × 3