-10

I want to know: is there impact of any kind (for example memory related issues) when I have empty lines between the code blocks in a java class?

GhostCat
  • 137,827
  • 25
  • 176
  • 248
Pand005
  • 1,095
  • 3
  • 23
  • 53
  • No impact on the code/binary at all. But enough empty lines can make your source code's size hit the limit :-) – ernest_k Jun 22 '18 at 12:11
  • 1
    Each empty line will use one (or 2) more bytes on your hard drive. That's pretty much all there is to it... The JVM doesn't execute your java file, it executes a compiled file which ignores your code formatting. – assylias Jun 22 '18 at 12:11
  • NO ........ ... – Deb Jun 22 '18 at 12:11
  • 1
    [SoftwareEngineering SE: Is too much whitespace a bad thing](https://softwareengineering.stackexchange.com/questions/251216/is-too-much-whitespace-a-bad-thing) – Malte Hartwig Jun 22 '18 at 12:13
  • 1
    For the record: yes, this is a very basic question, but it has a **clear** specific scope. So closing it for "too broad" doesn't make sense. Also note that the answers given are clear and specific (disclaimer: I wrote one). I think this question should neither be put on hold, nor deleted. – GhostCat Jun 22 '18 at 14:16

3 Answers3

15

Not at all.

Empty lines do not matter at runtime, as the java compiler turns your source code into bytecode in the very first place. The only "consequence" of using empty lines will be the line number information that the compiler can include in the bytecode files. More empty lines, resulting in higher numbers. But that is really of no concern at all.

From that point of view, you strive to use vertical spacing for one purpose, and one purpose only: to communicate intent to human readers of your code. You use empty lines to "group" things that belong together. Of course, you use them with care: too many empty lines don't help with readability a bit. You don't want the reader to scroll around when there is not need to do so.

And just to add the concern by Ernest: yeah, theoretically you could add so many empty lines that your methods get too long/big ( see here). But well, that seems to be a limit for byte code size. As said, no empty lines in byte code, so not even a theoretical problem with that.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 1
    Things are come back as they should ! Great my dear :) – davidxxx Jun 29 '18 at 20:04
  • 1
    It is unfortunate that an undelete doesn't allow to vote again ! Otherwise I would do it again :) PS : No need to upvote for mine as the limitation was reached. But thanks any way :) – davidxxx Jun 29 '18 at 20:06
7

There is no impact on the compiled code, but having too much (or too little) whitespace may make your code harder to read.

For example, put blank lines between methods, and you can use it to make 'paragraphs' in your code. Where the paragraph contains closely related code, this can help readability (and it can also help when refactoring a single method into multiple methods).

However overdoing that (eg using a lot of blank lines or using blank lines between each line of code) can actually make it harder to read.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Not sure why no one is mentioning that there's a limit to source file size, which can be reached with enough empty lines... – ernest_k Jun 22 '18 at 12:16
  • @ErnestKiwele Because that is hardly relevant, you have to be writing monsters of methods to hit those limits, and the few bytes involved for adding linebreaks hardly count towards that limit. – Mark Rotteveel Jun 22 '18 at 12:18
  • 1
    @ErnestKiwele Nonetheless I added your concern to *my* answer ;-) – GhostCat Jun 22 '18 at 12:19
  • @ErnestKiwele And the only limit I'm specifically aware of is the 64kilobyte **bytecode** length of a method or initializer, whitespace like linebreaks have no effect on that. – Mark Rotteveel Jun 22 '18 at 12:21
3

The only impact is for us

Your java code will be compiled in bytecode, i.e. a list of simple computation, ignoring whitespaces and empty lines.

However, that doesn't mean that tabs, spaces and empty lines are useles: they improve a lot code readability, so it's a good practice to indent code, and make some block using empty lines

Kepotx
  • 1,095
  • 12
  • 26