# Package interface best practice: More functions or more arguments?

**URL:** <https://discourse.julialang.org/t/package-interface-best-practice-more-functions-or-more-arguments/45452>\
**Category:** General Usage\
**Tags:** package\
**Created:** [August 24, 2020, 1:15pm UTC](https://discourse.julialang.org/t/package-interface-best-practice-more-functions-or-more-arguments/45452 "2020-08-24T13:15:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)\
**Post date:** [August 24, 2020, 1:15pm UTC](https://discourse.julialang.org/t/package-interface-best-practice-more-functions-or-more-arguments/45452/1 "2020-08-24T13:15:38Z")

</div>

For my package [JSONLines.jl](https://github.com/danielw2904/JSONLines.jl) I am considering a refactoring and provide 3 options to read a JSONLines file:

1. Iterator over an mmaped file. Basically returns the mmaped file at first and implements an iterator that produces the next row on each interation (can be parsed or returned as `Vector{UInt8}`)
2. Index of an mmaped file. Mmaps the file and iterates over it once saving the indices for the newlines such that rows can be accesed via `getindex` (can be parsed or returned as `Vector{UInt8}`).
3. Read and parse the whole file.

Would it be prefereable to export three different functions or one function with additional arguments specifying what version the user wants?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 24, 2020, 3:22pm UTC](https://discourse.julialang.org/t/package-interface-best-practice-more-functions-or-more-arguments/45452/2 "2020-08-24T15:22:18Z")

</div>

Separate functionality should go into separate functions. But you need at most 2.

You can have a function that returns an object that supports the iteration and abstract array protocols.

And then maybe a convenience function that just `collects` over this.

---

<div class="post-metadata">

**Author:** ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)\
**Post date:** [August 24, 2020, 4:09pm UTC](https://discourse.julialang.org/t/package-interface-best-practice-more-functions-or-more-arguments/45452/3 "2020-08-24T16:09:18Z")

</div>

Thanks for the input! The question is then in what order the operations should be performed. The “laziest” option would be to return the iterator and if the user calls `getindex` index the rows and return the appropriate row. This would make reading the file fast and the first getindex unexpectedly slow. Or break it up into multiple steps

```julia
file = File("path/to.jsonl")
file[1] # error
iterate(file, 1) # return first row
index!(file)
file[1] # return firstrow

```

In any case there are two costly operations: Indexing the rows and parsing the strings (rows). The main idea is to be able to defer both until needed.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 25, 2020, 7:57am UTC](https://discourse.julialang.org/t/package-interface-best-practice-more-functions-or-more-arguments/45452/4 "2020-08-25T07:57:06Z")

</div>

I am only mildly familiar with the format, but if you need to find line breaks sequentially anyway, then a random access API makes little sense. Just support iteration.
