I often find people curious about religion in general and concept of God in particular. Being a Muslim I think its an obligation for me to make the knowledge available for others and help them find it if they want to learn about Islam.
Instead of writing a long philosophical theory I’ll just summarize my view in points so it can be read quickly and understood easily.
Disclaimer:Please note that I’m not an expert in the area of religion (Islam) and following is solely my own understanding of God.
God is the entity that created the entire universe, all the mechanisms in it, space, time, black wholes, the surface behind the black whole, laws of physics, gravity, nature, animals.
God is absolute, eternal and single creator of everything.
God created science. Science explains how God automated different procedures and how he designed the universe. Science is an explanation of God’s design.
This entire universe is the area that God has confined us in. Our logic, brain, intelligence, knowledge, technology can not comprehend anything outside the dimensions that we exist in. We can not understand, see or feel God because he is not part of this world.
God doesn’t have gender, partners, relatives. These are all humanly attributes. God created us, he doesn’t have to be like us to be our God. It is us who need to have a beginning and an end. It is us who have life or death. It is us who have father, mother, wife, children, siblings. It is us who are male, female. God in our system can only be defined as a concept. We can not give any attributes to God because our attributes are limited to our own system in which we are confined. God is not part of this system. He is the creator of the system.
We don’t know why God created this universe but we do know that our life is a test. He has sent many messengers on earth with messages from him to guide us about the rules of the game. If we do as he says, we’ll be rewarded and if we don’t we’ll be punished. However all the messengers had a simple message that we’re supposed to worship him, enjoy all his bounties on earth, promote equality, justice, peace and stay away from all the things that are evil/harmful.
When we worship God i.e. offer prayers, we are not doing any favor to God. God doesn’t need us to be who he is. Whether we believe in his existence or don’t it doesn’t affect him in anyway.
When we believe in God and obey him, its only for our own benefit. Because the system that God designed has rules. If we follow the rules we’re rewarded.
God gave us free will to do anything we want. To believe in him or not. To obey him or not. It is for us to decide what we want to do with our life on earth and the life after we die.
God says in Quran
“Praise be to Allah. He will show you His Signs and you will recognise them. Your Lord is not heedless of anything you do.” (Surat an-Naml: 93)
This means that God encourages to explore the world. Religion is not about closing your eyes and fooling yourself into belief without believing. God or God’s religion doesn’t stop us from studying science, learning physics, maths, chemistry. In fact if we study deep enough we will see the signs that indeed the perfection in the entire automated system is due to involvement of an entity which is beyond our universe.
“A little knowledge of science makes man an atheist, but an in-depth study of science makes him a believer in God.” ~ Francis Bacon
None the less to explore the world and look for signs of God using reason, logic and science is not an easy endeavor and so for the rest of us there is Quran. Which God has created as a guidance that can easily be understood by common man.
God doesn’t want us to be in the world but go to a jungle and live a life totally dedicated to offering prayers. Angels are already doing that. They pray all the time. Human is a unique creation of God. We have to stay connected with the world ‘and’ offer prayers and abide by the laws created by him.
Its up to us to make an investment in our future by spending time and learning about God and religion. If we do make an effort we can easily find faith. If we don’t make an effort and instead declare ourselves disbelievers in God and his religion then we’re not affecting God in anyway. We’re just being careless about our future. God will never come down on earth for us to believe in him. If we don’t make an effort to find God, God will not do a wonder and prove his existence. He doesn’t have to. To him it doesn’t matter what we do or not do.
God has created all the rules but he can choose to send us to heaven even if we’ve not been totally pious. He can make exceptions to send people to heave or to reward them with bounties but he will never make an exception to be unfair with a person. God will never punish us for something we did not do but he can reward us for something we only thought of but never did.
God is very forgiving. If we’ve been ignorant in our life but we finally repent and ask for forgiveness, there is a very good chance he will forgive us if we start over and start living by his laws of being away from everything bad, enjoying everything that is good, offering our prayers, taking care of our relatives and neighbors and fellow human beings.
When we say that God created us, it doesn’t necessarily mean that God has to be physically involved in creating us. First of all the word ‘physically’ relates with ‘physics’ in which there has to be motion, etc. Like I said earlier physics and all other rules of this universe are designed for us. God doesn’t have to be explained in our terms how he accomplishes things. God is a designer. He designs things to happen. He wants something to happen and so it happens. Not because he has to ask someone to do it for him because he is not involved in the process but in fact because the process is his creation. Everything is automatic. Every human is created in his mother’s womb which is our way of explaining it in our worldly knowledge however the creation of human in mother’s womb is a God’s design. This is how God creates us. Science is one way of describing how God wants/designs things to happen. It doesn’t mean that God isn’t doing it.
Because everything by definition of previous point is a creation of God then it doesn’t mean that God is busy all the time because he has so many things to take care of. In fact that thought would be foolish because it again uses the knowledge of world and limitation of our dimensions and capabilities to understand God. God is by definition creator of everything and everything has a process. God doesn’t have to ‘do’ things for them to be ‘done’. God is not busy. The word busy is for us when we can’t handle more than a given set of tasks. God’s capabilities are infinite and undefined in our world.
So to summarize all the points above. You can not take the knowledge of how our world works to try to explain or understand God. Because we are confined in space, time and dimension and our logic, reasoning, knowledge and science is only applicable to us not God. It is for us to invest in our future and learn Quran, understand religion and live according to the rules of the book to be successful not only in here after but also in this world by living a peaceful, clean and pious life.
Squiggle 3.1 has just been released. This is mostly a maintenance release and has only minor bug fixes. No new features.
Since it is compatible with 3.0 it is perfectly fine for some people in the network to be on 3.0 and others to be on 3.1.
So a slow and gradual upgrade should work.
In next release I’ll try to implement some of the features from Issue Tracker
You can get the latest release from Download page
For most part of my career I’ve worked in C# and I love the language because of its sensible syntax and the fact that it constantly improves and takes only the good things from other languages and discards all the nonsense. Constantly working in C# made me so used to writing object oriented code that I would just not look at a language that allows writing any kind of code ‘outside’ a class.
I’m just not comfortable with the idea that some code can be hanging in the ‘air’. It takes me back to the time when I used to write code in QBasic and had no idea of what a class is and I would write public static methods in a module. I definitely don’t want to experience that again since writing that kind of code is unmaintainable, unintuitive and unexplainabily horrible.
But when I first got to know about Javascript it was only used for simple things like form validation, flashing things and opening popups, basically mostly childsplay and the fact that it had no keyword ‘class’ were good enough reasons to keep me away from web scripting (or even development). I’d prefer working on desktop applications or middleware services.
At some point I discovered that you can write object oriented code in Javascript and the way to do that is
When I saw that the way to declare a class like thing is by starting with the keyword ‘function’, I didn’t dig deeper into it. Since this was a retard concept to me that a function is a class and a class is a function and the fact that the declaration of both is same.
Specially when I joined Hyves there could be no way for me to just avoid it. And because I had never done OOP in javascript I wrote some crappy javascript code and then washed my hands but I didn’t feel bad about it because I thought in the back of my mind that “hey its stinking javascript, it’s got to stink”.
Then I saw some similar in function (not in style) code written by my colleague Ifthikhan. Who did write object oriented code in JavaScript and it looked much better than my mess (OOP is sweet no wonder). At that time I realized that due to ugly syntax of this language I was blinded to its true purpose i.e. helping me get things done.
Which programming language you learn and use doesn’t matter. Do not get sucked into the religion surrounding programming languages as that will only blind you to their true purpose of being your tool for doing interesting things. ~ Zed A. Shaw
And so I sucked it up and started writing object oriented code specially because these days I’m working in NodeJS and I’m writing more than just form validator and shiny bling bling.
So now even though JavaScript allows me to write code ‘in the air’ but I prefer not to and instead wrap my logic inside classes just as I’ve always done in C#. My code now looks somewhat like a C++ console app.
function HistoryRepository(store)
{
this.store = store;
this.deleteItems = function(beforeTime) {
// write code here
}
this.addItems = function(item) {
// write code here
}
}
function main()
{
var store = new Store('http://....');
var repository = new HistoryRepository(store);
repository.add({hello: 'world'});
}
main();
As you can see the only function that lives outside a class is the entry point method and the only line of code living ‘in the air’ is the entry point invocation. I don’t really appreciate the casing of this code but it goes with the convention of Javascript code. I’m glad that my horizons are broadened and writing javascript doesn’t bother me as much as it used to.
A couple of months back I came to Netherlands as a Knowledge Migrant to work for Hyves as a Core Developer. I think its good to jot down the check list on the web for others so here we go. Following are all the things that you should be doing once you arrive in Netherlands as a Knowledge Migrant.
Get a BSN Number (National ID Card)
Register yourself with City Hall (Tell them that you live on so and so address)
Get a Bank Account
Apply for 30% Ruling (To get some tax benefit)
Apply for Health Insurance
Some points to note:
Health insurance is mandatory for all immigrants.
If you move to a new residence you have to register your new address with City Hall of that area.
You can use Google to find houses for rent. If you go through agent you have to pay at least one month rent to the agent as commission.
You will have to visit GGD to take free TB Test
Health insurance is mandatory
A house in major city like Amsterdam is at least 950 euro and above.
Cost of basic medical insurance is at least 96 euro per month.
Once you get your health insurance and 30% ruling following steps need to be taken:
You need to register yourself with a general practitioner/house doctor in your area of residence.
You can exchange your foreign driving license with dutch driving license with 30% ruling
Some more points to note:
You can not visit a specialist of your medical problem unless you have been recommended by your GP/House Doctor.
You can not register with the GP without medical insurance
For driving license you have to fill a declaration form and pay some cash. If you have some medical problem like weak eye sight then you will also need a full checkup from a specialist to get a certificate of fitness and then you can apply for the license. This entire process can cost you up to 150 euro.
For easy travelling you can buy a Anonymous or Personal OV Chip card. This single card is valid for use on Bus, Tram and Metro. You obviously have to periodically add money in it.
This is all I’ve had to do so far. If there is anything else, I’ll update this post.
I just thought of an idea that I think can turn into a valuable product, a public website or a tool. The idea goes like this:
By making our data public we are effectively exposing our brain to the world in a sense we’re basically sharing what is inside in our mind. People do not have a way yet to tap into our brain and find out what do we believe in and what we think about but every time we tweet, blog or post something online, we’re effectively leaking information. All of this information can be analyzed to build a mind map of our brain. If we have a way to associate words with emotions then we can see on a calendar on which days we were upset or really happy just by analyzing all our tweets and blog posts.
If we can categorize words in emtions of love and hatred then we can find out what topics we have negative emtions for and what topics we’re interested in. Every time we re-tweet something on twitter we’re effectively saying that we agree with this idea. If we only analyze the retweets we can find like minded people.
So the idea that I have is that we can build a website that says something along the lines of “Who Am I” or “Analyze me” where you give it your twitter handle, your facebook account id, your blog url, etc and it will show you the following
Like minded people
On calendar your happiness and your sad times
Topics that you like
Topics that you dislike
Things that you think about the most
What kind of personality you have
The time you’re mostly active on internet
Do you have more hatred or love in your heart
Do you have more negative or positive emotions
Once you analyze all the public information, I’m sure there are alot of matrics you can derive and build a nice graph/chart out of everything. It should be fun visualizing this information and it can be useful in following scenarios
Considering marriage proposals
For police (any law enforcement agency) to profile someone
For you to analyze yourself / Self improvement
For recruitment
Actually this is a wonderful idea. I don’t think there is any complicated algorithm required to implement this that doesn’t exist today. Maybe in some corner of the world someone has already developed something like this but it should be very useful to make this public.
Why am I blogging about this idea instead of building it myself? I have my hands in too many things right now and its a waste to bury a useful idea in your brain and not implement it. I know a lot of people just kill ideas like this and I am not one of them. If I don’t have time or resources to build something I better give away my idea to someone who can make good use of it. I would post it on my www.letsdonatestuff.com but the site is not ready yet
In my 5+ years of professional experience I’ve seen two kinds of team structures
Where team is divided into sub teams each dedicated to a specific functional area of the software (Ownership mode)
Where there is no division of areas of software. Anyone and everyone is asked to work on any area or every area of the software (No ownership mode).
Let us first see what no ownership mode is.
No Ownership Mode
If you pick up any random source code file you’ll see that over time it has been modified by large variety of people
If you look at SVN Blame output of the file, you’ll see a new name after every 2-3 lines of code
When the design of the code written in file is flawed, no one can be held responsible because no one owns it.
If the code written in file is already bad. People modifying it later will continue with the bad design with an excuse that they don’t own it.
It takes more time for anyone to add a feature or fix a bug in that code because no one is owner of this and it has been modified by different people over time with their own idea of how this file works
You will see long methods and plenty of if and else statement
Programmers tend to add more lines in the methods to add some more ‘special’ conditions. The code is almost like a public toilet where everyone comes and poops.
Programmers are reluctant or scared of refactoring the code because they don’t completely understand how different portions of a single file work and they are unsure of how many things will break with their refactoring.
At the end of the day there is no one who completely understands how it works.
Above mentioned are some facts and observations. Now lets analyze these.
I’ve seen this kind of ‘No Ownership’ mode mostly in teams where Agile methodology/religion of development is followed. Where you have a bunch of tasks and a bunch of programmers and infinite sprint cycles. You assign tasks to programmers irrespective of what area of code they belong to. In Agile methodology software is not seen divided into functional areas but rather it is seen as a series of changes applied on one over the other and this is the reason why teams are not divided in functional areas either.
Its like treating software development like labor work. You have a set of laborers and set of bricks that need to be placed on the right places. You can assign any worker to pick up bricks and put them on any area as long as they are according to structure in the map. As Alan Kay the father of Small talk rightly puts it:
Most software today is very much like an Egyptian pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands of slaves
Or you could also see this as programmers being nodes of a distributed task scheduling system where any node can be assigned any random task. Now imagine if you have a faulty task with a memory leak in this case all nodes of the distributed scheduler will start acting up because tasks of that type can be scheduled on any node. If you have to find out what is causing this high memory usage, you have no idea what to do since all of the nodes are running all different kinds of tasks. Surely this heterogeneous distribution of work might appear to be more scalable because you can always add more nodes and without any additional configuration, work will appear to be accomplished more quickly.
Unfortunately software development is not like placing bricks on walls and software definitely is not a series of changes. Design has to be planned. Clean code and solid structure are very important. We can not have layers and layers of bad code written on top of one and another. I’m not saying here that Agile development is the root cause of this ‘No Ownership’ mode of working but I would certainly associate this disease with it because of how the entire methodology works.
Lets look at Ownership mode now.
Ownership mode
In this mode of working your team is divided into functional areas of software for example Client team, Server team, Design team, API team, Services team, etc. Not only that but each individual component in the software is owned by a particular person. Let us look at some facts and observations
Each file will be written mostly by a single developer but may be changed over time by one or two more developers as people join and leave a team.
You will have mostly consistent design with in a single component or functional area
If a developer has bad design sense then his bad code will be limited to his own component and will not plague the entire codebase
Developers can very quickly fix bugs and add functionality because they completely understand their own areas or components
When a component behaves badly then the corresponding developer directly feels responsible since it is written/maintained by him
When a component or feature is praised then the respective developer feels appreciated because he owns it
Developers improve and take care of their component/area because they ‘own it’. Its like their baby.
Everyone has a complete understanding of how things work in their area and can contribute valuable ideas on improvement and additional features related to their area.
This mode of development doesn’t work very well with Agile mindset but it is highly effective in maintaining a high quality product with a very clean codebase which is easy to maintain. However it does have some flaws too. Some of them are as follows:
If you suddenly have more work for one area you can not just pick random people from other teams and ask them to work on it.
While developers enjoy ownership but at the same time they often find themselves limited to one or two areas of application.
If there is less work in a component, developers may have not much work to do
If a developer leaves the team then it can create a hole in a team unless it is planned carefully and proper knowledge transfer is done
But the good news is that some of these issues can be addressed. First of all this ‘Ownership’ mode doesn’t dictate that each developer has to just stick to one component. A developer has to ‘own’ the components that they work on but they indeed can be owner of as many things as they can handle. Once a developer has mastered one area of the application he should be able to move to the other area and start working on that until he also gets a very good grip of that area. If there is less work or no work in their area of application these resources can be utilized in RnD on new features or they can take ownership of more components. To prevent making holes in teams when developers leave each component should have a primary and a backup resource. The backup resource is the one who reviews code written by primary resource and that will ensure that he understands how the component works besides maintaining good quality of code.
Conclusion
I did not plan to make this blog post sound like a rant against Agile development but unfortunately it has turned out to be that. As the world is progressing we are seeing technology being introduced in every area of life and every Tom, Dick and Harry is using some device running a software. The problem is that all of this software has to be written and it has to be changed quickly as there is a lot of competition. This rapid change in software and rapid development requirement had two side effects
Since every Joe wants to do Software. It has brought in a lot of non-IT people in the world of Software Development. They don’t know how it works they just want to make money out of it and for them developers are like laborers or donkeys to get the job done.
It has brought with it Agile methodology and thereby resulting into more unreliable, unstable software with plagued codebase. The fast paced development and rapid changes can only be addressed with Agile.
The fact that we want to do software quickly and rapidly now, is the reason why we have a lot of half baked products that crash all so often. In my personal experience I didn’t see that much of unreliable software some 10 years ago when every Joe was not in this business. When software companies were run by geeks and computer enthusiasts rather than people just wanting to make more money.
So if you give ownership to developers you may not be as fast paced and Agile but you will have a quality product and clean code base. But if you don’t give anyone ownership you will have bad code followed by bad code and no one is really responsible for it. You can hire the best developers in the world but you’ll have codebase as good as public toilet.
There are many concepts in computer programming which are commonly recommended against but rarely does anyone explain you why things like goto, singleton, static classes, etc can cause problems if not used with care. Today we’re going to see:
What kind of problem can a static class introduce
When it makes sense to create a static class
How to refactor the code which was using a static class
So to cover all of the above points let us take the example of our imaginary programmer friend ‘Tim’.
Tim was asked to write a product order placement component. One of the features of that component was that it could send email confirmation of the order to the user so he wrote something like the following:
class OrderSystem
{
public void AddOrder(User user, Product product)
{
// Persist the order in DB
Email.Send(user.Email, "Order confirmation", "Your order has been placed");
}
}
As you can see he created a static class Email with a Send method that takes bunch of arguments and sends the email. Lets see what was he thinking when he wrote this.
This is the simplest way to do it. Very clean one line code to send emails.
I don’t need ‘instances’ of the mail class. I just want to send email. Why create instance?
If there isn’t going to be any instance then lets make it a static class.
Later his supervisor asked him to write a unit test for this.
When he began writing a unit test he couldn’t verify that email was sent in a easy and straightforward way.
[TestMethod]
void AddOrderTest()
{
var orderSystem = new OrderSystem();
var user = new User(1, "john", "john@example.com");
var product = new Product(101, "pepsi");
orderSystem.AddOrder(user, product);
var order = orderSystem.GetOrder(user, product);
Assert.NotNull(order);
Assert.... // What do I write here to check if email is sent? Should I log into inbox and pull his emails?
}
He went ahead and actually wrote code to login to mailbox of user and see if the mail has arrived but then he realized he is not only testing his OrderSystem but also the mail delivery system. If the mail delivery system fails then his unit test fails. Failing test would mean there is something wrong with his order system but in reality the problem could either be in the network, the smtp server or somewhere else in the universe between the mailbox and his system.
Then he passed on the code to his colleague for review. The colleague first of all wanted to evaluate the design so he collapsed the code blocks and just looked at the interface. It wasn’t immediately clear that this component also has a dependency on email subsystem. Why? Because the implementation secretly and directly talks to the Email static class. Email subsystem doesn’t have to be injected into it anywhere. It doesn’t go in the constructor or in any property. It doesn’t seem that this system sends an email at all. Its after you look at the implementation of the class, when you realize that this class also has a dependency on another class.
Some people might say. Oh well! We don’t write unit tests and we don’t mind looking at implementation of classes to understand whats going on. We’re after all not the most productive team on earth. We take our time in fixing bugs and understanding code after all thats what our infinite sprint cycles are for. We’re ‘Agile’.
Nevermind, the story continues. Later Tim is asked to make it so that we either send sms or email confirmation based on the settings of the user.
If he changes the code to add an ‘IF’ statement then he is introducing two code paths in his method.
public void AddOrder(User user, Product product)
{
// Persist the order in DB
if (user.ConfirmationMethod == ConfirmationMethod.Email)
Email.Send(user.Email, "Order confirmation", "Your order has been placed");
else
Sms.Send(user.Mobile, "Your order has been placed");
}
The more number of distinct code paths a method has the more tests you have to write to cover all the code paths and ensure correctness.
The more number of distinct code paths a method has the more chances of bugs you have due to variation of code paths and states.
Now his test also fails if there is problem in SMS sending subsystem hinting as if there is a problem in his method.
In reality Tim was really asked to introduce a new mechanism of confirmation message sending but he wen’t and modified the order system class which is irrelevant but he ‘had to’ because the mechanism of sending email was ‘baked’ into the implementation.
Some may argue at this point, “Well he could have faced all these problems even if the class was not static”. Yes that’s right but could he arrive at the right solution keeping the class or his mentality static?
So Tim realized that sending a confirmation message is something that needs to be abstracted out. He needs to be able to plug in the right implementation and he should be able to solve most of the issues related to adding new ways of delivery, reduced cyclomatic complexity, unit testing etc. He was right in his thoughts up to this point but he was somehow obsessed with static classes and loved the simplicity of being able to do something in one line without creating any objects or anything so he chose a wrong solution to the problem which was something like the following:
abstract class OrderSystem
{
public void AddOrder(User user, Product product)
{
// Persist the order in DB
SendConfirmation(user);
}
protected abstract void SendConfirmation(User user);
}
As you can see he turned his class into an abstract class with a SendConfirmation method. Now he thought he would create one derived class for sending email and one for sending sms and he can mock this easily for unit testing purpose also.
Some may say, “Fair enough! Guy has a decent solution to the problem.”
So when you go to a restaurant do you have different kinds of waiters for taking orders for different kind of food?
Umm I want to eat vegetable so I’ll call that waiter wearing green shirt. Oh you want some meat? Lets call that waiter wearing red shirt. Doesn’t make sense right? The job of the waiter is to note down the dish you want to eat. The change of choice of dish for each customer is abstracted out in something called ‘order menu’. You don’t create different kind of waiters for different kind of dishes.
So he solved the problem by further damaging the design. Besides what would you do if you want to send sms and email both?
Alright so whats the ‘right’ way of solving the problem? The answer should be clear by now. We need to abstract out confirmation mechanism into a non static class like so:
abstract class ConfirmationSender
{
public abstract void SendConfirmation(User user);
}
class SMSConfirmationSender: ConfirmationSender
{
public override void SendConfirmation(User user)
{
// Send sms here how ever you want
}
}
class EmailConfirmationSender : ConfirmationSender
{
....
}
Now his AddOrder method can take the ConfirmationSender as a parameter or in the constructor of the class depending upon whether the mechanism changes per user or based on system setting.
From the interface of the class it is very clear that this class has a dependency on another class. You don’t need to look at the implementation of the method to realize it also has to send confirmation via email or sms.
You can unit test this method without having to actually check mailbox of the user.
Your test will no longer fail because of problem in email delivery mechanism because unit test will mock the ConfirmationSender and only verify if the confirmation was sent or not.
For adding new confirmation delivery mechanisms you don’t have to modify the OrderSystem. The less this code gets modified the more stable and bug free it will be.
The cyclomatic complexity will not increase with each new confirmation mechanism. Or even if you don’t want to send confirmation at all, you can just create a NullConfirmationSender that doesn’t do anything and just feed that to the method.
So like I said earlier. He could have all these problems even if the email sender was not a static class but if it is a static class it encourages all the wrong patterns and introduces the problems.
Some may say, “Ok so static class is evil then?” No! It does make sense to make a class static when
It doesn’t have any state to maintain.
The methods are very simple unit of operations that are ‘universal truth’ and won’t ever change.
The best example of perfect static class and method is Math class with methods like Min(num1, num2), Max(num1, num2), Sqrt(num), Abs(num), Sin(num).
Definition of Minimum, Maximum, Sin, Cos, etc. will never change. They are very small, highly concise fast operations. Whether a class has dependency on Math class at all, doesn’t even matter.
Some may say, “I’ve seen static classes with state in code written by top notch programmers”. Well every rule has an exception. You need to be able to judge what makes sense in what scenario, how much you care, how much time you have, how much ‘agile’ your team is, etc.
You might ask, “What about singletons? Shouldn’t we make a class static when there is only one instance in our universe for that?”. No! First of all the fact you need to have a single instance in your modeled universe, has nothing to do with whether class should be static or not. Static means “I don’t have state. I’m a helper.” If you really really want a single instance throughout your application you can opt for Singleton pattern.
“Why two times ‘really’ in your statement?”, you might ask. Well because such a strict definition of single instance can limit the possibility to have parallel worlds in your application and also you can still enforce single instance by using dependency injection framework and correct life time hints while registeration instead of making the class singleton. If you don’t use dependency injection and you want to make it clear by ‘design’ that you don’t want more than one instance lurking around in the app then you can opt for the Singleton.
This is mostly a maintenance release up till now. Since it doesn’t have any new features besides bug fixes and enhancements in the core.
It is 100% compatible with 3.0 and will continue to be till the last revision.
Which means it is safe for some people in the network to be using 3.0 and others can try out 3.1.
Feel free to try it out and report any bugs in the Issue Tracker
In Part 1 and Part 2 of this series we saw a couple of ways of splitting a collection in N parts but we’ve not yet designed a solution that:
Can support infinite sequences
Iterates over the enumerable only once
Is lazy
Initially our goal was to keep the implementation as succinct as possible but as we improved our code the size kept increasing. We’ll still try to achieve all 3 of the above goals in as less code as possible. So here is our new solution:
static class LinqExtensions
{
public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> items, int count)
{
var queues = new List<ConcurrentQueue<T>>();
for (int i = 0; i < count; i++)
queues.Add(new ConcurrentQueue<T>());
var enumerator = items.GetEnumerator();
Action getMore = () =>
{
for (int i = 0; i < count; i++)
lock (enumerator)
{
if (enumerator.MoveNext())
queues[i].Enqueue(enumerator.Current);
}
};
for (int i = 0; i < count; i++)
yield return GetBufferedEnumerable(queues[i], getMore);
}
static IEnumerable<T> GetBufferedEnumerable<T>(ConcurrentQueue<T> buffer, Action getMore)
{
do
{
T item;
if (buffer.TryDequeue(out item))
yield return item;
getMore();
}
while (buffer.Count > 0);
}
}
I mentioned in my last post that we needed a way to buffer the values that have been enumerated already and put them in the right sequence therefore we’ve used queues for this purpose. We have a simple getMore function that can pick N items from the source and put one item in each buffered sequence.
This way if you enumerate only on first split sequence, the remaining sequences will keep buffering their values. I’ve used concurrent queues here to allow you to use this in parallel code.
As you can see it went a little ahead of the last item required in first sequence because on each call to getMore we fill up all the sequences as apposed to only the one that asked for data.
In Part 1 we derived a new method for splitting a collection in to N parts but it had a major problem lets find out what it is.
On StackOverflow I once saw someone tracing his Linq to Objects query by writing a method that yields numbers and does a Console.WriteLine before every run to see his list as it is enumerated. I think that’s a fantastic way of finding out whether the query you wrote is lazy or has some issues. Following is a slightly modified version of that method.
static IEnumerable<int> GetNumbers()
{
for (int i = 0; i < 10; i++)
{
Console.Write(i + ",");
yield return i;
}
}
We will now use following code to evaluate our Split methods
This method as you can see is splitting the collection into 3 parts and then taking first 2 elements from the first sequence.
Now lets use our 2 implementations of split and see the output.
Method 1
static class LinqExtensions
{
public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> list, int parts)
{
int i = 0;
var splits = from item in list
group item by i++ % parts into part
select part.AsEnumerable();
return splits;
}
}
Output:
0,1,2,3,4,5,6,7,8,9,
As you can see our first method was clearly not lazy since it enumerated the complete list even if we wanted only 2 elements out of first sequence. This was mainly because of ‘group by’ clause which is eager in nature and consumes the entire enumerable. This method had one advantage though i.e. it enumerated only once on the enumerable to split it.
Lets see how our second method performs.
Method 2
static class LinqExtensions
{
public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> items, int count)
{
for (int i = 0; i < count; i++)
yield return items.Skip(i).Step(count);
}
public static IEnumerable<T> Step<T>(this IEnumerable<T> items, int size)
{
while (items.Any())
{
yield return items.First();
items = items.Skip(size);
}
}
}
Output
0,0,0,1,2,3,0,1,2,3,
The output is a bit bizarre. The method that apparently looked elegant has a major problem i.e. it re-enumerates the enumerable from the beginning for every iteration (MoveNext) on each split list. The reason is that we have chained the Skip calls in our step method due to which it never remembers where you left off on the last iteration. So its so lazy implementation that its disastrously useless. It will perform very very badly. Also every split list returned from the split method enumerates from the beginning but that’s not as bad as what is going inside the Step method.
So how can we fix the Step method to ‘remember’ where you left off in the last iteration? Well the answer is simple; the good ol’ GetEnumerator. So here is the new and improved implementation of the Split method.
Method 3
static class LinqExtensions
{
public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> items, int count)
{
for (int i = 0; i < count; i++)
yield return items.Skip(i).Step(count);
}
public static IEnumerable<T> Step<T>(this IEnumerable<T> items, int size)
{
var enumerator = items.GetEnumerator();
while (enumerator.MoveNext())
{
yield return enumerator.Current;
enumerator.Skip(size - 1);
}
}
public static void Skip<T>(this IEnumerator<T> enumerator, int count)
{
for (int i = 0; i < count; i++)
enumerator.MoveNext();
}
}
As you can see the better we try to make it, the longer the method becomes. Lets have a look at the output.
Output
0,1,2,3,
Aha! Now we’re talking. We only consumed 4 elements. The minimum required to reach the second element of first split list. The only problem left in this implementation is that it still issues N distinct enumerables for each split list causing the list to be re-enumerated from beginning for each split. We need to somehow cache the already consumed values but we’ll look at that in Part 3 of this series.