Forum > Suggestions

multiple variable initialisation

<< < (3/3)

440bx:

--- Quote from: marcov on June 11, 2021, 12:01:07 pm ---It was exactly for this reason. Avoiding endless decisions for endless suggestions of micro syntax "improvements".

--- End quote ---
It is not a simple syntax improvement. 

The reason for allowing multiple variables in a declaration is to make it clear that those variables should be of the same data type.  That way, if someone decides to change the type (or name of the type), all the variables in the declaration are automatically changed (less room to make a mistake.)

It's exactly the same thing with the value.  If a group of variables (obviously of the same type) should have the same initial value then that fact would be automatically ensured if the value could be stated in the declaration.   Currently, Pascal does not have a mechanism to ensure that two or more variables have the same initial state.

That feature is NOT about syntax or micro-anything.  It's not occult science... not even micro-occult.

What's genuinely surprising is that this extremely easy to implement feature is not yet part of the language (though, a bit less surprising when considering the "analysis" it gets from some people.)

eny:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---const MAGIC_NUMBER_5 = 5; // 5 because it is one more than 4 and one less than 6...procedure foo;var  a : integer = MAGIC_NUMBER_5;  b : integer = MAGIC_NUMBER_5;begin  //...end;

440bx:

--- Quote from: eny on June 11, 2021, 12:57:57 pm ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---const MAGIC_NUMBER_5 = 5; // 5 because it is one more than 4 and one less than 6...procedure foo;var  a : integer = MAGIC_NUMBER_5;  b : integer = MAGIC_NUMBER_5;begin  //...end;
--- End quote ---
Not good because the constant name is not unique in the declaration.  IOW, you should not have to duplicate the constant's name because when modifying the program, the programmer has to ensure every instance of the constant is updated and must also ensure that if a new variable is declared, the constant's name is used to initialize its value.

The natural solution to those problems is to have a single declaration for all the variables that share a type and initial value.

Seenkao:
Я так же хотел чтобы это было в паскале. Но, чем дальше я занимаюсь программирование, тем более понятно, что тут будет множество подводных камней. И код:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  a: integer = 3;  b: integer = 3;...должен быть идентичен коду:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  a, b: integer = 3;...
Но если взглянуть на это с другой стороны, то многие не будут понимать что происходит. И код:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  a: integer;  b: integer = 3;...не будет идентичен коду

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  a, b: integer = 3;...хотя пользователь во втором случае захочет выделить именно значение "b = 3", но не "a".
А таких ситуаций будет больше, чем при той ситуации, когда так объявлять переменные нельзя.

google translate:
I also wanted it to be in pascal. But, the further I do programming, the more it becomes clear that there will be many pitfalls. And the code:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- var  a: integer = 3;  b: integer = 3;... should be identical to the code:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- var  a, b: integer = 3;...
But if you look at it from the other side, then many will not understand what is happening. And the code:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- var  a: integer;  b: integer = 3;... will not be identical to the code

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- var  a, b: integer = 3;... although the user in the second case will want to highlight exactly the value "b = 3", but not "a".
And there will be more such situations than in the situation when it is impossible to declare variables in this way.

damieiro:
Well, i have read this now.

I agree with 440bx view.  I think that group variables like this


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- const  INITIAL_VALUE=0var  a, b, c, d, e: integer = INITIAL_VALUE;
Or


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---const  INITIAL_VALUE=0var  a, b, c, d, e: integer; begin  a, b, c, d, e:=  INITIAL_VALUE; end; 
it's better for homogeneus treatment of these. There is One entry point to change. And if you like old ways, then use it. I like modern ones.
And compiler has a hint of serialization (do all the operations with less register operations, for example)
Really is more information in this code, because programmers does the group of similar variables.

And i do not view this as a C vs Pascal syntax. I view this like a old-ways modern-ways of doing things. C vs pascal is a=0 or a:=0 or braces vs begin end.
But the code is really more readable allowing multiple instantiation/initialisation. And technically is possible.

Other way (but this could be out of pascal way of thinking) if we were allowing initialising types

Example (for the sake of completness, but really with above it's fairly enough)


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- const  INITIAL_VALUE=0 type TIntegerInitialized=integer =INITIAL_VALUE;  //example of type plus initialization, all of these types will be initialized. var  a, b, c, d, e: TintegerInitialized;


Navigation

[0] Message Index

[*] Previous page

Go to full version