Why am I angry at developers? Is it factory? Is it testable?

October 17, 2011 Design 4 comments

Please take a look at this code snippet. What would you say about it?

    public class TreatmentDataProviderFactory
    {
        private IDataAccess _dataAccess;
        private TreatmentDataProvider _treatmentDataProviderHost;
        private TreatmentDataProviderField _treatmentDataProviderField;

        public TreatmentDataProviderFactory(IDataAccess dataAccess)
        {
            _dataAccess = dataAccess;
        }

        public ITreatmentDataProvider Provider
        {
            get
            {
                if (SomeInteractionSingleton.PluginHost.GetWorkMode() == WorkMode.ConnectedMode)
                {
                    if (_treatmentDataProviderHost == null)
                    {
                        _treatmentDataProviderHost = new TreatmentDataProvider(_dataAccess);
                    }
                    return _treatmentDataProviderHost;
                }
                else
                {
                    if (_treatmentDataProviderField == null)
                    {
                        _treatmentDataProviderField = new TreatmentDataProviderField(_dataAccess);
                    }
                    return _treatmentDataProviderField;
                }
            }
        }
    }

Doesn’t it smell bad a lot?

1) Let’s start with obvious: properties in C# are not intended to be 10-20 lines long and having complex logic. Anyway I won’t wrote post if this was a problem.

2) Now the worst mistake here: how am I supposed to test this code if it uses SomeInteractionSingleton in the if condition. Why should this code be so coupled to SomeInteractionSingleton? I wrote this post because developer didn’t run tests. If he ran the tests, he would see they fail.

3) Now second bad mistake: this code keeps two instances of data providers. We can suppose that this is storage for two providers or something? :) I think GC is designed for something and lifecycle of objects shouldn’t be treated as this. I thought IoC is invented for something like this. At least developers should not keep code that much coupled and deliver creation logic.

4) Another mistake: Is this a Factory Method design pattern? – Almost. At least it has such name and looks like it (a bit). But it doesn’t meet either its original definition or either Parameterized Factory Method definition.

I redesigned this class. See:

    public class TreatmentDataProvider
    {
        protected TreatmentDataProviderHost TreatmentDataProviderHost { get; private set; }
        protected TreatmentDataProviderField TreatmentDataProviderField { get; private set; }
        protected IPluginHost PluginHost { get; private set; }

        public TreatmentDataProvider(IPluginHost pluginHost, TreatmentDataProviderHost treatmentDataProviderHost, TreatmentDataProviderField treatmentDataProviderField)
        {
            PluginHost = pluginHost;
            TreatmentDataProviderHost = treatmentDataProviderHost;
            TreatmentDataProviderField = treatmentDataProviderField;
        }

        public ITreatmentDataProvider Provider
        {
            get
            {
                if (PluginHost.GetWorkMode() == WorkMode.ConnectedMode)
                {
                    return TreatmentDataProviderHost;
                }
                else
                {
                    return TreatmentDataProviderField;
                }
            }
        }
    }

Code above does the same logic, but it delivered control of creation of instances to other parties (IoC), also it now doesn’t have dependency on static methods, so code is less coupled. I didn’t remove this class as we have to keep verification for WorkMode at runtime. I know you might complain about these protected properties, but I like to have it that way for more flexibility when testing.

What are your thoughts? [Sentence removed 10/18/2011]

[Added 10/18/2011]

5) Yet another big mistake: Code reviewer. Guess who he was. When I’ve been reviewing this code by request of developer I just thought “it works, then it should be ok”. Why didn’t I ask about unit tests and why didn’t I took reviewing more scrupulously. I have to be more accurate when reviewing others code. Bad code reviewer.

6) Yet another mistake: writing this blog post. I understand that my criticism might be taken to close, especially if this was read. I also don’t like criticism. No one likes. Man, if you reading this you have to know I didn’t mean to abuse you or something, I just was upset at night about failing tests.


4 comments


Help me select UML for book: hand-drawn or tool-prepared

October 14, 2011 Design Patterns 16 comments

As you may know I’m working on Design Patterns book in Ukrainian and as most of the posts I had on patterns have UML-s, I’m considering having UML diagrams in book as well to be consistent. Thus I can either prepare them in some UML tool or just draw. See yourself.

Drawn by me

FACTORY_NEW

Also drawn by me, but in advanced tool

image

And here how it looks like when on paper

image

image

Why I like hand-drawn option

I would like to use hand-drawn variant because it will make the book look cheery and will make the difference. It might bring some interest like “So what’s is really on that diagram?”. Also I still don’t position book as an “official” book, so I would like to have some bits of unofficially. Especially taking into account that auditory is mostly young starting developers.

Why I don’t like it

I’m of course hesitating, as this variant in its origin is inaccurate (of course I can try harder). Also maintaining such diagrams is bit more difficult, but I don’t see any problem with this.

Please help me choose!

 

[Added later (wasn’t in original post)]

After I posted this friend suggested me to use this online uml generation tool. And it would be great another option to consider, but it generates not really what I want:

image

But it was extremely nice, that to generate picture above I just used this code:

# Abstract Factory
[Cat]^[WoodenCat], [Cat]^[TeddyCat]
[Bear]^[WoodenBear], [Bear]^[TeddyBear]
[IToyFactory]^[WoodenToysFactory], [IToyFactory]^[TeddyToysFactory]
[TeddyToysFactory]uses-.->[TeddyBear]
[TeddyToysFactory]uses-.->[TeddyCat]
[Client]->[IToyFactory]
[Client]->[Cat]
[Client]->[Bear]
[WoodenToysFactory]uses-.->[WoodenCat]
[WoodenToysFactory]uses-.->[WoodenBear]

Nice tool, indeed, but for extremely simple diagrams.

[Added later (15 Oct, after comments)]

After comment by Satomi Joba I tried community edition of another tool, called Astah. Below is what I was able to draw by it. Two things about it: 1) Drawing in this tool is just fabulous, smooth and easy. For me it was more quick and intuitive drawing than in such matured tools as Enterprise Architect for example. 2) Although I’m not sure I like this bold borders and I wasn’t able to quickly change styling of diagram (maybe because of edition I used?).

image

Anyway question is still the same: do I use hand-drawing or do I use tool?


16 comments


Uneta Plus

October 8, 2011 Conferences 2 comments

Week ago I had a chance to be at one of the best Ukrainian conferences for the recent time. Awesome conference, awesome presenters, awesome people, awesome uneta plus.

Andriy Buday at Uneta Plus ConferencePPhoto by Mike Chaliy

Conference was far from my home in faraway city Kharkiv. I’ve been there once, when delivering MEF talk last year at ITJam2010. Even location kept the same:

 

Now more on conference itself. It started with keynote from two maybe most known ms guys in Ukraine – Dmitriy Nikonov and Serhiy Baidachni.

I cannot say that keynote was structured and well organized speech, it was more improvising, but having those two guys rescued it. They definitely are not new in field of presenting something, so auditory listened with attention, and all get acquainted with what’s next. Especially I really enjoyed stuff now available in TFS. It is getting matured over time.

Following presentation I attended was about Silverlight and XNA and how they live together in WP7 Mango. It wasn’t deep dive into things in Silverlight or XNA, but I enjoyed observing small UFO flying thought the auditory (background was transmitted from phone camera).

“Every game consists with 3-4 parts at each level. They are load and unload. Between those is while loop that has two calls – update game world and render game world.” – said Alex Golesh*. Now in WP 7.5 we can render both Silverlight and XNA in same application. I found this msdn page well informative.

Sharepoint for internet sites gives good business solution. I was able to see Sharepoint in action. Marat showed how we can start with file->new project and proceed to completed site, designed in sharepoint designer. Also some bits about authorization and authentication in sharepoint, and I felt like I’m guru in sharepoint… NO! I still think it is complex and not clear, but Microsoft actively pushes this product. Personally I think, that even if it is great product, it is not something that you would want to listen at programmers conference.

Lunch. Oh yeah, I really appreciate guys, who organized this event, as I could eat tasty (no really!) food without leaving conference. I guess money I paid for conference has something to do with taste of food, but believe me – it worth to pay more and get normal food.

I also attended presentation on entity framework, which consisted with two parts and was delivered in English by Diego Vega. Man, if you are reading this, you have to know – you rocked! I found this presentation to be best structured and most fulfilled with information on its topic.

Also I’m bit disappointed that I didn’t saw that amazing luster of Dmitriy Kostylev. Everyone who was in section 2 listening to him were literally excited. Ah… sad I missed that hardcore SQL optimization wisdom.

Another thing made this event different – so called “round table”. All of the speakers were gathered at the scene answering questions from auditory. I enjoyed this part as well, but left before the end, as I had train back to home.

Organizers of even asked few times about feedback. They want to hear more about what can be improved in this event, what was great and what wasn’t that much great. Here are few of my subjective thoughts:

  1. Lower price a bit. I think more people will attend if price would be something more moderate, but who knows… you tried to balance between number of attendees and price, not me. I was frustrated about showroom not being full.
  2. How about more sections, so I can choose if I get overview of sharepoint, tfs, or if I get flood of hardcore wp7 stuff. I just want more clear vision on what I can grasp from one or other presentation, because in some topics I feel myself confident and need advanced level, respectively in some I need introductory level.
  3. Personally I would like to see more foreigners, like Diego. I don’t know if it is hard to have them on event, but if you bring someone like Hanselman, I bet I will visit you even I have to pay more and travel longer.
  4. I’m not sure it worth complaining but why not Kyiv?

Despite of what I listed above uneta plus really was different sort of event of those I visited ever before because of extremely awesome presenters and great organization. Well done!

* This is how I remembered his words. I don’t promise I didn’t misinterpret something.


2 comments


LvivIT #0 – it was awesome!

September 17, 2011 Conferences No comments

Yet another time, I’m on .net community conference in Lviv again. This time LvivIT. You know what makes me happy about this? It is understanding that such events are happening more and more often and they become more mature in my native city. Presenters are more and more known guys. This also makes me think that we start witnessing something that is more common to big cities in USA, cities that have few or more technology user groups. At least now I know that we are moving in the right direction. Cool.

This event welcomed presenters from other cities of Ukraine. Below I’m going to list some of my and only my thoughts on all of presentations, without mentioning names of presenters.

1. This man simply rocks, he had absolutely another style of presenting. He doesn’t follow any kind of agenda and even doesn’t have presentation. He simply started with throwing two iPads into auditory with his app started on it. Application is game, that scored first place in marketplace for some period of time, thus man simply has up to 20K per day, and he is delivering his story of success to us. Main thing I got from this presentation is that if you want to succeed, you have to work on things you like and don’t compromise with different bulshit surrounding you. He ended presentation with no special attention to the audience, but started finishing soup he brought instead of lunch (in between of what? or doesn’t he have enough time to eat normally?). Everyone expressed much interest in this person and his game. I saw how much he is proud of it, when he had words with interested guys after presentation. This really inspires.

2. Second presentation was about html4 and html5 & further future of HTML and web technologies, so called web 3.0. During this presentation I got what it means when there is person in audience that takes all words of speaker into criticism and then simply gives hard time to presenter. They almost yelled at each other.

3. Believe me or not, but there was presentation on Agile, when guy said nothing about Agile itself, but gave everyone know that it really works. “AGILE! Yes!” He showed example of SorceForge implementing agile and succeeding. Another portion of presentation were results of surveys on if agile really works. From this presentation I’ve learned about another way of presenting material.

4. I’ve also learned, that presenters have to be ready before the presentation, and not show that they are not up to speed with stuff. Even when you joke about preparing some slide hour ago, you have to cover it with your deep knowledge… Or at least it is my personal thought. Ok, maybe that is because I’m experienced in material of topic presented on fourth presentation.

5. “Silverlight is not dead” first words given from him, latest presenter. He talked about features that will be added to Silverlight 5, like multicolumn text, multi-click, XAML extensions, ancestor, elevated trust, and in-browser elevated trust, graphics improvements, multicore usage, vector printing, 3D with XNA. Also presenter showed known slide on future of SL and Windows 8 with green and blue divisions of world :). Personally I liked this presentation because it clearly and honestly listed things coming in SL. I even think that one of the features can be utilized in project I’m working on now.

To summarize, I want to say that this event really was awesome! I enjoyed it very much! Well done, guys, especially Dima and sponsor and of course presenters!


No comments


Delivering presentation w/o eye-contact sucks

August 29, 2011 Presentation No comments

On Thursday 18 August I delivered presentation on Software Estimation. Mainly I went thought the agenda that more-or-less matches contents of the book “Software Estimation: Demystifying the Black Art”, as I found it covering most of the main aspects of the estimation. I prepared a lot of interesting stories and analogies to explain stuff and presented that with great dedication… but… my audience was connected on the phone (except of two in room with me) and we did not have web cam (our IT dep. failure)… so… it really sucked.

I simply cannot know if they accept words and jokes I produce, thus I cannot understand if I have do adjust my speech. Also it is not possible to know if audience gets information easily or it goes hard. Asking audience different questions and getting response from them is also hard. Much more conversation was going between me and two people in meeting room. Long story short, when there is no visual feedback presenting information makes me, as presenter, frustrated. Of course it is not secret that it is important to have eye-contact with audience, I just wanted to complain a bit.

Ok, as we live in bureaucracy and connected world, there was feedback – filled survey. And you know what? It s***ed again – I got only 3 records in survey, even they are very positive (except complains on work of IT) it means only one thing – my audience didn’t admire the presentation. I won’t be surprised if 2 of 3 responses are from guys listening to me in the meeting room.


No comments


Just upgraded to AutoMapper 1.1 RTM

August 24, 2011 AutoMapper No comments

Just upgraded AutoMapper to 1.1 version. What is really funny about this update is that we used pre-release version of this tool in production. And it worked well, even there were some bugs, so I’m having some doubts if we relied on some of those bugs : ).

Here are Release notes we should benefit from:

For the 1.0 RTM: http://automapper.codeplex.com/releases/view/39791 For the 1.1 RTM: http://automapper.codeplex.com/releases/view/44802

Honestly I did not find some major changes and there were no obvious (compile error) breaking changes, even we used many of AutoMapper features. There are slight performance improvements and added features that we probably don’t need or maybe need. Thus I would like to complain about AutoMapper a bit – it is lacking documentation. Sometimes it is much easier to read about some feature and realize “aha, great, now we can utilize this capability!”.

There is only one hint to upgrading process I would like to add. When upgrading version I guess you would usually replace path to assembly in all *.csproj, but don’t be lazy and do full search in other files, as you might have different build/deploy scripts that push 3rdParty components to appropriate folders.


No comments


Upgrading to higher version of NHibernate w/ FluentNH

August 23, 2011 NHibernate No comments

“How much would it take?” – Yeah, it is interesting question, which we gave to ourselves being maybe 3rd year of developing projects based on NHibernate. We took quick look and from top of the head it looks like it would take a lot of time, because FluentNhibernate has changed syntaxes, because there might be something not working after changes, etc.
You know what? I was able to change version of NHibernate and make it working for us within 25 minutes.
It just proves known statement about giving estimates: Judge as last resort!” Better compute and much better count if applicable.
So what is needed to be changed when you are upgrading to higher version of NHibernate w/ FluentNHiberante (from 2.0.0.4000 to 3.1.0.4000):
  1. Download compatible versions of NH and FNH. Usually FNH is step behind. (see release notes)
  2. Update references of your projects if you have folders for versions of 3rdParties.
  3. Update fluent initialization of NH with adding proxyfactory, like below:

var configuration = new Configuration();
var properties = new Dictionary<string, string>();
properties.Add(“proxyfactory.factory_class”, “NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle”);

4. Also you would need to reference NHibernate.ByteCode.Castle in your starting project.

5. Update fluent mapping with removing different noise code like below (if you are below 1.0 version):

image

We had about 250 mapping files so “Ctrl+H” had to be used a lot.
6. Run your application and see if you don’t have any issues.

So, long story short, updating to higher version of NH is not painful. It is still not clear if it has big impact or not, especially for big project.
If we decide to upgrade I will let you know if testing shows big impact.


No comments


My 200th blog post. Sometimes it is hard to blog.

August 22, 2011 Uncategorized No comments

Hello. Officially this is my 200th blog post.

In my “100th blog post” I wrote that blogging pays off and indeed it brought me many cool things, like knowledge, opportunities, also I became noticeable by others in my company.

In that hundred posts I finished my series on design patters. I prepared for being senior developer and became him. I delivered many presentations, including one big it-jam. I wrote few popular posts on career plan and interviews. I had many other not less interesting posts.

It took me 9 months to have those first 100 posts and now I see that next portion took me over year. Interesting… when will I get my 300th post. Would it take year, less, or more? I cannot answer, but I know that blogging started to be hard for me. Not because I don’t have things to blog about, but because I don’t stop myself for few moments to write about something. Almost each day I have some challenges at work, I can blog about. I read many interesting stuff, which I can try out and blog about. But I don’t.

I know, I know real blogger shouldn’t write such posts. And I wouldn’t even try to convince anyone that I’ve been very busy or something… I had lot of routine stuff that many people have all their life. I had nothing unusual but for some reason lost feeling of the road I’ve decided to take. Why did I?

I have disease – I often think that I will do the stuff later, maybe in the evening or tomorrow. And when I think so I do nothing next day. It is damn stupid habit. For the dozen of weeks I thought that I will post latest 2 design patterns, but I did not. Some time passed and I had them on my blog, but it was too hard.

Why do I think that I will do all the stuff tomorrow? It is not possible. There are guys at work that never think that way. They are so much responsible, they fill that cannot left working place until some good portion of their job is done. WTF? Why I don’t have such skill?

I probably didn’t read my blog for some long time! I need to! Here is something that gives me some advices on how I should proceed.

Where have I been? Nothing is going to be tomorrow if I do nothing today!

I DO NOT LEAVE THINGS FOR TOMORROW!!!


No comments


Book Review: “Software Estimation: Demystifying the Black Art”

August 17, 2011 Book Reviews No comments

imageFinally I have another post on read book (after so long time not reading posting). This time book is on software estimation called “Software Estimation: Demystifying the Black Art”.

This is first book that I read about estimation end-to-end, and I have to say that it is extremely good and I think it covers most of the topics needed to be able to see how estimation works.

It consists with three parts, each gives you good set of knowledge, first one on core concepts, second on fundamental techniques and third one on challenges you might dive in when estimating.

Honestly I started to read this book, because I knew that I will have presentation/training to deliver. I heard about most of the concepts mentioned in book, but (!) book gave great insight into them and from different perspectives so I started to wish that I had reconsidered all of the many estimates I gave in the past.

I would really recommend you to read this book as it helps you to think deeper when working on estimates.



No comments


WCF Configuration caused memory ‘leaks’

August 17, 2011 Performance, WCF 9 comments

If you are lazy to read whole post, jump to “Summary”.

Have you been in situation when a project was designed for low performance needs and in the end of the day customers want it to be 100 times faster? You have to be ready for such time and be ready to scale.

We changed the configuration of our application for more aggressive processing but it did not meet customer needs, so we had to do much more other tweaks to make our service performing better. Unfortunately along with with improvements we started to get memory leaks.

So our app started to eat memory dramatically. Within 5-15 minutes it was already hitting 2Gb score. Why the hell? Most of the changes were configuration and performance tweaks.

I decided to profile the application with ANTS Memory Profiler. (by the way it is simply amazing profiler). After I learned what means what in the profiler, I was able to analyze what I see. Turns out our application produces huge memory fragmentation with large objects. (see screenshots below)

Here are some recommendations for fixing such issues from profiler website:

Solving large object heap fragmentation

Large object heap fragmentation can be one of the most difficult types of memory problem to solve, because it often involves changes to the architecture of the application. The best approach to use will depend on the exact nature of your program:

•Split arrays into smaller units so that they remain below 85kB (and so are never allocated on the large object heap).

•Alternatively, you can allocate the largest and longest-living objects first (if your objects are files which are queued for processing, for example).

•In some cases, it may be that periodically stopping and restarting the program is the only option.

So what are these large objects? My initial thought was that out app simply was not keeping up with processing and we overloaded memory with many large collections that were not garbage collected.

clip_image002

But what was interesting was this funny  Byte[] array keeping 134Mb of memory… I drilled deeper and found the following:

image

Oh, System.ServiceModel.Channels.BufferManager! This made me thinks that large objects are actually allocated by WCF and never disposed, so in the end of the day it was causing our memory leaks.

So I knew that it is something related to WCF BufferManager. I started looking at what was changed in WCF configuration in order to process bigger messages. Here is what I found:

<binding name=”Allscripts.Homecare.WSHTTPBinding.Configuration” closeTimeout=”00:05:00″ openTimeout=”00:05:00″ receiveTimeout=”00:10:00″ sendTimeout=”00:05:00″ maxBufferPoolSize=”2147483647″ maxReceivedMessageSize=”2147483647″>
<readerQuotas maxDepth=”2147483647″ maxStringContentLength=”2147483647″ maxArrayLength=”2147483647″ maxBytesPerRead=”2147483647″ maxNameTableCharCount=”2147483647″/>

Do you see anything interesting about the configuration above? Yes, all values are set to max. What are chances that someone really evaluated their impact on performance before setting everything to max? Max is always better. Isn’t it? maxBufferPoolSize property is related to BufferManager. A bit of a search and I found this:

From http://kennyw.com/work/indigo/51 :

“On the built-in WCF Bindings and Transport Binding Elements, we expose MaxBufferPoolSize as a property for you to control our cache footprint. If you are sending small (< 64K) messages, then the default value of 512K is likely acceptable. For larger messages, it’s often best to avoid pooling altogether, which you can accomplish by setting MaxBufferPoolSize=0. You should of course profile your code under different values to determine the settings that will be optimal for your application.”

So with changing maxBufferPoolSize to 0 I was able to witness stability in the work of our service. Of course, it was still consuming some memory because of hard time we gave it, but it was not leaking.

From the picture below you can see that there is no problem with Large Objects Heap. It was nicely bumping up and down (blue line).

image

 

Summary

If you have performance problems, and try to fix them by changing all configuration parameters (timeouts, buffer sizes, other stuff) do this carefully. Always evaluate the performance impact. If you already have problems, use advanced profilers.

Surprisingly, increasing WCF maxBufferPoolSize doesn’t always mean increase in performance, rather it can result in huge memory leaks if the size of your messages is larger than 64K. For larger messages it is better to set MaxBufferPoolSize=0. For me it sounds like GC is not so keen on disposing large objects from heap and this caused our issues.

Hope this helps someone.


9 comments