# Better design pattern for this type of development..?

**URL:** <https://discourse.julialang.org/t/better-design-pattern-for-this-type-of-development/44497>\
**Category:** General Usage\
**Tags:** design-pattern\
**Created:** [August 7, 2020, 1:03pm UTC](https://discourse.julialang.org/t/better-design-pattern-for-this-type-of-development/44497 "2020-08-07T13:03:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [August 7, 2020, 1:03pm UTC](https://discourse.julialang.org/t/better-design-pattern-for-this-type-of-development/44497/1 "2020-08-07T13:03:51Z")

</div>

I have a lot of Julia code that goes something like this:

```julia
const data = DataFrame!(CSV.File("path/to/some/file.csv"))

function foo(data)
    for row in eachrow(data)
        # do some stuff 
    end
    return # some interesting result
end

function bar(data)
    a = [findfirst(x -> x == value, data.column_C), :column_A]
    # ... #
    return # some interesting result
end

```

Basically, I have some data source that I’m working with that I assign to a `const` variable in the global scope and then I generally have numerous functions that manipulate that data in some way and return results.

Is this the best way to do this kind of work or is there a design pattern that makes more sense? The data files aren’t always CSV files and are sometimes quite large so I typically only want to load them once.

---

<div class="post-metadata">

**Author:** ![lostella](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lostella/32/356_2.png) [@lostella](https://discourse.julialang.org/u/lostella)\
**Post date:** [August 7, 2020, 1:11pm UTC](https://discourse.julialang.org/t/better-design-pattern-for-this-type-of-development/44497/2 "2020-08-07T13:11:42Z")

</div>

It’s hard to say without knowing what you need these functions to do, and how much flexibility you need in using them. But my first guess is that your `foo` and `bar` functions could take `data` as input argument (of DataFrame type), potentially together with other options on how to operate on it (like column names and such).

An immediate advantage of this is: if you need to operate on two dataframes you can just invoke `foo` on each of them (same for `bar`).

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [August 7, 2020, 1:13pm UTC](https://discourse.julialang.org/t/better-design-pattern-for-this-type-of-development/44497/3 "2020-08-07T13:13:47Z")

</div>

> [@lostella](#):
>
> But my first guess is that your `foo` and `bar` functions could take `data` as input argument (of DataFrame type), potentially together with other options on how to operate on it (like column names and such).

I should have clarified - what you are describing is often what I do as many times I have more than one data source (edited the OP to reflect this). What I’m really asking about is assigning the data to a variable in the global scope…

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [August 7, 2020, 1:13pm UTC](https://discourse.julialang.org/t/better-design-pattern-for-this-type-of-development/44497/4 "2020-08-07T13:13:59Z")

</div>

That’s how I usually do it, I generally have `data_transform(data; options...)` or `data_transform!(data; options)` when whatever I’m doing changes the raw data in the process (e.g. adding columns).

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [August 7, 2020, 1:15pm UTC](https://discourse.julialang.org/t/better-design-pattern-for-this-type-of-development/44497/5 "2020-08-07T13:15:53Z")

</div>

> [@nilshg](#):
>
> That’s how I usually do it

I’m in good company then - this makes me feel better about it 🙂

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [August 7, 2020, 1:18pm UTC](https://discourse.julialang.org/t/better-design-pattern-for-this-type-of-development/44497/6 "2020-08-07T13:18:53Z")

</div>

I think this seems good, but I think its important to be consistent between modifying functions `!` and copying functions.

Do you want to do Stata or R, basically. I think both have merits.
