Reputation: 14551
What are the basic structural aspects that should exist in Julia code I am writing? I will link some other languages' implementation of this for reference.
I am looking for officially sanctioned components for the language itself, not one's opinion on best practices.
Basic structure of a C program
Basic structure of a Java program
Upvotes: 7
Views: 3031
Reputation: 6086
Larger Julia programs tend to first have modules that are included, then any const declaration of const global variables, then one or more functions, then a statement that calls the first function run.
However, your question may be due to the fact that you already know that C and Java have a certain structure mandated in order to compile and run.
Perhaps comparing a "Hello, World!" minimal programs would help.
Here is a Julia minimal program:
println("Hello, World!")
Julia its inherits its free program structuring more from Fortran and especially Python than C or Java. Here is a Python minimal program:
print("Hello, World!")
Here is a Fortran77 minimal program:
print *, "Hello, World!"
On the other hand, C requires, for the same function, a main() wrapper function and inclusion of code for printing to screen. Here is a C minimal program:
#include <stdio.h>
int main()
{
return printf("Hello, World!\n");
}
Java also requires that its main() function be wrapped within a user defined class. Here is a Java minimal program:
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello, World!");
}
}
So, Julia simply does not have certain structural requirements that you find well documented for the C or Java languages.
Upvotes: 6
Reputation: 14551
First off, if you are new to Julia and the structure of writing a program therein, I suggest you check out the Official Julia Docs for a great explanation as to how to get started writing code in Julia.
If you are coming from a language like Java and read the documentation linked above, you may be a little confused as to where the docs on the structure of a Julia program are. To my understanding, that doc does not exist for a few reasons.
Julia as a language imposes very little on you as the programmer. This can lead to a bit of uncertainty and doubt with all of the newfound freedom. There are some basic structures that should be followed, despite the flexibility the language provides:
using
and import
statements are generally made at the very top of the file. It's also worth checking out the Julia Style Guide for things like:
Write functions, not just scripts: Writing code as a series of steps at the top level is a quick way to get started solving a problem, but you should try to divide a program into functions as soon as possible. Functions are more reusable and testable, and clarify what steps are being done and what their inputs and outputs are. Furthermore, code inside functions tends to run much faster than the top-level code, due to how Julia's compiler works.
In general, Julia is flexible. There are very few things that you have to include in your program.
It's important to designate the difference between writing a simple Julia script and creating a project in Julia.
While there are limited structural suggestions for creating a simple script, there are a number of suggestions related to how one can structure a Julia Project. In fact, many of these aspects are built into Julia itself! You can find out more about creating Julia Projects (which should have a similar if not the same structure as Julia packages) here.
Note: If you are trying to find the structure of a Package in Julia, a great resource would be PackageTemplate.jl.
Upvotes: 7