Earlier quoted context omitted.
You'll run out of memory long before your array can reach 2^64 items.
Which means in a practice “infinity” is a lot lower than 2^64, although exactly how much lower depends on system configuration (how much memory the system has, how much of it this process is permitted to consume)
Zero one infinity rule
131–140 of 149 posts
Re: Zero one infinity rule
#132While this has some theoretical merit, IME, limits are quite useful to catch bugs (or prevent degenerate cases). For example I was recently working on a permissions system wherein there can be members of groups. I set a reasonable limit on the size of a group based on a maximum of actual usage and what I could foresee being reasonable. A few days later, this limit was triggered, and I got a bug report. But it turned…
Re: Zero one infinity rule
#133Earlier quoted context omitted.
Which means in a practice “infinity” is a lot lower than 2^64, although exactly how much lower depends on system configuration (how much memory the system has, how much of it this process is permitted to consume)
The point is that the limit should be determined by physical limitations of the hardware, not an arbitrary number chosen by the programmer.
A machine might have 64GB of RAM, but this application might be configured to be allowed to consume a maximum of 4GB of it. In which case, the limit isn’t being determined by the physical limitations of the hardware; whether or not it is under the control of the programmer depends on who does what. Some programmers get to decide how much memory to allocate to their program, for others that decision is made by somebody else.
Re: Zero one infinity rule
#134It's my rule of thumb that the only numbers one should find in code are 0 and 1. Anything else is a magic number and needs to be handled accordingly.
def solve_quadratic_equation(a, b, c): d = b * b - 4 * a * c if d >= 0: return [(-b + sqrt(d)) / (2 * a), (-b - sqrt(d)) / (2 * a)] else: return [] How would you deal with the “magic numbers” here?
def print_every_second_for_a_minute():
...
having said that - there are always exceptions.Re: Zero one infinity rule
#135Earlier quoted context omitted.
def solve_quadratic_equation(a, b, c): d = b * b - 4 * a * c if d >= 0: return [(-b + sqrt(d)) / (2 * a), (-b - sqrt(d)) / (2 * a)] else: return [] How would you deal with the “magic numbers” here?
good question. but if you look at the "magic" concept from a higher level then you'll find that this function of course is based on magic numbers as it itself is a _magic function_. it doesn't solve generally equations but specifically _quadratic_ equations. a more extreme and obvious example in the same vain would be: def print_every_second_for_a_minute(): ... having said that - there are always exceptions.
Solving equations in general is not possible, so if you're trying to say that this function is too specific, good luck generalizing it.
Re: Zero one infinity rule
#136Earlier quoted context omitted.
good question. but if you look at the "magic" concept from a higher level then you'll find that this function of course is based on magic numbers as it itself is a _magic function_. it doesn't solve generally equations but specifically _quadratic_ equations. a more extreme and obvious example in the same vain would be: def print_every_second_for_a_minute(): ... having said that - there are always exceptions.
How would you define a “magic function”? Solving equations in general is not possible, so if you're trying to say that this function is too specific, good luck generalizing it.
---
you could also think of it this way. a generalized equation solver would necessarily derive the solution in a complete fashion. for someone analyzing the code it would be come clear what's going on. your specialized equation solver otoh just has some "magical" solution in there.
Re: Zero one infinity rule
#137While this has some theoretical merit, IME, limits are quite useful to catch bugs (or prevent degenerate cases). For example I was recently working on a permissions system wherein there can be members of groups. I set a reasonable limit on the size of a group based on a maximum of actual usage and what I could foresee being reasonable. A few days later, this limit was triggered, and I got a bug report. But it turned…
Re: Zero one infinity rule
#138Earlier quoted context omitted.
I'd say that 2 is indeed quite common in Computer science. In graphs, for example, each edge has 2 ends. In an abstract syntax tree, a binary operator has 2 children. All raster images are based on 2 dimensions. A "for each" loop often involves 2 variables: the iterator object and an index. In all such cases it makes sense that a language/ construct/library special-cases 2 elements without generalizing for an arbitra…
Usually you wouldn't store these things in a collection, but as separate fields of a record type, so the rule doesn't apply.
Re: Zero one infinity rule
#139Earlier quoted context omitted.
Usually you wouldn't store these things in a collection, but as separate fields of a record type, so the rule doesn't apply.
The principle applies to all kinds of structures. For example a common way to implement a generic tree that respects this rule is to model each node with exactly one parent and any number of children. My statement instead is that you may have legitimate reasons to want exactly one parent and from zero to two children, but not more.
Re: Zero one infinity rule
#140Earlier quoted context omitted.
Usually you wouldn't store these things in a collection, but as separate fields of a record type, so the rule doesn't apply.
The principle applies to all kinds of structures. For example a common way to implement a generic tree that respects this rule is to model each node with exactly one parent and any number of children. My statement instead is that you may have legitimate reasons to want exactly one parent and from zero to two children, but not more.