1

I tried to find if someone had asked this before, and they probably have, but since the words "separated" and "spaces" were involved, I could only find questions on how to separate a string with space characters.

I guess what I mean is, when reading other people's code, I've seen lots of times that some tend to write among the lines of

a=1+(3-sqr(1/567));

or

for(int i=0;i<=limit;++i){} 

or

if(val1>=val2&&val1<=val3) {
    doSomething(arg1,arg2,arg3);
} 

I think you get the idea.

The thing is, I personally find this style harder to read. I have even found myself making a copy of the code I'm trying to read and writing the spaces just so I can read it more easily. For example, I would go from

a=1+(3-sqr(1/567));

to

a = 1 + (3 - sqr(1 / 567));

which I'm almost certain is objectively easier to read, at least easier for me. And suffice to say, I tend write my own code in this "easier to read" style.

So, am I wrong for doing this? Is it just matter of style? Or is there a bigger reason for this? Is it compiler or interpreter related? I'm sure it wouldn't hurt performance to write with separation when writing in c or similar languages since the source code will be compiled into machine code anyway. But, would it hurt performance if I did this when writing in Javascript or Python since they are interpreted on the fly? I've seen some developers write a minified version of their scripts with single character variable names and almost no separation, which is no doubt harder to read. Does this increase performance? And, even if it did, wouldn't it be worth it to write clearer since, although it would slow performance a bit, it would increase readability and therefor speed up development?

Thanks for your answers.

itiel
  • 27
  • 4
  • 3
    This is a style question, which is naturally opinionated. Your in-house rules might state the specific rules. It has zero effect on the "performance" or whatever code generated (for compiled languages). Mybe for interpreted languages there might be a very slight difference in performance, as it needs to read the source each time it is run, so reading more characters might lead to some performance difference. – Eugene Sh. Apr 19 '21 at 15:12
  • 1
    What is wrong is not to have a linter that enforce a given style across the whole codebase. Pick either style but stick with it – aureliar Apr 19 '21 at 15:13
  • As far as i know ,it doesnt matter on the performance side as a good compiler wouldnt get bothered ... – Saifeddine Ben Salem Apr 19 '21 at 15:16
  • 1
    Some people skip spaces because it makes no difference for the code and reduces the number of characters you have to type. Personally, I too find spaced code more readable and I think the time you save by skipping spaces is not worth the loss in code readability. But it is a very subjective matter (unless you have to abide to a coding style - or is it code styling? - standard enforced by your employer/team leader). – secan Apr 19 '21 at 15:16
  • Who cares, anyway your code will be minified in the end. You do minify your code, right? :) – Jeremy Thille Apr 19 '21 at 15:17
  • 1
    Consistency is more important than code style. If you're working on your own project, choose your own style but make sure you keep using that style. In a project with others, a single style for braces/spaces between operators/whatever should be decided so the code looks consistent. – mediocrevegetable1 Apr 19 '21 at 15:17
  • 1
    @secan *Some people skip spaces because it makes no difference for the code and reduces the number of characters you have to type* The amount of time spent ***thinking*** about the code you're writing should make the time to type a few spaces that add readability entirely irrelevant. If you're typing so fast that typing spaces hurts your ability to write code, ***you're typing faster than your brain is thinking***. – Andrew Henle Apr 19 '21 at 15:26
  • @AndrewHenle, I agree with the first part of your comment (as I wrote in my previous one) but not with the last sentence as it would imply that you think about the code you have to write while you are typing it, without any previous planning. ;) – secan Apr 19 '21 at 15:41

1 Answers1

0

It does not matter either way for most languages. It is a style preference. If you don't minify your code, it may take a few extra bytes to put the spaces.

awholegnuworld
  • 381
  • 1
  • 12