### Julia Docstring Example for `bar` Function Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Illustrates a basic docstring for a Julia function `bar`, explaining its purpose and parameters, including an optional `y` argument. This example demonstrates a concise docstring for a simple function. ```Julia """ bar(x[, y]) Compute the Bar index between `x` and `y`. If `y` is missing, compute the Bar index between all pairs of columns of `x`. """ function bar(x, y) ... ``` -------------------------------- ### Julia Function Type Annotation Usage Examples Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Demonstrates the flexibility of a function defined with generic type annotations. It shows the function's compatibility with different input types, such as ranges and arrays of floats, highlighting the benefits of broad type acceptance. ```Julia julia> splicer(1:10, 2) 1:2:9 julia> splicer([3.0, 5, 7, 9], 2) 2-element Array{Float64,1}: 3.0 7.0 ``` -------------------------------- ### Adding Blue Style Code Badge to README Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Instructions for adding the 'Blue Style' code badge to a project's `README.md` file, indicating adherence to the Julia Blue Style guide. ```Markdown [![Code Style: Blue](https://img.shields.io/badge/code%20style-blue-4495d1.svg)](https://github.com/JuliaDiff/BlueStyle) ``` -------------------------------- ### Sublime Text Configuration for Julia Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Recommended Sublime Text settings for Julia files. These settings can be applied by opening a `.jl` file and navigating to `Preferences > Settings - More > Syntax Specific - User`. ```JSON { "translate_tabs_to_spaces": true, "tab_size": 4, "trim_trailing_white_space_on_save": true, "ensure_newline_at_eof_on_save": true, "rulers": [92] } ``` -------------------------------- ### Julia Import Line Length and Continuation Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Provides examples of how to handle long explicit import lines in Julia. It shows preferred methods using line continuation or multiple 'using' statements, and discouraged methods that break a single 'using' statement with line breaks or use excessive separate statements. ```Julia # Yes: using AVeryLongPackage: AVeryLongType, AnotherVeryLongType, a_very_long_function, another_very_long_function ``` ```Julia # Yes: using AVeryLongPackage: AVeryLongType, AnotherVeryLongType using AVeryLongPackage: a_very_long_function, another_very_long_function ``` ```Julia # No: using AVeryLongPackage: AVeryLongType, AnotherVeryLongType, a_very_long_function, another_very_long_function ``` ```Julia # No: using AVeryLongPackage: AVeryLongType using AVeryLongPackage: AnotherVeryLongType using AVeryLongPackage: a_very_long_function using AVeryLongPackage: another_very_long_function ``` -------------------------------- ### Incorrect Julia Import Ordering Source: https://github.com/juliadiff/bluestyle/blob/master/README.md An example of incorrect import ordering, where 'using' statements are not sorted alphabetically or grouped according to style guidelines. ```Julia using B using A ``` -------------------------------- ### Importing Julia Modules Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Demonstrates the preferred way to import modules in Julia, using one 'using' statement per line and ordering them alphabetically. It explicitly shows the correct and incorrect methods for module imports. ```Julia # Yes: using A using B # No: using A, B ``` -------------------------------- ### Julia Docstring Keyword Argument Wrapping Example Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Shows the recommended formatting for wrapping long lines in docstring bullet points, specifically for keyword arguments. This ensures readability and proper indentation without aligning the wrapped text to the colon, adhering to the 92-character line limit. ```Julia """ ... # Keywords - `definition::AbstractString`: Name of the job definition to use. Defaults to the definition used within the current instance. """ ``` -------------------------------- ### Vim Configuration for Julia Development Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Recommended Vim settings for Julia development, including tab and indentation settings in `.vimrc` and Julia-specific filetype plugin configurations in `~/.vim/after/ftplugin/julia.vim` to enforce style guidelines. ```Vimscript " ~/.vim/vimrc set tabstop=4 " Set tabstops to a width of four columns. set softtabstop=4 " Determine the behaviour of TAB and BACKSPACE keys with expandtab. set shiftwidth=4 " Determine the results of >>, <<, and ==. " Identify .jl files as Julia. If using julia-vim plugin, this is redundant. autocmd BufRead,BufNewFile *.jl set filetype=julia ``` ```Vimscript " ~/.vim/after/ftplugin/julia.vim setlocal expandtab " Replace tabs with spaces. setlocal textwidth=92 " Limit lines according to Julia's CONTRIBUTING guidelines. setlocal colorcolumn=+1 " Highlight first column beyond the line limit. ``` -------------------------------- ### Julia `import` and `using` Statement Grouping Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Shows the recommended way to group 'import' and 'using' statements when both are necessary within a Julia file. They should be separated into distinct blocks with a blank line in between. ```Julia # Yes: import A: a import C using B using D: d ``` ```Julia # No: import A: a using B import C using D: d ``` -------------------------------- ### Julia Short-Form Function Definitions Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Explains when to use short-form function definitions in Julia. They should only be used when the entire definition fits on a single line for improved readability. ```Julia # Yes: foo(x::Int64) = abs(x) + 3 ``` ```Julia # No: foobar(array_data::AbstractArray{T}, item::T) where {T<:Int64} = T[ abs(x) * abs(item) + 3 for x in array_data ] ``` -------------------------------- ### Julia: Formatting NamedTuples Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Provides guidelines for `NamedTuple` syntax, including spacing around the `=` character (like keyword arguments), avoiding semicolons at the start, and using `NamedTuple()` for empty instances. Also notes the requirement for trailing commas in single-element NamedTuples. ```Julia # Yes: xy = (x=1, y=2) x = (x=1,) # Trailing comma required for correctness. x = (; kwargs...) # Semicolon required to splat correctly. # No: xy = (x = 1, y = 2) xy = (;x=1,y=2) x = (; x=1) ``` -------------------------------- ### Julia: Placing Triple-Quote/Backtick Assignments Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Dictates that the opening triple-quotes or triple-backticks for multi-line assignments should be on the same line as the assignment operator, promoting a compact and consistent style. ```Julia # Yes: str2 = """ hello world! """ # No: str2 = """ hello world! """ ``` -------------------------------- ### VS Code Configuration for Julia Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Recommended VS Code settings for Julia files, to be added to `settings.json`. These settings configure indentation, line endings, trailing whitespace, and rulers to align with Julia's style guidelines. ```JSON { "[julia]": { "editor.detectIndentation": false, "editor.insertSpaces": true, "editor.tabSize": 4, "files.insertFinalNewline": true, "files.trimFinalNewlines": true, "files.trimTrailingWhitespace": true, "editor.rulers": [92] } } ``` -------------------------------- ### Julia Docstring Template for Functions with Many Arguments (`Manager`) Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Demonstrates a docstring template for Julia functions with numerous arguments or keywords, where `args...` and `kwargs...` are used in the signature line to keep it concise. Detailed arguments and keywords are then listed in dedicated sections, improving readability for complex function signatures. ```Julia """ Manager(args...; kwargs...) -> Manager A cluster manager which spawns workers. # Arguments - `min_workers::Integer`: The minimum number of workers to spawn or an exception is thrown - `max_workers::Integer`: The requested number of workers to spawn # Keywords - `definition::AbstractString`: Name of the job definition to use. Defaults to the definition used within the current instance. - `name::AbstractString`: ... - `queue::AbstractString`: ... """ function Manager(...) ... end ``` -------------------------------- ### Julia Docstring Template for Exported Functions (`mysearch`) Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Outlines a comprehensive docstring template for exported Julia functions, including sections for arguments, keywords, returns, and throws. It demonstrates how to document parameters, return types, and potential exceptions, ensuring thorough and standardized function documentation. ```Julia """ mysearch(array::MyArray{T}, val::T; verbose=true) where {T} -> Int Searches the `array` for the `val`. For some reason we don't want to use Julia's builtin search :) # Arguments - `array::MyArray{T}`: the array to search - `val::T`: the value to search for # Keywords - `verbose::Bool=true`: print out progress details # Returns - `Int`: the index where `val` is located in the `array` # Throws - `NotFoundError`: I guess we could throw an error if `val` isn't found. """ function mysearch(array::AbstractArray{T}, val::T) where T ... end ``` -------------------------------- ### Organizing Julia Tests with Testsets Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Julia's test sets allow developers to group tests logically and can be nested. It is recommended to have a single root test set in `runtests.jl` that includes other test files. ```Julia @testset "PkgExtreme" begin include("arithmetic.jl") include("utils.jl") end ``` -------------------------------- ### Julia: Using 'in' for For Loops and Comprehensions Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Mandates the use of the `in` keyword for `for` loops and list/generator comprehensions, explicitly disallowing `=` or `∈` for iteration, ensuring consistent syntax. ```Julia # Yes for i in 1:10 #... end [foo(x) for x in xs] # No: for i = 1:10 #... end [foo(x) for x ∈ xs] ``` -------------------------------- ### Julia: Grouping Similar One-Line Statements Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Encourages grouping similar single-line statements together without blank lines in between, enhancing code density and readability for related declarations or assignments. ```Julia # Yes: foo = 1 bar = 2 baz = 3 # No: foo = 1 bar = 2 baz = 3 ``` -------------------------------- ### Manage Empty Lines in Julia Method Definitions Source: https://github.com/juliadiff/bluestyle/blob/master/README.md This section provides guidelines on using empty lines, recommending avoiding extraneous empty lines, especially between single-line method definitions, and separating functions with a single empty line. ```Julia # Yes: # Note: an empty line before the first long-form `domaths` method is optional. domaths(x::Number) = x + 5 domaths(x::Int) = x + 10 function domaths(x::String) return "A string is a one-dimensional extended object postulated in string theory." end dophilosophy() = "Why?" # No: domath(x::Number) = x + 5 domath(x::Int) = x + 10 function domath(x::String) return "A string is a one-dimensional extended object postulated in string theory." end dophilosophy() = "Why?" ``` -------------------------------- ### Julia: Using Ternary Operators Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Restricts ternary operators (`?:`) to a single line and advises against chaining multiple ternary operators. Suggests using `if`-`elseif`-`else` conditionals, dispatch, or dictionaries for complex conditional logic. ```Julia # Yes: foobar = foo == 2 ? bar : baz # No: foobar = foo == 2 ? bar : baz foobar = foo == 2 ? bar : foo == 3 ? qux : baz ``` ```Julia # Yes: foobar = if foo == 2 bar else baz end foobar = if foo == 2 bar elseif foo == 3 qux else baz end ``` -------------------------------- ### Julia Docstring for Multiple Methods of a Function (`Manager`) Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Illustrates how to document multiple methods of the same Julia function within a single docstring. This approach is suitable when the methods share a common purpose and arguments, providing a unified explanation for different call signatures and reducing redundancy. ```Julia """ Manager(max_workers; kwargs...) Manager(min_workers:max_workers; kwargs...) Manager(min_workers, max_workers; kwargs...) A cluster manager which spawns workers. # Arguments - `min_workers::Int`: The minimum number of workers to spawn or an exception is thrown - `max_workers::Int`: The requested number of workers to spawn # Keywords - `definition::AbstractString`: Name of the job definition to use. Defaults to the definition used within the current instance. - `name::AbstractString`: ... - `queue::AbstractString`: ... """ function Manager end ``` -------------------------------- ### Julia: Separating Multi-line Blocks with Blank Lines Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Recommends using blank lines to visually separate distinct multi-line code blocks, such as `if` statements and `for` loops, to improve code organization and readability. ```Julia # Yes: if foo println("Hi") end for i in 1:10 println(i) end # No: if foo println("Hi") end for i in 1:10 println(i) end ``` -------------------------------- ### Julia Comments: Explaining Intent vs. Obvious Code Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Illustrates the principle of using comments to explain the *why* or *intent* behind code, especially for non-obvious logic. This guideline advises against merely restating what the code explicitly does, thereby improving code maintainability. ```Julia # Yes: x = x + 1 # Compensate for border # No: x = x + 1 # Increment x ``` -------------------------------- ### Julia: Avoiding Blank Lines in Function Definitions Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Specifies that blank lines should not be included immediately after a function definition or directly before its `end` statement, maintaining a compact and consistent function structure. ```Julia # Yes: function foo(bar::Int64, baz::Int64) return bar + baz end # No: function foo(bar::Int64, baz::Int64) return bar + baz end # No: function foo(bar::In64, baz::Int64) return bar + baz end ``` -------------------------------- ### Avoid Alignment Spacing for Assignments in Julia Source: https://github.com/juliadiff/bluestyle/blob/master/README.md This rule advises against using multiple spaces to align assignment or other operators, promoting natural indentation over visual alignment for better maintainability. ```Julia # Yes: x = 1 y = 2 long_variable = 3 # No: x = 1 y = 2 long_variable = 3 ``` -------------------------------- ### Julia Explicit Import Ordering for Dates.Period Subtypes Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Illustrates an exception to alphabetical sorting for explicit imports. For 'Dates.Period' subtypes, a logical order (largest to smallest unit) is preferred over strict alphabetical sorting. ```Julia using Dates: Year, Month, Week, Day, Hour, Minute, Second, Millisecond ``` -------------------------------- ### Avoid Whitespace Between Unary Operators and Operands in Julia Source: https://github.com/juliadiff/bluestyle/blob/master/README.md This rule states that no whitespace should be used between a unary operator and its operand, ensuring clear and concise expressions. ```Julia # Yes: -1 [1 0 -1] # No: - 1 [1 0 - 1] # Note: evaluates to `[1 -1]` ``` -------------------------------- ### Julia Function Export Styles Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Illustrates preferred and discouraged styles for exporting functions in Julia. It emphasizes defining one export per line or grouping exports by theme, while avoiding splitting a single export over multiple lines. ```Julia # Yes: export foo export bar export qux ``` ```Julia # Yes: export get_foo, get_bar export solve_foo, solve_bar ``` ```Julia # No: export foo, bar, qux ``` -------------------------------- ### Julia: Line Breaks Between Control Flow and Returns Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Suggests using line breaks between control flow statements (like `if`) and `return` statements to improve clarity. While optional, it's a recommended practice for better visual separation. ```Julia # Yes: function foo(bar; verbose=false) if verbose println("baz") end return bar end # Ok: function foo(bar; verbose=false) if verbose println("baz") end return bar end ``` -------------------------------- ### Julia `using` vs `import` for Function Extension Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Demonstrates the preferred use of 'using' over 'import' in Julia to ensure explicit function extension. It shows the correct way to extend functions from other modules by qualifying them with the module name, and the discouraged way using 'import' for extension. ```Julia # Yes: using Example Example.hello(x::Monster) = "Aargh! It's a Monster!" Base.isreal(x::Ghost) = false ``` ```Julia # No: import Base: isreal import Example: hello hello(x::Monster) = "Aargh! It's a Monster!" isreal(x::Ghost) = false ``` -------------------------------- ### Julia Explicit Import Grouping and Sorting Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Demonstrates the recommended way to group and sort explicit 'using' imports in Julia. Imports should be grouped by modules, constants, types, macros, and functions, and sorted alphabetically within each group. A one-line alternative for fewer items is also shown. ```Julia using Example: $(sort(modules)...) using Example: $(sort(constants)...), $(sort(types)...), $(sort(macros)...), $(sort(functions)...) ``` ```Julia using Example: $(sort(modules)...), $(sort(constants)...), $(sort(types)...), $(sort(macros)...), $(sort(functions)...) ``` -------------------------------- ### Avoid Whitespace Before Commas and Semicolons in Julia Source: https://github.com/juliadiff/bluestyle/blob/master/README.md This guideline prevents extraneous spaces directly preceding commas or semicolons, ensuring consistent formatting for argument lists and statement separators. ```Julia # Yes: if x == 4 @show(x, y); x, y = y, x end # No: if x == 4 @show(x , y) ; x , y = y , x end ``` -------------------------------- ### Julia Comments: Inline vs. Block for Length Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Shows the guideline for placing comments: use inline comments only if they fit within the line length limit. If a comment cannot be fitted inline, it should be placed on the line(s) above the content it refers to, ensuring readability. ```Julia # Yes: # Number of nodes to predict. Again, an issue with the workflow order. Should be updated # after data is fetched. p = 1 # No: p = 1 # Number of nodes to predict. Again, an issue with the workflow order. Should be # updated after data is fetched. ``` -------------------------------- ### Julia Package Versioning: Avoiding Caret Specifier Source: https://github.com/juliadiff/bluestyle/blob/master/README.md Demonstrates the preferred style for specifying package version requirements in Julia. It advocates for direct version numbers without the default caret (`^`) specifier, promoting simplicity and consistency with the wider community. ```Julia # Yes: DataFrames = "0.17" # No: DataFrames = "^0.17" ``` -------------------------------- ### Standard Whitespace for Binary Operators in Julia Source: https://github.com/juliadiff/bluestyle/blob/master/README.md This guideline requires a single space on either side of most binary operators like assignment, updating, comparison, and lambda operators, while listing exceptions like range, rational, and exponentiation operators. ```Julia # Yes: i = j + 1 submitted += 1 x^2 < y # No: i=j+1 submitted +=1 x^2