Posts

Showing posts from October, 2015

Prefer Swift's for-in loop over traditional C for loop

I'm sure most of us started our first programming adventures in C. How many timesI'm sure most of us started our first programming adventures in C.How many times have you writtenfor (int i = 0; i < blah; i++) or something similar in countless methods, functions, etc.?In C, C++, Javascript, Java, it's ubiquitous.Then I came upon the most glorious programming language ever created, Rubyfor index in 0..5 puts "Value of local variable is #{i}" end This is obviously equivalent to:for (int index = 0; i <= 5; i++) The ease of reading should be obvious in Ruby's typical verbosity. Not that any programmer worth his/her salt can't immediately decipher a traditional for loop.The idea though is to flow code in a way we read natural language.So it was no suprise that Swift adopted this for-in loop pattern, and in all cases you should prefer to use this format over the C for loop.Here's Swift's for-infor index in 0...5 { print("current in…