Friday, April 29, 2011

Too much flexibility in programming language, good or bad?

PHP is a very flexible programming language, and javascript is worse. I use the word 'worse' and i think that presents my point regarding language flexibility.

Too much flexibility usually means you can achieve same goal through variant ways. My point of view, that can only bring confusion. I guess that is one reason why software engineering stress so much on convention like naming convention and coding convention.


var person = {name:'henry', gender:'m'};


The code define a variable, person. Obviously, here person is an object. So we can access its name property through:

 person.name


Well, and you probably know that we can get the same result by calling

person['name'].


For me, that brings nothing but confusion. Let's see another example. The code below works very well:

function func() {
     return {name:'henry'};
}
//you will see Object { name="henry" } in console
console.log(func());


This also works as well:

//there is no semicolon!!!!
function func() {
     return {name:'henry'}
}
//you will see Object { name="henry" } in console
console.log(func())


See the difference? The second code snippet doesn't have semicolon! Javascript will add semicolons for you!
But check this code:
function func() {
     return 
     {
           name:'henry'
     }
}
//you will see undefined
console.log(func())



What do you expect from the above code? You are wrong if you think you still get an object. Actually, you will get undefined! Thanks to the javascript semicolon insertion mechanism, the code is actually like this:
function func() {
     return ;
     {
           name:'henry'
     }
}
//you will see undefined
console.log(func());


So, i hope you see my point. I don't like too much language flexibility because it helps and even encourages to create error-prone, inconsistent, and confusing code.

3 comments:

Kesavan Rengarajan said...

The 'dot' and the other notation have their use cases. Look at the following:
var a = {name: 'k7', 'name 2': 'k72'};
alert(a.name);
var k = 'name 2';
alert(a[k]); //the key can be dynamic and can the key can also contain spaces in this notation.

As for JS not requiring ';' after an expression and the resulting confusion, you just have to look at other languages (Python, Haskell, etc) that do not require or do not allow semicolons ,and, they are doing just fine.

And, your example:

function func() {
return
{
name:'henry'
}
}
//you will see undefined
console.log(func())

does not work as expected not because JS does not require a ';' after an expression but, because the return statement is on a line by itself (think of how you will fix that code - you will not add ';' after the return statement and one after the returned object but, you will put them all on the same line)

Flexibility is usually good. In most companies you working environment(including: OS, languages being used, IDEs, VCS and so on) and projects that you can work on are all fixed and you have little say on those. Your creativity can only be expressed by the code that you produce. And, I think a flexible language appeals to people with different tastes. I would not like to work on a rigid platform where there is only one way to do things.

Just my 2c :)

Henry said...

well, i guess this comes to the statement you mentioned before:Software Engineering is in many ways a pathetic field, because so much of it is anecdotal and based on people's judgement or even people's aesthetic judgements

So, i wouldn't say which is right, which is wrong. As it is said, it is based on people's judgement. But i think in software engineering, minimize any possible confusion is a good practice.

I know when accessing dynamic properties, we have to do it as an array key. But my point is, just like PHP can do $this->$property, javascript should somehow allow developers to access dynamic property through object access mechanism, something like person.{$property}. And object properties should be accessible only through '.' while array should be through []

Kesavan Rengarajan said...

I agree with most of what you have said.

BTW, Arrays are objects in JS.