<p>Java-family languages often use a <code>package</code> declaration at the top of a source file, to group source files into modules/packages independently of their file system layout. Python, however, uses the file system layout to infer what files are in a module. What are the pros and cons of these options?</p>

<python><java><modules>
What are the advantages and disadvantages of package declarations?
<p>Disadvantage:</p>
<ul>
<li>If you know where the source root is, this is a redundant information, while users will still have to fill it in every time they write a file.</li>
<li>Moving a file from one package to another requires modifying the file itself, which can cause unnecessary diff in source control (e.g. Git).</li>
<li>Cannot paste such a file directly from web to a local project, because one needs to change the package declaration. However, this is handled well in modern tools like IntelliJ.</li>
</ul>
<p>Advantage:</p>
<ul>
<li>Whenever you find a Java file, you may use this declaration to find the source root, and then find the other files. This is good for making clever IDEs and editors.</li>
<li>For beginners, it is clear that where should this file be located in.</li>
<li>If we see an unimported symbol in a file, we may immediately know that it should be from the same package as declared at the top of the file, without looking at any other file. This improves the information locality in the compiler.</li>
</ul>

CC BY-SA 4.0
2023-05-17T00:31:03.990Z
2023-05-17T00:31:03.990Z
ice1000
CC BY-SA 4.0
2023-05-26T16:29:19.777Z
The last point in advantages doesn't require you package declaration. If your language has packages implicitly from the folder hierarchy you can still have this feature.
Aiono
CC BY-SA 4.0
2023-05-26T19:22:15.103Z
@Aiono but implicit imports from the same package will not be added automatically
ice1000
CC BY-SA 4.0
2023-05-26T22:20:09.073Z
I don't understand, if you have a symbol in the same package you can use it without specifying an import just like you would do with package declarations.
Aiono
CC BY-SA 4.0
2023-05-27T00:53:36.513Z
@Aiono I'm confused. Isn't the question about the advantages and disadvantages of explicit package declarations? It didn't mention it anyway.
ice1000
# What are the advantages and disadvantages of package declarations?
Posted by: username_0 on 2023-05-17
Java-family languages often use a `package` declaration at the top of a source file, to group source files into modules/packages independently of their file system layout. Python, however, uses the file system layout to infer what files are in a module. What are the pros and cons of these options?
**Tags:** python java modules
---
## 1 answers
### Answer by username_1 on 2023-05-17. Score: 6
Disadvantage:

* If you know where the source root is, this is a redundant information, while users will still have to fill it in every time they write a file.
* Moving a file from one package to another requires modifying the file itself, which can cause unnecessary diff in source control (e.g. Git).
* Cannot paste such a file directly from web to a local project, because one needs to change the package declaration. However, this is handled well in modern tools like IntelliJ.

Advantage:

* Whenever you find a Java file, you may use this declaration to find the source root, and then find the other files. This is good for making clever IDEs and editors.
* For beginners, it is clear that where should this file be located in.
* If we see an unimported symbol in a file, we may immediately know that it should be from the same package as declared at the top of the file, without looking at any other file. This improves the information locality in the compiler.
> **Comments:**
> **username_2** (2023-05-26): The last point in advantages doesn't require you package declaration. If your language has packages implicitly from the folder hierarchy you can still have this feature.  
> **username_1** (2023-05-26): @Aiono but implicit imports from the same package will not be added automatically  
> **username_2** (2023-05-26): I don't understand, if you have a symbol in the same package you can use it without specifying an import just like you would do with package declarations.  
> **username_1** (2023-05-27): @Aiono I'm confused. Isn't the question about the advantages and disadvantages of explicit package declarations? It didn't mention it anyway.  
---
