Skip to main content

Posts

Showing posts with the label .Net

Simplifying use of Static

Hope everyone must have come across static keyword while doing development, specifically using C#. Static modifier is used to declare static member, which means it belong to the type itself. Well, as part of this article, I’m not going to discuss more about static as it will increase the length of this writeup. public   class  Logger   {         public   static   int  GetLogLevel( string  logType)       {            …       }   }   Above is a sample code snippet wherein, we have a class called Logger and it has a static method called GetLogLevel . Now in order to call this GetLogLevel(…) method, first we have to add the required namespace, where this Logger class has been defined. Something like this, using  Planner.Utilities;    Well, nothing new as of now. Next, let...

Tips for Effective Code Reviews

In the field of software development, code review plays a very vital role. It not only enhances the code quality, but also identifies issues before the code goes into the tester’s hand. T hroughout my development experience, I came across many amazing guidelines that can contribute to an effective code review. So, as part of this article I’m going to list down all those points. Naming Conventions, Data types, Sizes and Comments V ariable names should be small but self-explanatory. Based on the language and platform, standards and casing should be followed. Always prefer field names using adjective or noun. Prefer to use PascalCasing for resource keys. Always prefer to name events with a verb. It’s good to think about tense while naming events, i.e. rendered, rendering, etc. Keep method size as small as possible. I personally prefer, size of method body of <=12 lines. So, that complete method can be seen without scrolling the screen. All the parameters should pass validation check be...

Why normal .NET exception handling doesn't work in WCF?

Errors and exceptions are part of our programming life and WCF is no different. So, when we get errors in WCF, we would like to propagate those errors to our WCF client so that they can accordingly take actions. In order to demonstrate this, let’s go through the code of a simple service: public int Add(int number1, int number2) { return number1 + number2; } public double Divide(int number1, int number2) { return number1 / number2; } Both the above methods will perform some calculation and return the result to the client. Let’s say, now for some reason someone sent 2 nd parameter of Divide method as 0.  What will happen? Definitely code will throw an error or say DivideByZero exception. Isn’t it? Know how to handle this error? Most of the developers will simply decorate Divide method with Try-catch block and throw the exception, similar to our normal .NET exception handling mechanism as shown i...

Performance and Memory tips

Two things play a very significant role in any application development, and those are application’s footprint and performance. Whenever I’m asked to work on these two tracks, I used to visit a huge list of sites to get many more ideas apart from what I already know. So, I thought, why can’t I collate all the good points and add them to my repository. At the same time, I thought of sharing those points in this blog. Rather than making this blog post full of theory, I’m planning to make it simple by just adding the bullet points. Uhh! Enough of gossip. Let’s get started by going through some common and important rules. Create object only when it is really required More the objects lesser the performance :(  Grab resources, use them and release at the earliest Default capacity of StringBuilder is 16. So, if you want to store less than 16 values then make sure to set the capacity. Avoid unnecessary boxing and unboxing Prefer lazy loading Use Static variables cautiou...

Error: Icon file is not set to be published...

Today while working on one of the application that was for ClickOnce deployment I faced a small issue which took almost my half an hour. My task was to associate a default icon to my application. So, in order to do this, I went to my project properties and simply set the icon as shown below: As you can see in above screenshot, it is showing an error icon which states: 'Icon file is not set to be published with the application, or is not part of the required download group' Then I build my application and land up with below error message which was more clear: After hitting my head for many minutes, I thought, let's check out what is this 'download group'. On surfing net, I got the clue that download group is nothing but a collection of files which are going to be part of our publish activity. So, I quickly opened Application Files dialog using Project properties >> Publish. The Application Files dialog looks like: ...

Controlling Degree of Concurrent Execution in Parallel Loops

Having multi-core machine, one can get the benefit of Parallelism for long-running and blocking tasks. I hope most of you might have used Parallel. For, Parallel. Foreach and TPL several times. Generally parallelism is used to get the benefits of all the available cores on the machine that commonly leads to the shortest execution time for any task. Did you ever thought about what can be the possible demerits of using all the available cores for just a single application? Is it really required to use all the available cores of your machine for a single application? Can't we use only a few of the cores? Is there any way to restrict these parallel loops in terms of cores? The answer of all the preceding questions is YES . But why should we bother about how many cores are participating in execution? Well, there can be several reasons behind this.  The foremost reason  that I feel is, when a single time consuming application/task is running on all the available cores utili...

Customizing debugging session - Part II

Continuing to my previous article on 'Customizing debugging session' , we can further control on how properties and fields appear in our debugger window. Based on our debugging requirement, we can show and hide properties in our debugging session using one of the useful attribute named DebuggerBrowsableAttribute . Let’s go ahead and see debug window for one of the code snippet: In above image you will notice that by default list of Awards is not in expanded form. In order to view the values of awards, one needs to expand that forcibly either by hovering the mouse or by click on the plus symbol, which will result in below screenshot: How to hide unwanted properties during debugging session? What if a developer is not interested in viewing employee’s branch? Well, that unwanted property can be made hidden by using DebuggerBrowsableState as Never as shown below: Now let’s run the code and check, whether it is h...

Customizing debugging session - Part I

I hope, being a developer everyone needs to debug their code at least once a day even if it is a very small snippet. Frankly speaking, sometimes it becomes very frustrating when we are looking for a value of particular property or let’s say very few properties of a huge object and it gets TIMEOUT. Uhhh !!! This time, we feel like there should be some easy way to navigate to that particular property instead of going to the object and clicking on plus symbol to reach the required property. Well, let’s understand it via code: Aim: I have an Employee class with two members as EmployeeName and BranchName. I want to know the name and branch of an Employee during my debugging session. So, I start debugging and lend up on below screen: Now in order to view the required details, I need to expand the employee object as shown in below screenshot: Now first question is, is there any way to display customized message in debugger window?  Answer wo...

Automatic Numbering for the Primary Key Column

Many of you might have come across an issue of Auto incrementing while working with database products. Truly speaking, recently I also came across one silly problem and thought of sharing it here. Imagine that at some point of time, you might want to send your new data to a back-end database. If your application supplies the auto-increment values, then what will the database do? what if application receives duplicate values from different client applications? The answer is that these auto-increment values are never sent to the database because the auto-increment column in the database table will provide a value whenever a new row is added. After each new row is added, the back-end database table generates a new-increment number and then your application will query the database to get the newly created number. Your application will then update its primary key number to the values that came from the database. This means that all the foreign key references will need t...

Const and Readonly keyword

Both these words play a very important role in defining constants in an application (C#). At one sight, it seems like, both are same but exactly it is not he case. Let's understand one by one to get the clear picture. The word const itself means, it will never change. If you are specifying any variable as a  const t hat means the value of the variable is never going to change inside the application. How we declare a constant is, by using a  const  keyword. Basically, one can define  const  only on the primitive types, like int, double, etc. One should make sure that the value should be assigned at the time of declaration itself and another important thing is whatever value is set for  const  variable, that value will be set at the compile time itself and this value will get stored inside a .dll or an .exe. In later part, I'll show u on how we can see this value inside a dll or an exe using an Ildasm. Sample code to define  const   v...