Coding style – why DRY is not considered a good thing?

People who seem to never dared to cut and paste code have no problem specifying the type of things over and over again. Why not emphasize that type information should only be declared once, so that the entire source code If a certain type of thing is modified as much as possible? For example, using pseudo code borrowed from C# and D:

MyClass foo = new MyClass(ctorArg);

void fun(MyClass arg) {
gun(arg);
}

void gun(MyClass arg) {
// do stuff.
}

than.

var foo = new MyClass(ctorArg);

void fun(T)(T arg) {
gun(arg);
}

void gun(T)(T arg) {
// do stuff.
)

If you change the name of MyClass, or change the type of MyGenericArg, or decide to change the type of foo in other ways, it seems that the second one is not so fragile.

< /div>

It’s not bad at all. In fact, C# maintainers have begun to use the var keyword to reduce tiring boilerplate< p>

MyContainer cont = new MyContainer();

Exactly equivalent to

var cont = new MyContainer();

Although you will see that many people will oppose the use of var, this situation shows that many people are not familiar with strongly typed languages ​​with type inference ;Type inference is mistaken for dynamic/soft typing.

People who seem to never dare to cut and paste code have no problem specifying the type of things over and over again. Why not emphasize that type information should only be declared once, in order to have the smallest possible chain reaction in the entire source code, if a certain type of thing is modified? For example, using pseudo code borrowed from C# and D:

MyClass foo = new MyClass(ctorArg);

void fun(MyClass arg) {
gun(arg);
}

void gun(MyClass arg) {
// do stuff.
}

than.

var foo = new MyClass(ctorArg);

void fun(T)(T arg) {
gun(arg);
}

void gun(T)(T arg) {
// do stuff.
)

If you change the name of MyClass, or change the type of MyGenericArg, or decide to change the type of foo in other ways, it seems that the second one is not so fragile.

< /p>

It’s not bad at all. In fact, C# maintainers have begun to use the var keyword to reduce tiring boilerplate

MyContainer cont = new MyContainer();

Exactly equivalent to

var cont = new MyContainer ();

Although you will see that many people will object to the use of var, this situation shows that many people are not familiar with strongly typed languages ​​with type inference; type inference is mistaken for dynamic/soft typing.

Leave a Comment

Your email address will not be published.