DEV Community

Discussion on: No to Imperative Code! Because Debugging Spaghetti is No Fun

Collapse
 
jbristow profile image
Jon Bristow

Accumulator, maximum, array. Common names for library development in javascript.

However, the code above is also only slightly declarative because it’s still a command.

100% Declarative style imo doesn’t really exist in Javascript. Spend some time getting to know Prolog and you’ll see the difference.

Collapse
 
mcsee profile image
Maxi Contieri

IMHO, using abbreviations makes code less readable and less declarative
Even being common in many mainstream languages they are code smells
Again, in my opinion.
to achieve 100% declarativeness, you should stop using abbreviations.
I've used both prolog and JS

Thread Thread
 
jbristow profile image
Jon Bristow

The variable names don't really come into whether it's imperative or declarative.

Abbreviations are a readability issue. I'd agree with you that they're generally less clear in most places except for examples and generics.

Thread Thread
 
mcsee profile image
Maxi Contieri

good names are clean and declarative.
bad names are not imperative, they are just bad

Thread Thread
 
jbristow profile image
Jon Bristow

Yes, but the way you’re using the terms “declarative and imperative” is not aligned with the computer science definitions of those words.

Imperative code tells the computer to do something. “Reduce over this list of orders by summing their cost”

Declarative states an axiom or truth. “This value is the result of applying a fold right operation using the function a on x

Thread Thread
 
mcsee profile image
Maxi Contieri

you are right.
I am abusing the concept.
It is declarative code with bad names

Thread Thread
 
noriller profile image
Bruno Noriller

I don't agree because it should never be set on stone.

"Max" is pretty descriptive even if you don't use "maximum", inside that scope it's ok. Wouldn't be true if "m" were used.

However, in this example:

const productsIds = products.map(p => p.id)
Enter fullscreen mode Exit fullscreen mode

I would say one letter is the correct choice, since:

const productsIds = products.map(product => product.id)
Enter fullscreen mode Exit fullscreen mode

Having no abbreviations actually hurt how to read and generate just extra clutter.

My rule of thumb is to have the name length inversely proportional to the scope.

A dozens of lines function calls for bigger, descriptive names.
A one liner? Maybe one letter is enough.

Thread Thread
 
mcsee profile image
Maxi Contieri

'My rule of thumb is to have the name length inversely proportional to the scope'

It is fine.