Is it possible to improve the writing of this code?

1

I have the following code below:

memo1.Lines.Add('def' + ' ' + nome + ' ' + '('+ variavel1 +',' + ' '+ variavel2 + ',' + ' ' + raster1 + ')' +':');

That results in something like this:

def nome (variavel1, variavel2, raster):

But I would like to optimize the code above. The changes I made were to leave the variables aside and use an Array

Variaveis[i] := TEdit(FindComponent('edt_variavel'+IntToStr(i))).text;
Rasters[i] := TEdit(FindComponent('edt_raster'+IntToStr(i))).text;

Is there a better way to optimize code instead of leaving it like this?

memo1.Lines.Add('def' + ' ' + nome + ' ' + '('+ Variaveis[1] +',' + ' '+ Variaveis[2] + ',' + ' ' + Rasters[1] + ')' +':');
    
asked by anonymous 01.02.2016 / 12:22

2 answers

2

Instead of giving the spaces between the variables and the Strings, you should already write the Strings with the spaces, however, this change does not optimize at all, for the compiler this is indifferent!

But for reading and interpreting the code, it really does look bad!

I would write this way:

memo1.Lines.Add('def ' +nome+ ' ('+Variaveis[1]+ ', '+Variaveis[2]+ ', ' +Rasters[1]+ '):');

But each case is a case, the way you are passing the Array values is acceptable if it is not within a Loop, if it is in the Loop you can use the whole variable itself to identify the Array position, in case you used i .

    
01.02.2016 / 12:37
1

You can also use the Format that assists when you have more than one type of array. memo1.Lines.Add(Format(' def %s, (%s, %s, %s):', [Variaveis[1], Variaveis[2], Rasters[1]]))

    
01.02.2016 / 12:53