# Simple is better than complex

**URL:** <https://discourse.julialang.org/t/simple-is-better-than-complex/18821>\
**Category:** General Usage\
**Tags:** question, proposal\
**Created:** [December 19, 2018, 6:06am UTC](https://discourse.julialang.org/t/simple-is-better-than-complex/18821 "2018-12-19T06:06:47Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhangliye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhangliye/32/3208_2.png) [@zhangliye](https://discourse.julialang.org/u/zhangliye)\
**Post date:** [December 19, 2018, 6:06am UTC](https://discourse.julialang.org/t/simple-is-better-than-complex/18821/1 "2018-12-19T06:06:48Z")

</div>

For the following two version of Julia code, the first version is much simple and beautiful. However, it no longer works in Julia 1.  
Version 1:

```julia
a = 0 # as simple as Python code 
for i in 1:10
	a +=1
end
@show a

```

Version 2:

```julia
global a = 0 # The compiler should judge the global variable
for i in 1:10
	global a # seems tedious, should be the compiler's work 
	a +=1
end
@show a

```

I know when put the code in a function it works in Julia 1. However, when using Jupyter we usually do not put every thing in the function, because global variables from different cells are very convenient.

Is it possible to make the simple version code run in the future version?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [December 19, 2018, 6:19am UTC](https://discourse.julialang.org/t/simple-is-better-than-complex/18821/2 "2018-12-19T06:19:10Z")

</div>

Please search this forum and the issues on github - there has been A LOT of discussion about this, for example these threads

> [@Another possible solution to the global scope debacle](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894):
>
> The infamous issue [https://github.com/JuliaLang/julia/issues/28789](https://github.com/JuliaLang/julia/issues/28789) has been on most of our minds for a while now. Although I’ve stopped posting there, I can tell you I’ve been thinking about it a lot, as have Stefan and Jameson and others, and we have continued discussing it off and on. We should really try to get it right. Last time around, we more-or-less settled on changing only the REPL, which has the main advantage of being non-breaking for most code. But after sleeping on it, it does not …

> [@New scope solution](https://discourse.julialang.org/t/new-scope-solution/16707):
>
> I have an alternate, non-breaking solution to the scope issue previously discussed on [discourse](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894) and [github](https://github.com/JuliaLang/julia/issues/28789). The new potential solution is the following rule to decide whether a variable, x, which is not marked as explicitly local or glogal and which is assigned to in a top-level scope construct is local or global: x is global if it is accessed before being assigned on all paths through the scope body. In other words, x is local unless there’s no way it could make sense for it to be local. Or…

> <https://github.com/JuliaLang/julia/issues/28789>
>
> \### Example 1
> 
> This came up with a student who upgraded from 0.6 to 1.0 direct…ly, so never even got a chance to see a deprecation warning, let alone find an explanation for new behavior:
> 
> \`\`\`julia
> julia\> beforefor = true
> true
> 
> julia\> for i in 1:2
> beforefor = false
> end
> 
> julia\> beforefor # this is surprising bit
> true
> 
> julia\> beforeif = true
> true
> 
> julia\> if 1 == 1
> beforeif = false
> end
> false
> 
> julia\> beforeif # Another surprise!
> false
> 
> julia\> function foo()
> infunc = true
> for i in 1:10
> infunc = false
> end
> @show infunc
> end
> foo (generic function with 1 method)
> 
> julia\> foo() # "I don't get this"
> infunc = false 
> \`\`\`
> 
> \### Example 2
> 
> \`\`\`julia
> julia\> total\_lines = 0
> 0
> 
> julia\> list\_of\_files = \["a", "b", "c"\]
> 3-element Array{String,1}:
> "a"
> "b"
> "c"
> 
> julia\> for file in list\_of\_files
> # fake read file
> lines\_in\_file = 5
> total\_lines += lines\_in\_file
> end
> ERROR: UndefVarError: total\_lines not defined
> Stacktrace:
> \[1\] top-level scope at ./REPL\[3\]:4 \[inlined\]
> \[2\] top-level scope at ./none:0
> 
> julia\> total\_lines # This crushs the students willingness to learn
> 0
> \`\`\`
> 
> I "get" why this happens in the sense that I think I can explain, with sufficient reference to the arcana in the manual about what introduces scopes and what doesn't, but I think that this is problematic for interactive use.
> 
> In example one, you get a silent failure. In example two, you get an error message that is very there-is-no-spoon. Thats roughly comparable to some Python code I wrote in a notebook at work today.
> 
> I'm not sure what the rules are in Python, but I do know that generally you can't assign to things at the global scope without invoking global. But at the REPL it does work, presumably because at the REPL the rules are different or the same logic as if they were all are in the scope of function is applied.
> 
> I can't language-lawyer the rules enough to propose the concrete change I would like, and based on Slack this isn't even necessarily perceived as an issue by some people, so I don't know where to go with this except to flag it.
> 
> Cross-refs:
> \#19324
> https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514
> https://stackoverflow.com/questions/51930537/scope-of-variables-in-julia

And plenty of others.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [December 19, 2018, 6:21am UTC](https://discourse.julialang.org/t/simple-is-better-than-complex/18821/3 "2018-12-19T06:21:03Z")

</div>

Also note that both your examples are currently working in IJulia, since it uses Steven’s SoftGlobalScope.jl by default.

---

<div class="post-metadata">

**Author:** ![zhangliye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhangliye/32/3208_2.png) [@zhangliye](https://discourse.julialang.org/u/zhangliye)\
**Post date:** [December 19, 2018, 6:32am UTC](https://discourse.julialang.org/t/simple-is-better-than-complex/18821/4 "2018-12-19T06:32:42Z")

</div>

Thanks for your kind reminding! The example code works in IJulia. However the following code does not work in IJulia which seems very odd. This is the reason why I re-tested the simple code in console environment.

```julia
## check the OD trip of different months 
csv_file = get_month_file(files, 5) #trip of May
isfile(csv_file) || error("Trip file not exist. $csv_file") 
    
frm = loadtable(csv_file)  
frm = filter(p->p.start_station_id!=p.end_station_id, frm) # OD should be different station

frm_wk = filter(p -> 1<=dayofweek( p.starttime )<=5, frm) # weekday trips
frm_wkend = filter(p -> 6<=dayofweek( p.starttime )<=7, frm) # weekend trips

# extract daily matrix / week and weekend 
d_wk = 0 
d_wkend = 0
min_d = day( minimum( select(frm, :starttime) ) )
max_d = day( maximum( select(frm, :starttime) ) )
for d in min_d:max_d 
    tb = filter(x->day(x)==d, frm_wk, select=:starttime)
    length(tb)>0 && (d_wk+=1)
    
    tb = filter(x->day(x)==d, frm_wkend, select=:starttime)
    length(tb)>0 && (d_wkend+=1)    
end
@show d_wk
@show d_wkend

```

The error is as follows.

 ![Capture](https://global.discourse-cdn.com/julialang/original/3X/f/5/f51a8bf2ccd11412ae241b1e86263756fd930a5c.png)

---

<div class="post-metadata">

**Author:** ![jeff.bezanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff.bezanson/32/48_2.png) [@jeff.bezanson](https://discourse.julialang.org/u/jeff.bezanson)\
**Post date:** [December 19, 2018, 6:45am UTC](https://discourse.julialang.org/t/simple-is-better-than-complex/18821/5 "2018-12-19T06:45:08Z")

</div>

I’m not sure what’s happening there; that should perhaps be filed as an issue with SoftGlobalScope.jl.

I agree that simple is better than complex, but please remember that simple is not the same thing as easy. Easy is what you’re used to or expect. Simple is having a short formal description. These are often in conflict. For example it would be very simple to say that every new variable must be introduced with a keyword like `var` as in some languages. It would be immediately clear what each assignment does. But half the lines in your program would begin with `var`, which I believe most julia programmers don’t want.

It has proven fairly difficult to come up with rules that generally “do what we want” but are also simple.

---

<div class="post-metadata">

**Author:** ![zhangliye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhangliye/32/3208_2.png) [@zhangliye](https://discourse.julialang.org/u/zhangliye)\
**Post date:** [December 19, 2018, 6:58am UTC](https://discourse.julialang.org/t/simple-is-better-than-complex/18821/6 "2018-12-19T06:58:04Z")

</div>

@jeff.bezanson Hi, Jeff, Thanks for your excellent work on Julia language. I totally agree with your viewpoint. Simple and good-performance is usually more challenging than Complex and Good-performance.

Julia is nearly Simple and Good-performance compared with Python. I like the clean code and high-performance of Julia. Thanks for your great work!

---

<div class="post-metadata">

**Author:** ![Steven\_Sagaert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steven_sagaert/32/29578_2.png) [@Steven\_Sagaert](https://discourse.julialang.org/u/Steven_Sagaert)\
**Post date:** [December 19, 2018, 3:53pm UTC](https://discourse.julialang.org/t/simple-is-better-than-complex/18821/7 "2018-12-19T15:53:31Z")

</div>

Given how difficult it has been to come up with a good solution for the scoping issue that everybody can agree on I think starting every variable declaration with var/val (for mutable/immutable assignment) like some languages is probably the best solution. Unfortunately this wasn’t done before 1.0 and since it’s a breaking change it’s probably too late now.

---

<div class="post-metadata">

**Author:** ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)\
**Post date:** [December 19, 2018, 5:33pm UTC](https://discourse.julialang.org/t/simple-is-better-than-complex/18821/8 "2018-12-19T17:33:47Z")

</div>

> [@Steven\_Sagaert](#):
>
> I think starting every variable declaration with var/val (for mutable/immutable assignment) like some languages is probably the best solution.

Did I understand you correctly that you would prefer explicit variable definition? Like this:

```julia
var x = 3

```

I have a feeling that you belong a very small minority in this forum.

---

<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:** [December 19, 2018, 5:48pm UTC](https://discourse.julialang.org/t/simple-is-better-than-complex/18821/9 "2018-12-19T17:48:16Z")

</div>

> [@Steven\_Sagaert](#):
>
> starting every variable declaration with var/val

Probably that’s the cleanest solution conceptually (and of course [`(let ({var | (var [init-form])}*) declaration* form*)`](http://www.lispworks.com/documentation/HyperSpec/Body/s_let_l.htm) 😉), but users find it cumbersome.

Modern interactive languages have been making an effort to avoid explicit variable declarations.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 19, 2018, 6:44pm UTC](https://discourse.julialang.org/t/simple-is-better-than-complex/18821/10 "2018-12-19T18:44:59Z")

</div>

> [@zhangliye](#):
>
> However the following code does not work in IJulia which seems very odd.

Filed [an issue](https://github.com/stevengj/SoftGlobalScope.jl/issues/18) with a simplified example, thanks.

_Update_: Now fixed in master, and v1.0.8 with the fix is tagged and will be available for Pkg `update` shortly.

---

<div class="post-metadata">

**Author:** ![zhangliye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhangliye/32/3208_2.png) [@zhangliye](https://discourse.julialang.org/u/zhangliye)\
**Post date:** [December 20, 2018, 12:56am UTC](https://discourse.julialang.org/t/simple-is-better-than-complex/18821/11 "2018-12-20T00:56:10Z")

</div>

@stevengj Thank you so much for your quick response! IJulia and PyPlot are two packages I used almost every day. Thanks for your work! Happy Christmas!
