DEV Community

Cover image for Are the codes written/read left to right ?! (a misconception among beginners)

Are the codes written/read left to right ?! (a misconception among beginners)

Hooman Talakian on January 15, 2022

Before we begin, we would like to mention that if you are a professional programmer, this article will most probably be a repeat for you, But if yo...
Collapse
 
jzombie profile image
jzombie

When you write asynchronous code, you can write in any direction:

For example, bottom up:

let message = ''

await Promise.all([
  new Promise(resolve => setTimeout(() => {message += 'world'; resolve()}, 200)),
  new Promise(resolve => setTimeout(() => {message += 'hello '; resolve()}, 100))
])

console.log(message) // Hello world
Enter fullscreen mode Exit fullscreen mode
Collapse
 
hoomantalakian profile image
Hooman Talakian

exactly. that is what I'm talking about .as I mentioned in the article :
" Note that this code only makes sense for this example, and based on your programming language and work environment, you should make your own interpretation of this example "
It's all about "thinking" about your code and the order depends on your environment. are we in the same page?

Collapse
 
jzombie profile image
jzombie

Yep, absolutely.

Collapse
 
johnbabu021 profile image
john

Great

Collapse
 
eljayadobe profile image
Eljay-Adobe

Let's say you want to get the user's name, capitalize it (which may be incorrect... sorry k d lang), convert it to a greeting, and print it out.

Most languages would do it this way, in pseudo-code:

string name = input();
name = capitalize(name);
string greeting = greet(name);
println(greeting);
Enter fullscreen mode Exit fullscreen mode

And could do it as a one liner, also in pseudo-code:

println(greet(capitalize(input())));
Enter fullscreen mode Exit fullscreen mode

See the onion-like nesting? Just like what the article is talking about. This code is read inside-out to see the order of operations.

There are other programming languages that would do the one-liner in a slightly different way, in pseudo-code:

input() | capitalize | greet | println
Enter fullscreen mode Exit fullscreen mode

No onion! It reads in order, more like the first step-by-step example.

Collapse
 
hoomantalakian profile image
Hooman Talakian • Edited

I'm not sure if I understand your point, but in every instance you have listed (aside from code direction or difference in programming languages), the syntax would follow the raw data. Am I right? I mean you have a Raw data in the first place and after that you decide how to manage it (based on specific syntax of your program).

Collapse
 
eljayadobe profile image
Eljay-Adobe

My point is only that there are programming languages which do have the codes such that they are read from left to right, like the ML family of languages.

Thread Thread
 
hoomantalakian profile image
Hooman Talakian

Yes, there are many syntactic differences between programming languages, but in this case we are just discussing the path of thought. Thank you for your attention

Collapse
 
colocodes profile image
Damian Demasi • Edited

const result = await name.save()

I would read this code as follows: “create a constant variable named result and store in it the result/response of awaiting for the name to be saved”.

But even better, I think is best to read it as we read mathematical equations: from outside to inside.

Collapse
 
hoomantalakian profile image
Hooman Talakian

Of course, this code can be read, just as you said. This is exactly what "different thinking" is all about. We have just mentioned "one" way of thinking.
By the way, I appreciate your point of view about the mathematical approach.

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
hoomantalakian profile image
Hooman Talakian

Thank you for mentioning. Exactly. "Top to bottom" is the same.