Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Usage of braces around variable names

38 views
Skip to first unread message

Cecil Westerhof

unread,
Jan 22, 2018, 1:28:06 PM1/22/18
to
From my days in bash programming I have the habit of always using
braces around variable names. So I do not write $foo, but ${foo}. Two
times I was told that this is not done in the tcl world. Is this true?
Then I should change my ways. ;-)

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Gerald Lester

unread,
Jan 22, 2018, 2:08:51 PM1/22/18
to
On 01/22/2018 12:25 PM, Cecil Westerhof wrote:
> From my days in bash programming I have the habit of always using
> braces around variable names. So I do not write $foo, but ${foo}. Two
> times I was told that this is not done in the tcl world. Is this true?
> Then I should change my ways. ;-)

It is not normally done since it is not normally required. It is
required in cases where the variable name is not a word. Thus when most
tclers see the braces, they pause and attempt to figure out why.

IMHO, you should change your ways.

--
+----------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |
| Email: Gerald...@kng-consulting.net |
+----------------------------------------------------------------------+

Matthew Hiles

unread,
Jan 22, 2018, 4:00:45 PM1/22/18
to
Between all the [] and all the {} you have to use anyway, I'd say break the habit and cut down on the character noise.

sled...@gmail.com

unread,
Jan 22, 2018, 4:28:04 PM1/22/18
to
On Monday, January 22, 2018 at 1:28:06 PM UTC-5, Cecil Westerhof wrote:
Hold on gang...not so quick...

Excellent question.

Consider
set firstname rick
set fullname $firstnameHorwitz
set fullname ${firstname}Horwiz

Furthermore, if you would like to use multi-word variable names, then braces are the way to do it.

Cecil Westerhof

unread,
Jan 22, 2018, 5:59:06 PM1/22/18
to
sled...@gmail.com writes:

> On Monday, January 22, 2018 at 1:28:06 PM UTC-5, Cecil Westerhof wrote:
>> From my days in bash programming I have the habit of always using
>> braces around variable names. So I do not write $foo, but ${foo}. Two
>> times I was told that this is not done in the tcl world. Is this true?
>> Then I should change my ways. ;-)
>>
>> --
>> Cecil Westerhof
>> Senior Software Engineer
>> LinkedIn: http://www.linkedin.com/in/cecilwesterhof
>
> Hold on gang...not so quick...
>
> Excellent question.
>
> Consider
> set firstname rick
> set fullname $firstnameHorwitz
> set fullname ${firstname}Horwiz

That was one of the reasons I used it in bash. If the variable
firstnameHorwitz existed, you got another result as when it did not
existed. But that is not the case anymore.

But there where some other reasons also, but I do not remember them
anymore. :'-(


> Furthermore, if you would like to use multi-word variable names, then braces are the way to do it.

And I like to do things 'always' the same way. I remember many years
ago when I visited a family in the middle of nowhere I still locked my
bicycle. They where a bit offended by that. But my reasoning was that
if I did it when it was not necessary, I also did it when it was
necessary. ;-)

By the way if I remember correctly the style-guide says to always use
( and ) in expressions, even when they are not necessary. (Something I
already did.) So that is a bit inconsistent. ;-)

lmn...@gmail.com

unread,
Jan 22, 2018, 8:07:03 PM1/22/18
to
Indeed, using braces around variable names is embraced by the tcl world...

I have a bit more nuanced set of 'rules' for braces then simply eliminating them whenever possible...

The rules I 'generally' follow:

Use braces: If var name contains a leading underscore: ${_var_name}
Use braces: If performing concatenation: ${var}suffix
Otherwise default is not to use braces: $var [1]

Jim

[1] Caveats to the default rule.

Consider adding braces, if using:
- long variable names
- adding braces helps read the code

Generally: If I can read the code more effectively with braces -- I use them.

Consistency is an important factor. (this means different things to different people)

Another consideration: Unmatched braces. The more braces, the more opportunities for missing braces and abrupt syntax run-time errors (this is both a positive and negative consequence). Slight more positive in my view.
(In the end, I don't give this much weight +/- but mention because you might).
0 new messages