Simple is better than complex

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:

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

Version 2:

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?

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

And plenty of others.

3 Likes

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

1 Like

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.

## 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.

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.

10 Likes

@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!

3 Likes

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.

1 Like

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

var x = 3

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

6 Likes

Probably that’s the cleanest solution conceptually (and of course (let ({var | (var [init-form])}*) declaration* form*) :wink:), but users find it cumbersome.

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

Filed an issue 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.

8 Likes

@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!

2 Likes