<!-- language-all: java --> Dave Thomas, the author of the [Don't Repeat Yourself][1] principle [said][2]: > DRY says that every piece of system knowledge should have one > authoritative, unambiguous representation. Every piece of knowledge in > the development of something should have a single representation. A > system's knowledge is far broader than just its code. It refers to > database schemas, test plans, the build system, even documentation. I have difficulty to understand how it applies to programming code documentation. Java API has the [java.util.Arrays][3] class with the *copyOf* method that is overloaded 7 times. The 8 methods can be documented in two ways containing the same information. The first way describes all overloaded methods using a single description, highlighting the differences between overloads. The second way describes each overloaded method using a separate description. Below are both ways - I bolded the differences in the content. The second way was literally copied from the API documentation of the java.util.Arrays class. **Is the second way compliant with the DRY principle?** In my opinion it isn't, because it contains text which was copied multiple times, then, additionally, minor modifications were made to each copy. The same information can be provided in one text, which is almost eight times shorter. But I am not sure if I understand correctly the definition of the DRY principle. What are the "pieces of system knowledge" in this case? Do they have single, unambiguous, authoritative representation in the quoted documentation? Documentation of the overloaded copyOf() methods - 1st way ========================================================== > > public static byte[] copyOf(byte[] original, int newLength) > public static short[] copyOf(short[] original, int newLength) > public static int[] copyOf(int[] original, int newLength) > public static long[] copyOf(long[] original, int newLength) > public static float[] copyOf(float[] original, int newLength) > public static double[] copyOf(double[] original, int newLength) > public static char[] copyOf(char[] original, int newLength) > public static boolean[] copyOf(boolean[] original, int newLength) > > Copies the specified array, truncating or padding with **the default > values** (if necessary) so the copy has the specified length. For all > indices that are valid in both the original array and the copy, the > two arrays will contain identical values. For any indices that are > valid in the copy but not the original, the copy will contain **the > default value**. Such indices will exist if and only if the specified > length is greater than that of the original array. > > **The following table lists default values for given type of array elements:** > > > | type | the default value of type | > |----------------------------|-------------------------------| > | `byte`, `short`, `int`, `long`| `0` | > | `float`, `double` | `0.0` | > | `char` | null character `('\u0000')` | > | `boolean` | `false` | > > Parameters: > - original - the array to be copied > - newLength - the length of the copy to be returned > > Returns: a copy of the original array, truncated or padded with **the > default values** to obtain the specified length > > Throws: > - NegativeArraySizeException - if newLength is negative > - NullPointerException - if original is null > > Since: 1.6 Documentation of the overloaded copyOf() methods - 2nd way ========================================================== > public static byte[] copyOf(byte[] original, int newLength) > > Copies the specified array, truncating or padding with **zeros** (if > necessary) so the copy has the specified length. For all indices that > are valid in both the original array and the copy, the two arrays will > contain identical values. For any indices that are valid in the copy > but not the original, the copy will contain **(byte)0**. Such indices > will exist if and only if the specified length is greater than that of > the original array. > > Parameters: > - original - the array to be copied > - newLength - the length of the copy to be returned > > Returns: a copy of the original array, truncated or padded with > **zeros** to obtain the specified length > > Throws: > - NegativeArraySizeException - if newLength is negative > - NullPointerException - if original is null > > Since: 1.6 > > public static short[] copyOf(short[] original, int newLength) > > Copies the specified array, truncating or padding with **zeros** (if > necessary) so the copy has the specified length. For all indices that > are valid in both the original array and the copy, the two arrays will > contain identical values. For any indices that are valid in the copy > but not the original, the copy will contain **(short)0**. Such indices > will exist if and only if the specified length is greater than that of > the original array. > > Parameters: > - original - the array to be copied > - newLength - the length of the copy to be returned > > Returns: a copy of the original array, truncated or padded with > **zeros** to obtain the specified length > > Throws: > - NegativeArraySizeException - if newLength is negative > - NullPointerException - if original is null > > Since: 1.6 > *and so on for: int, long, float, double, char, boolean.* It is *exactly* the same text, eight times, where only the bold parts are different. [1]: https://en.wikipedia.org/wiki/Don%27t_repeat_yourself [2]: https://www.artima.com/articles/orthogonality-and-the-dry-principle [3]: https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/Arrays.html