Is it possible to improve the writing of this code?

Asked

Viewed 63 times

1

I have the following code below:

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

Which 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 set the variables aside and use a Array

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

There is a better way to optimize code instead of leaving it so?

memo1.Lines.Add('def' + ' ' + nome + ' ' + '('+ Variaveis[1] +',' + ' '+ Variaveis[2] + ',' + ' ' + Rasters[1] + ')' +':');
  • Yes, it is possible, do not use Object pascal/Delphi =D

  • What do you recommend, friend? D

  • how about python? perl? c# ? Anyone is better hauhauhaha

  • I am using Delphi to assemble a python equation "formula".. I will consider the idea.

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 really gets bad!

I would write that way:

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

But each case is a case, the way it passes the values of the Array is acceptable if not inside a Loop, if you are in the Loop you can use the entire variable itself to identify the position of the Array, in the case that you used the i.

  • I agree the reading will get a little better, but wanted to avoid staying using variables[1], variables[2]...

  • @Guilhermelima come on, these variaveisX comes from the same place ?

  • Yes, they come from the same place.

  • Opa, then next, concatenate the result of all this into a single variable separating with the ,, and there you pass: memo1.Lines.Add('def ' +nome+ ' ('+Variaveis[1]+ Rasters[1]+ '):'); if the Rasters come from the same place already concatenates it Together too!

  • I’ll try to figure it out, and I see how it turned out.

1

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.