# Hi, I have just started learning. Do you have any suggestions on how to improve my code?

**URL:** <https://discourse.julialang.org/t/hi-i-have-just-started-learning-do-you-have-any-suggestions-on-how-to-improve-my-code/24237>\
**Category:** New to Julia\
**Created:** [May 15, 2019, 11:38am UTC](https://discourse.julialang.org/t/hi-i-have-just-started-learning-do-you-have-any-suggestions-on-how-to-improve-my-code/24237 "2019-05-15T11:38:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cameron\_Belcher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameron_belcher/32/8174_2.png) [@Cameron\_Belcher](https://discourse.julialang.org/u/Cameron_Belcher)\
**Post date:** [May 15, 2019, 11:38am UTC](https://discourse.julialang.org/t/hi-i-have-just-started-learning-do-you-have-any-suggestions-on-how-to-improve-my-code/24237/1 "2019-05-15T11:38:53Z")

</div>

positionX = 0  
positionY = 0

while true  
global positionX  
global positionY

println(“X: $positionX”)  
println(“Y: $positionY”)

print("Enter command: ")  
userInput = readline()

if userInput == “a”  
positionX -= 1  
elseif userInput == “d”  
positionX += 1  
elseif userInput == “s”  
positionY -= 1  
elseif userInput == “w”  
positionY += 1  
end  
end

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [May 15, 2019, 11:51am UTC](https://discourse.julialang.org/t/hi-i-have-just-started-learning-do-you-have-any-suggestions-on-how-to-improve-my-code/24237/2 "2019-05-15T11:51:11Z")

</div>

Hi and welcome to Julia 🙂 In what sense would you like to improve your code? Have you read the [manual](https://docs.julialang.org/en/v1/)? If you are after performance, I can recommend the [performance tips](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-tips-1)?

---

<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:** [May 15, 2019, 12:05pm UTC](https://discourse.julialang.org/t/hi-i-have-just-started-learning-do-you-have-any-suggestions-on-how-to-improve-my-code/24237/3 "2019-05-15T12:05:57Z")

</div>

Please [quote your code](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530).

Also, it is best to ask a more concrete question. Your code is neither optimal (because it uses globals) nor idiomatic, but may be fine for a trivial task, such as this exercise.

---

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [May 15, 2019, 4:40pm UTC](https://discourse.julialang.org/t/hi-i-have-just-started-learning-do-you-have-any-suggestions-on-how-to-improve-my-code/24237/4 "2019-05-15T16:40:30Z")

</div>

There’s nothing particularly wrong with your code, it’s just very simple.

Something you could do is split the code into functions, for example you would want a function `update_position`.

To avoid the if else you could try to use a Dictionary (e.g. `Dict("a"=>1, "d"=>-1)`).

Or better, you could try to introduce some types. For example an `Action` type and a `State` type that hold `positionX` and `positionY`, you code would then read something like:

```julia
actions = [Action("Left","a",-1), Action("Right","d",+1)]

for action in actions
    iskey(action, userInput) && update!(state, action)
end
```
