Are "function's block", "loop's block", etc. good terminology?

The Go Programming Language, p. 46: “A syntactic block is a sequence of statements enclosed in braces like those that surround the body of a function or loop.”

But I don’t see anyone out there using phrases like “the code in the loop’s block runs repeatedly” or “the function’s block is executed”.

Is this just because such beginning-programmer-oriented instruction hasn’t generally been needed in the Go community thus far? Or is this terminology technically inaccurate in some way?

Probably just overly verbose, compare

“the code in the loop’s block runs repeatedly”

to

“the code in the loop runs repeatedly”

or

“the function’s block is executed”.

vs.

“the function is executed”.

The use of block is superfluous.

Aside: if we’re talking about describing code, I’d like to see more people talk about evaluation, rather than execution.

1 Like

Aside: if we’re talking about describing code, I’d like to see more people talk about evaluation, rather than execution.

Can you elaborate on the distinction? I’m used to “evaluation” implying both parsing and running code, as done in interpreted languages; I don’t think of compiled code as something that gets “evaluated”. Could be I’ve just never looked into it that closely, though.

The statement x := y + 4 isn’t executed, it’s evaluated.

Here’s a Stack Overflow question relating to execution vs. evaluation in C… Is this applicable here? Would you say the answer from “haccks” is correct, the one from “Fabio Turati”, or both?

Probably Fabio’s, but that isn’t the important part. The important part is building you own mental model how expressions are evaluated, because when you are able to do that, you can ask questions like “what is the type of evaluating this expression”?"

So being able to say "the result of y + 4 is an int"? Or are you saying the evaluation itself has a type?

Evaluating an expression results in a value, each value has an associated type.

Generally I think it makes most sense to talk about loop or function bodies, however, blocks are still an important concept. Consider the following code:

func foo() {
    // Do stuff...
    {
        bar := true
    }
    // bar is out of scope.
}

Here I’ve used a code block to define a variable that is out of scope after the block is executed. Same as what happens with if statements and for loops.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.