# How to optimize code without repeating similar part, based on the type of input argument?

**URL:** <https://discourse.julialang.org/t/how-to-optimize-code-without-repeating-similar-part-based-on-the-type-of-input-argument/38149>\
**Category:** New to Julia\
**Created:** [April 24, 2020, 2:41pm UTC](https://discourse.julialang.org/t/how-to-optimize-code-without-repeating-similar-part-based-on-the-type-of-input-argument/38149 "2020-04-24T14:41:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![QUANG\_VU\_NGUY\_N\_PH\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quang_vu_nguy_n_ph_m/32/14299_2.png) [@QUANG\_VU\_NGUY\_N\_PH\_M](https://discourse.julialang.org/u/QUANG_VU_NGUY_N_PH_M)\
**Post date:** [April 24, 2020, 2:41pm UTC](https://discourse.julialang.org/t/how-to-optimize-code-without-repeating-similar-part-based-on-the-type-of-input-argument/38149/1 "2020-04-24T14:41:54Z")

</div>

I have these 4 functions, which are almost similar. Based on the type of input argument, different Gaussline(i, j) will be given.

Is there any way to shorten this code?

```julia
function create_gauss_points(::Type{Bar2})
    gauss_line = GaussLine(2,1)
    gauss_points = [GaussPoint(i...) for i in gauss_line]
    return gauss_points
end

function create_gauss_points(::Type{Bar3})
    gauss_line = GaussLine(3,1)
    gauss_points = [GaussPoint(i...) for i in gauss_line]
    return gauss_points
end

function create_gauss_points(::Type{Quad4})
    gauss_line = GaussLine(2, 2)
    gauss_points = [GaussPoint(i...) for i in gauss_line]
    return gauss_points
end

function create_gauss_points(::Type{Quad8})
    gauss_line = GaussLine(3,2)
    gauss_points = [GaussPoint(i...) for i in gauss_line]
    return gauss_points
end

```

Thank you for your help!

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [April 24, 2020, 3:04pm UTC](https://discourse.julialang.org/t/how-to-optimize-code-without-repeating-similar-part-based-on-the-type-of-input-argument/38149/2 "2020-04-24T15:04:26Z")

</div>

Make the arguments to `GaussLine` type parameters?

```julia
struct Element{A,B}
  name
  ...
end
function create_gauss_points(::Element{A,B}) where {A,B}
  gauss_line = GaussLine(A,B)
...
end

```

---

<div class="post-metadata">

**Author:** ![QUANG\_VU\_NGUY\_N\_PH\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quang_vu_nguy_n_ph_m/32/14299_2.png) [@QUANG\_VU\_NGUY\_N\_PH\_M](https://discourse.julialang.org/u/QUANG_VU_NGUY_N_PH_M)\
**Post date:** [April 24, 2020, 3:12pm UTC](https://discourse.julialang.org/t/how-to-optimize-code-without-repeating-similar-part-based-on-the-type-of-input-argument/38149/3 "2020-04-24T15:12:37Z")

</div>

I really appreciate your reply. This is really helpful!!
