2
Between one line and another there are some doubts that do not change the flow of decisions at all, but perhaps can influence the performance of the application by the amount of extra lines depending on the way you program.
I like to keep my codes as streamlined as possible, but when it comes to keeping organized I don’t spare lines of code.
In this eagerness to keep a well readable code I always open and close a block of commands as in the example below:
if (a = b) then
begin
ShowMessage('A é igual a B');
end;
See that there is only one line inside the if
, but "I like" always open the begin
and end
, because tomorrow I have to insert more lines the block is already ready and I don’t have to look for end if
, I do it even though I’m nestled in if
s simple, example:
if (a = b) then
begin
ShowMessage('A é igual a B');
end
else
begin
if (a = c) then
begin
ShowMessage('A é igual a C');
end;
end;
I could reduce the number of these lines if
s, but "like" this way of working.
Now the question of the theme:
The use of more lines as in the examples above, will influence the performance of the application?
- I imagine that the more independent lines of anything the processor and memory will work a little more, right?
- Or is the compiler smart enough to ignore the
begins
when it comes to aif
simple?
This doubt came to me because the application already be with thousands and thousands of lines of code, now as quoted, I do not know and these thousands are only lines considered valid by the compiler for the execution of the application or if all lines within the executable.
Another example, but now with "comments"
// Esse bloco de comentário será ignorado pelo compilador?
// Não
// Importa
// Quantas
// Linhas
// Tenha?
if (a = b) then
ShowMessage('A é igual a B');
The number of lines above is greater than one if
simple with begin
and end
, but I imagine that the compiler treats different from a command block, because even being comment the processor will have to read the line to know what it is.
An example I particularly detest, but can be more efficient:
if (a = b) then
ShowMessage('A é igual a B')
else if (a = c) then
ShowMessage('A é igual a C');
Maybe this may be a useless discussion, but day to day as the application grows we realize that the performance drops a little with each new Form, so any change of habit in the programming that will contribute to improve this performance can be valid.
Note: I gave as an example the Delphi (Pascal), but it is valid for any language.
It would be better if you contextualized it in the form of a question and posted it as your answer. Because as it is, it runs away from how the site works. It is a Q&A, it needs to have a question and an answer. That fits more as an answer.
– user28595
But I would like to know whether or not it influences performance, whether I answer it would not be as if I already knew?
– Marcelo
Then turn it into a question, the same one you asked yourself to get this answer you posted. That way, it’s all within the format of the site. ;)
– user28595
Note: there is no problem in creating a question already posting answer, it is even advisable, if you have some knowledge and want to share, this is welcome!
– user28595
I changed the question
– Marcelo
Marcelo, I don’t think you understand. The complete field, including the title and the body, are for questions. If you have a question that you have found the answer and want to share, you should do as per the [tour] explains. You should separate the question from the answer, because as the tour explains, here is a Q&A, this field you used is for the question, the field below, where has the button "Post your answer" is for answer. You should separate what is question and leave in these 2 fields above, and what is solution or answer, post in the field below.
– user28595
Ixi, complicated,I don’t think I really understand, I don’t have the answer, I’m putting what I imagine to tell me if I’m wrong.
– Marcelo
@Marcelo is what he’s saying... you asked and posted his vision that nothing else is an answer. Whether you are wrong or not you answered your question in the question field and not the answer.
– Leonardo Bonetti
Would be duplicate of Comments weigh? or just related?
– Woss
Forget it, leave it as it is, the community decides. If they close, you can question the reason in [meta]
– user28595
@Marcelo I think all the confusion happened because you wrote a redaction to ask something relatively simple. In my view, nothing that is after the yellow frame adds to something in the question and could be removed be content loss. See, for example, the question I quoted above. It asks practically the same thing as you - but with only 2 words.
– Woss
I find it too broad to answer, each interpreter/analyzer can work in a way, has no answer to say whether or not it is costly to all languages, just testing one by one to have some parameter, and sometimes the difference is so small that it is not worth worrying about this for performance, since many languages "compile" for some intermediate language (JIT, of course not all).
– Guilherme Nascimento
@Articuno, rsrsrs, sorry ae, really was not understanding, did not know that I should put my vision in the answers.
– Marcelo
@Marcelo without problems, I had conviction of something, but I ended up getting confused after debating with other users of the site, so I said it was to leave as it is, nor I knew if I was right or not. xD
– user28595
@Anderson Carlos Woss, regarding HTML and javascript as quoted in the link you passed I already knew that by being interpreted by the browser the more lines more cost to the user machine, it reinforces a little my doubt, but as Delphi is compiled I decided to put this question here.
– Marcelo