# Suggestions on code-style?

**URL:** <https://discourse.julialang.org/t/suggestions-on-code-style/30928>\
**Category:** General Usage\
**Created:** [November 10, 2019, 6:28pm UTC](https://discourse.julialang.org/t/suggestions-on-code-style/30928 "2019-11-10T18:28:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bashonubuntu](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bashonubuntu](https://discourse.julialang.org/u/bashonubuntu)\
**Post date:** [November 10, 2019, 6:28pm UTC](https://discourse.julialang.org/t/suggestions-on-code-style/30928/1 "2019-11-10T18:28:55Z")

</div>

Hello,

Below are two functions that do the same thing and I was wondering what is the more appropriate julian way to write and allocate output within these functions. In the second function, the only difference is that I am using knowledge of the output dimensions of intermediaries `(A_tmp, Z_tmp, x and y)` to pre-initialize the arrays of the appropriate sizes whereas I am not doing that in the first.

```julia
function f(App::Array{T}, Offer::Array{T}, D::Array{T}, pie::Array{S},
           N::T, J::T, K::T, L::T) where {T} where{S}

  A_tmp = permutedims(App) .* permutedims(D)
  Z_tmp = permutedims(Offer) .* permutedims(D)
  x = permutedims(pie.*Offer) .* permutedims(D)
  y = permutedims((1 .- pie).*(1 .- Offer)) .* permutedims(D)
  res = @view (prod((A_tmp .* (x .+ y)) .+ (1 .- A_tmp) .* (1 .- Z_tmp), dims = 1))[1, :, :, :]
  return res
end

function f_new(App::Array{T}, Offer::Array{T}, D::Array{T}, pie::Array{S},
               N::T, J::T, K::T, L::T) where {T} where{S}

  A_tmp = Array{T}(undef, J, L)
  Z_tmp = Array{T}(undef, J, L)
  x = Array{S}(undef, J, L)
  y = Array{S}(undef, J, L)
  res = Array{S}(undef, L, 1)

  A_tmp .= permutedims(App) .* permutedims(D) # J x L
  Z_tmp .= permutedims(Offer) .* permutedims(D) # J x L
  x .= permutedims(pie.*Offer) .* permutedims(D) # J x L
  y .= permutedims((1 .- pie).*(1 .- Offer)) .* permutedims(D) # J x L
  res .= @view (prod((A_tmp .* (x .+ y)) .+ (1 .- A_tmp) .* (1 .- Z_tmp), dims = 1))[1, :, :, :] # L x 1 (product is taken across j = 1, ..., J)
  return res
end

```

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [November 10, 2019, 7:29pm UTC](https://discourse.julialang.org/t/suggestions-on-code-style/30928/2 "2019-11-10T19:29:44Z")

</div>

The first is better.

The only reason to preallocate is if you are doing things e.g in a loop.  
Where you can thus avoid multple allocations.

Preallocating and only using once, just makes the code harder to read,  
and maybe marginally slower.

---

<div class="post-metadata">

**Author:** ![bashonubuntu](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bashonubuntu](https://discourse.julialang.org/u/bashonubuntu)\
**Post date:** [November 10, 2019, 7:44pm UTC](https://discourse.julialang.org/t/suggestions-on-code-style/30928/3 "2019-11-10T19:44:12Z")

</div>

Thanks for the reply, although ultimately f\_new will be nested in a for loop (which I’ll parallelize over). So, there will be multiple calls to f\_new, once for each index in the outer for loop.
