DEV Community

Discussion on: What do you think about the idea that "there should be only one way of doing a thing" in programming languages?

Collapse
 
vonheikemen profile image
Heiker • Edited

Is not a terrible idea.

In PHP you can append an item to an array in two ways

$arr = [];

// like this
array_push($arr, 1);

// or this
$arr[] = 2;

// PHP, why would you do that to yourself?
Enter fullscreen mode Exit fullscreen mode

It certainly could help avoid meaningless discussions. Not so long ago the javascript twitterverse got mad because apparently reduce is like 500 times slower than a for loop, and of course the performance people was all over the place saying: stop using reduce, your killing your app. Think about the children!!

Might also lead to less bugs, because there are less "tricky parts." Consider this javascript object:

const shape = {
  radius: 10,
  diameter() {
    return this.radius * 2;
  },
  perimeter: () => 2 * Math.PI * this.radius
};

shape.diameter();
shape.perimeter();
Enter fullscreen mode Exit fullscreen mode

Even though diameter and perimeter are "methods" one of them raises an error.

Collapse
 
aryamaan08 profile image
Aryamaan08

Apparently, they like to do things in 2 ways, because they have 2 ways of adding a single-line comment.