Peter Bahaa's Blog

Business and Technical Articles

Coding Standards and Best Practices : Dealing with strings Part (1)

August 18
by Peter Bahaa 18. August 2010 21:57

Week ago i started a series of blog posts about coding standards and best practices and i will be posting those post between my personal and Integrant blogs, as one of the most things i love about Integrant is that all the developers are concerned about using coding standards and best practices and approaches,

As best practices and coding standards works like a contract between the developers to ensure maintainability, readability, performance and usability, for example I’m writing a piece of code in a certain module and for whatever reason I will not be able to continue coding in that nodule, and another developer need to do some changes in the code due some changes in customer requests, do if this code being wrote with coding standards, the other developer will be easily able to read it, and using best practices for sure will help that developer to maintain this code easily, and even can reuse it in another module, Most of developers can write “Working Code” but not “Efficient Code”.

and this time a common issue i have faced working with Mid-Level and Junior developers is dealing with strings,  like the following example

1 string userName = "Peter Bahaa"; 2 string message = "Hi Mr. ( " + userName + ")";

The issue is that adding + operator after the “Hi Mr.” string initializing another object in the memory, Perhaps concatenating two string with plus format will not affect your application performance, but imaging that each time you want to concatenate two string together - for any purpose (special formatting , display a specific message) – by adding a plus will create another object in the memory , and simply by using String.Format that will enhance your application performance, so there are a great function called String.Format this function is simply doing formatting and decoration for your string, and also you can set the culture for this formatting so you can use it when in multi language application development. for example

1 string message = String.Format(CultureInfo.CurrentCulture, "Hi {0} {1}; your birthday is: {2:MMMM dd yyyy} and you won {3:C3}", title, userName, birthdate, prize);

So using String.Format will ease the job and will grantee no more object creation in the memory For more info about String.Format see the following link http://blog.stevex.net/string-formatting-in-csharp/

One more thing about dealing with strings is when we just want to concatenate two or more string or a hug number of strings

for small number of strings you can use String.Concat and never use the + operator to concatenate strings

1 String.Concat("Peter", " ", "Bahaa")

and for large amount of strings always use StringBuilder Class

1 StringBuilder builder = new StringBuilder(); 2 builder.Append("Message \n\r"); 3 builder.AppendFormat(CultureInfo.CurrentCulture, "Hi {0} {1}; your birthday is: {2:MMMM dd yyyy} and you won {3:C3}", title, userName, birthdate, prize); 4 Console.Write(builder.ToString());
I hope this helps :) , Have a nice day every one.

Tags:

C# | Best Practices | Coding Standards | Tips & Tricks | Technical

Comments

7/27/2011 11:18:13 PM #

slimming Series oucik trade online sales and wholesale, cheap price. Provide OEM and ODM services. Welcome to Consult and Buy。

Ouick

7/30/2011 5:11:38 AM #

Ohh its really cool to have such amazing post Thanks and regards...
<a href="www.phlebotomytrainingonline.org/">Phlebotomy classes</a>.

Phlebotomy classes

7/30/2011 5:12:00 AM #

Ohh its really cool to have such amazing post Thanks and regards...
<a href="www.phlebotomytrainingonline.org/">Phlebotomy classes</a>.

Phlebotomy classes

9/17/2011 12:02:02 PM #

Genuinely educational thanks, I do think your trusty readers may very well want significantly more posts like that carry on the great content.

John Jefferson United States

9/26/2011 11:06:23 PM #

Thanks for your article. I have usually seen that the majority of people are needing to lose weight simply because they wish to appear slim in addition to looking attractive. Nonetheless, they do not always realize that there are additional benefits so that you can losing weight also. Doctors state that obese people are afflicted with a variety of illnesses that can be directly attributed to their own excess weight. Thankfully that people who\'re overweight in addition to suffering from numerous diseases can help to eliminate the severity of their own illnesses through losing weight. You\'ll be able to see a continuous but identifiable improvement with health if even a negligible amount of fat reduction is achieved.

One thing I would like to touch upon is that weightloss routine fast can be carried out by the appropriate diet and exercise. A person\'s size not just affects the look, but also the overall quality of life. Self-esteem, major depression, health risks, as well as physical ability are affected in extra weight. It is possible to do everything right whilst still having a gain. Should this happen, a medical problem may be the culprit. While an excessive amount food and not enough exercise are usually at fault, common health conditions and traditionally used prescriptions may greatly increase size. Thanks for your post here.

I acquired more new things on this weight-loss issue. A single issue is that good nutrition is especially vital whenever dieting. A tremendous reduction in bad foods, sugary foods, fried foods, sugary foods, pork, and bright flour products may be necessary. Having wastes bloodsuckers, and harmful toxins may prevent goals for shedding fat. While selected drugs quickly solve the problem, the bad side effects will not be worth it, and so they never present more than a non permanent solution. It is just a known proven fact that 95% of dietary fads fail. Many thanks for sharing your thinking on this weblog.

Thanks for the points shared on your blog. Another thing I would like to convey is that fat loss is not about going on a dietary fad and trying to shed as much weight as possible in a couple of weeks. The most effective way to shed weight is by getting it slowly but surely and following some basic ideas which can make it easier to make the most through your attempt to shed pounds. You may know and be following these tips, but reinforcing knowledge never hurts.

I believe  avoiding packaged foods is a first step to lose weight. They might taste good, but refined foods include very little nutritional value, making you take more to have enough electricity to get throughout the day. In case you are constantly eating these foods, transitioning to grain and other complex carbohydrates will let you have more vigor while consuming less. Interesting blog post.

dspangler731

10/3/2011 9:31:08 AM #

Good read, I just passed this onto a colleague who had been doing a minor research on that. Together with he definitely bought me lunch because I found it for him smile So let me rephrase that: Thanks for lunch!

How To Diet

10/3/2011 10:26:10 AM #

Love the article.  I will gladly post a do follow trackback if this comment is approved.  Also - feel free to use our hcg diet recipes on your site.

Diet Recipes Online

10/3/2011 10:26:30 AM #

Love the article.  I will gladly post a do follow trackback if this comment is approved.  Also - feel free to use our hcg diet recipes on your site.

Low Fat HCG Recipes

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar