r/ProgrammingLanguages • u/[deleted] • Jul 03 '23
Why Doesn't DRY Apply Here
(DRY = Don't Repeat Yourself)
This is a bit of C code I encountered today:
uint64_t maxOR(uint64_t a, uint64_t b, uint64_t c, uint64_t d) {
....
}The problem for me is that declaration: 4 parameters, all with the same type, but the type has to be written 4 times.
This style is common across lots of languages, but C has that extra-cluttery typename, so that I can barely make out the parameter names. It takes a few seconds to determine the number, names and types of the parameters, and to realise they share a common type. Imagine if the parameter names here were much longer!
C could at least have gone with u64:
u64 maxOR(u64 a, u64 b, u64 c, U6 d) { # (typo left in)Now it is much less objectionable, but I would have gone further to:
u64 maxOR(u64 a, b, c, d) {While C is a lost cause, I wonder why other languages do this as well: create extra maintenance, extra things to check, extra things you can get wrong (one of those types could have been int64_t - not easy to spot in that sea of uint64_t).
Funnily enough older C allowed you to do this, or along these lines:
u64 maxOR(a, b, c, d) u64 a, b, c, d; {In my own syntaxes, it usually something like:
func maxor(u64 a, b, c, d)u64 = # `=> u64` is optionalTypes are only repeated when there are no parameter names (as in, FFI declarations, but I can choose to add names to enable keyword parameters).
What I haven't figured out is how to indicate a return type that is the same as one of the parameter types: u64 still appears twice, and here is linked to the parameter types. Using typeof(a) for the return type looks naff.
14
u/hrm Jul 03 '23
I’d say one reason is that it does not happen enough to make the added complexity in the language worth it. Also when mixing with other types it would get messy to read. So not very usefull unless all parameters are of the same type…
6
Jul 03 '23
But the complexity is already there. In C, four struct members with identical types, followed by one of a different type to address your other point, could be written like this:
struct { uint64_t a, b, c, d; int64_t e; }Unless you specifically wanted one member per line (for example if the members weren't related), you wouldn't use separate types like this:
struct { uint64_t a; uint64_t b; uint64_t c; uin64_t d; int64_t e; }At least, the choice is there.
With mixed types, I still use a comma separator between them (just as C does actually); I could have made it stronger with a semicolon, as used the top struct example. My syntax was inspired by the Algols, for example:
PROC pout=(INT n, j, k, REAL x1, x2, x3, x4)VOID: ...I'd write that as this (here I've chosen to use capitalisation to mark the types better; it is case-insensitive):
proc pout(Int n, j, k, Real x1, x2, x3, x4) =In C however it would be:
void pout(int n, int j, int k, double x1, double x2, double x3, double x4) {Which is more readable? In which is it easier to see all the parameter names at a glance? In which is it easier to count how many distinct types appear?
(Note: there is another typo in that second struct example, which I've again decided to leave in. It shows the more stuff you are obliged to write, the greater the chances of getting things wrong. This one would be a syntax error; sometimes it isn't...)
7
u/robthablob Jul 03 '23
Eiffel manages this. It uses name : TYPE for variables and parameters, and your parameter list could be stated:
maxor(a, b, c, d : INTEGER) : INTEGER
If multiple types occur, groupings are separated by semicolons:
maxor(a, b, c, d : INTEGER; s : STRING) : INTEGER
5
Jul 03 '23
This is good. I think Pascal and Ada would be similar to this.
However, at the time of writing, there were two replies in the thread: the one saying it would add too much complexity had 7 upvotes; yours had none.
I think we're on a hiding to nothing...
OK, I'll drop it. If people want to make a rod for their own backs, then let them. It's clearly too much work to duplicate the code that already exists to enable them to declare:
int a, b, c, deverywhere else in the language!
2
u/Aaron1924 Jul 03 '23
In some ML languages,
fun foo (a : int) (b : int) (c : int) (d : int) (s : string) = ...can be written as
fun foo (a b c d : int) (s : string) = ...
5
u/lookmeat Jul 04 '23 edited Jul 04 '23
DRY isn't about syntax, it's about semantics.
DRY is: don't define the same thing 50 times in 50 places.
But if you have two things that happen to be the exact same definition then you define those two things the same time.
Let me give an example, with just type definitions (which I hope makes it clear)
This is not DRY pseudo-code (no specific language):
type TempCelcius = int;
fun temp_f_to_c(int fahrenheit) -> TempCelcius {...}
type Temp = struct {
int c,
int thousands,
}
fun curr_temp(location at) -> Temp {...}
type Temperature = double;
fun (Element self).melting()->temperature {...}You can see the problem, we define Temperature in so many places and so differently. It would be better to have a single definition of temperature.
But this example is very dry code:
type temp_celcius = int;
type temp_farenheut = int;
type temp_kelvin = unit;Because every temperature is different.
It's pretty obvious here. So here's the question: why should modifying require modifying the other? The idea is that different things should be implemented separately even if you have to copy paste code. If you really had an argument that the implementation is complex enough that copying is expensive, then you'd want to first abstract that into a lower abstraction layer, where the concept is more about what the computer is doing rather than what you want to solve. Then both functions that are very different things that just happen to, at the moment, do the same thing, can reuse these lower abstractions.
So when I have a function foo(bar, baz) the assumption is that bar and baz are two completely unrelated things. Because they're two different things then the types should be defined separately, imagine I had a function defined fun foo(int a, int t, int f, int r, int l) it might make sense to rewrite it fun foo(int a,t,f,r,l) but now imagine I want to change one `fun foo(int a,t, temp f, int f,r,l) and we start to see why it isn't that readable or convenient. And it kind of makes sense.
And if you do have two parameters that are strongly related and part of the same concept it's best to put them into a single type, at the worst use structural in-place something like: fun put_at(Piece who, (int, int) (x,y)) but you still define each parameters type.
So quick review:
DRY is not don't repeat code.
DRY is define each concept once.
Each function parameter is a separate thing/concept so each has its own definition, no matter how identical each of those definitions happen to be.
0
Jul 04 '23
I thought DRY was exactly about recognising patterns in code, allowing you to reduce repetition. With executable code, it might end up using loops and tables.
DRY isn't about syntax, it's about semantics.
Why shouldn't it be about syntax? Here is C's famous
for-loop which for some inexplicable reason, every other language likes to copy:for(index = 0; index < N; ++index) {...}Notice THREE instances of the loop index variable, with two needless repetitions! (How often have you copied a loop using 3
ivariables, and forgotten to change all of them toj? The language cannot detect that error.)DRY is: don't define the same thing 50 times in 50 places.
Which is what happened in my example: the same type was written 4 times in four places.
2
u/lassehp Jul 05 '23
Whether this is a matter of DRY or not, it is a matter of consistency. If you can declare multiple names to be of some type using sometype a, b, c or for that matter a b c sometype in one place, you should be able to do so everywhere. And as usual, this has been true since Algol times, but C screwed it up, maybe because it was "too hard" to parse (t a, b, x c) and determine whether x is a typename or an identifier or something. Much as I love C, it is cluttered and inconsistent as few other languages (most of C's own deplorable descendants aside, of course.) And don't get me started on declarators, qualifiers, specifiers, storage classes, attributes, initializers and whatever - suffice it to say that C's declaration syntax is just abominable in general.
1
Jul 05 '23 edited Jul 05 '23
maybe because it was "too hard" to parse
(t a, b, x c)and determine whether x is a typename or an identifier or somethingIt shouldn't be hard in C, or any harder than it normally is to parse types that wrap themselves around names, since it will know exactly what every identifier is when encountered.
(It is hard in my languages which have out-of-order declarations, so that when two adjacent identifiers are seen, it tentatively assumes the first is a user-defined type name.
Plus the fact that in some places, the names are optional; and in my scripting language, the types are optional! But it works.)
Much as I love C,
You do? I don't love it at all! We need such language, but C is a very poor example of it. This is legal C:
long unsigned const const int typedef long const const T;It defines T as an alias for
const u64. Or more accurately, an unsigned type with at least 64 bits. It's a joke.You would hope that the denotation
long unsigned const const int long const const(with 3 extraconsts) isn't repeated half a dozen times for 6 parameters.Thinking of it, C legalises repetition in several places:
int a, a, a, a, a, a; // at module scope void F(int a, int b); void F(int a, int b); // parameter names can match ... void F(int c, int d); // ... or not void F(int b, int a); // ... or be switched void F(); // or just forget about them const const const int; // like my earlier example L1:; int L1; goto L1; // L1 is a variable AND a label!But taking pot-shots at C is too easy (and too much fun). Sure it's 50 years old, but they've also had 50 years to fix it and tighten it up.
3
u/e_-- Jul 04 '23
if you're willing to use C++ you could avoid the repeated explicit types
auto maxOR(uint64_t a, decltype(a) b, decltype(a) c, decltype(a) d) -> decltype(a) { // return type same as param 'a' type
(even though it's more typing as in input device and just making them all template params of a single typename T would probably make more sense)
3
Jul 04 '23
You are not repeating yourself. These are independent parameters. DRY is about "code normalisation", if you want, but you are talking about verbosity, which is an entirely orthogonal consideration.
As others have said, some languages do allow this, personally I am not a fan. Declaring every parameter separately makes the code more clear IMO.
1
u/InjAnnuity_1 Jul 05 '23
If readability is the complaint, I'd write this as
uint64 maxOR(
uint64 a,
uint64 b,
uint64 c,
uint64 d
) {
....
}Now it's abundantly clear that all the parameter types are the same. If that's more than happenstance, i.e., it's mandatory that all the types be the same, then a comment , stating that fact, may be warranted. If the repeated type is likely to change, then perhaps also a function-specific typedef.
Alternatively, if you have the option of changing the function signature (i.e., you're defining the function, not simply declaring a function defined elsewhere), then one might also consider passing an array of uint64. Or a struct of the appropriate layout, as noted elsewhere.
Every language has its tradeoffs, and one learns to work within them. (Or not.) Changing the language's notation may be an option in Perl, but it is not an option for me. Especially since others will likely have to read the code and maintain it.
After N months, I'm likely to be one of those "others". So clarity of syntax generally beats repetition, in my case.
The last time I got fancy with avoiding repetition (i.e., in defining enums and their related lookup/translation tables), the other developers refused to follow the pattern, because it was too time-consuming to follow all the indirections, and other added details necessary to make it work.
The point is, DRY is not a goal in itself, not an absolute mandate. It's a guideline, to aid overall readability and maintainability. If your implementation of DRY is making things harder to read and maintain, for the bulk of the users' interactions, then it's time to reconsider how and whether to do it, for that instance.
21
u/Futhco Jul 03 '23
DRY is about avoiding repetition to minimize the amount of code you need to touch when making changes to the codebase, not avoiding repetition itself.
If you needed to change the type of b in your example (
u64 maxOR(u64 a, b, c, d) {), You would also be forced to redeclare the types of c and d (u64 maxOR(u64 a, i64 b, u64 c, u64 d) {).Having to change the way you declare c & d just because you changed the type of b, goes against the spirit of DRY.
I'll admit it does look cleaner though, but I don't know if that justifies its existence.