Click here to Skip to main content
16,020,840 members
Articles / General Programming / String
Tip/Trick

Common Mistakes Made by Programmers

Rate me:
Please Sign up or sign in to vote.
3.16/5 (21 votes)
22 Oct 2010CPOL1 min read 40.6K   6   22
Common Mistakes Made by Programmers
Introduction

Making mistakes is inevitable in programming. Even a small mistake could prove to be very costly. The wise thing is to learn from your mistakes and try not to repeat them.

In this article I will be highlighting the one of the mistakes which I consider to be the most common mistake made by C# developers.

Formatting a string

There lies the strong possibility of making costly mistakes while working with string types in C# programming. String is always an immutable type in the .NET Framework. When a string is modified it always creates a new copy and never changes the original. Most developers always format the string as shown in the sample below

<br />
string updateQueryText = "UPDATE EmployeeTable SET Name='" + name + "' WHERE EmpId=" + id;<br />
<br />


The above code is really messy and also as the string is immutable it creates 3 unnecessary garbage string copies in the memory as a result of multiple concatenations.

The better approach is to use string.Format as it internally uses StringBuilder which is mutable. It also paves the way for a clean code.

<br />
string updateQueryText = string.Format("UPDATE EmployeeTable SET Name='{0}' WHERE EmpId={1}", name, id);<br />


Keep watching for some more tips....
Eswar

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
I have good knowledge on Application Development, Developing components, Participating in design workshops and reviews. Particularly interested in client/server and windows applications. i have working experience with Oracle 10g, 11g, PostgreSQL, and MS-SQL Server Databases.

Comments and Discussions

 
GeneralReason for my vote of 4 nice tip Pin
MessiNRG12-Feb-12 5:03
MessiNRG12-Feb-12 5:03 
GeneralReason for my vote of 4 Good Tip Pin
Sandesh M Patil3-Dec-10 2:31
Sandesh M Patil3-Dec-10 2:31 
GeneralReason for my vote of 1 It's a total mistake to concatenate.... Pin
ArchElf27-Oct-10 9:32
ArchElf27-Oct-10 9:32 
GeneralReason for my vote of 1 bad sql script Pin
yunusatmaca27-Oct-10 3:51
yunusatmaca27-Oct-10 3:51 
GeneralYes.I completely agree. Pin
GPUToaster™26-Oct-10 22:38
GPUToaster™26-Oct-10 22:38 
GeneralReason for my vote of 1 If you're going to demonstrate strin... Pin
Richard Deeming26-Oct-10 8:15
mveRichard Deeming26-Oct-10 8:15 
Generaloops I was meant to say: string text = tbStatic.text + " ... Pin
Blubbo26-Oct-10 3:11
Blubbo26-Oct-10 3:11 
GeneralReason for my vote of 5 Thanks for the tip! I've never used ... Pin
Blubbo26-Oct-10 3:09
Blubbo26-Oct-10 3:09 
GeneralReason for my vote of 1 This is SQL injection waiting to hap... Pin
jim lahey26-Oct-10 2:16
jim lahey26-Oct-10 2:16 
GeneralReason for my vote of 3 Information is not full. It contains... Pin
Anton Kratenok25-Oct-10 22:24
Anton Kratenok25-Oct-10 22:24 
GeneralReason for my vote of 5 practible Pin
Semions25-Oct-10 19:07
Semions25-Oct-10 19:07 
GeneralReason for my vote of 3 If you expect to execute the line of... Pin
dmjm-h25-Oct-10 12:38
dmjm-h25-Oct-10 12:38 
GeneralIt may also be good to mention the StringBuilder.AppendForma... Pin
supercat925-Oct-10 6:10
supercat925-Oct-10 6:10 
GeneralStringBuilder isn't magic. If it has to expand its internal ... Pin
Henry.Ayoola25-Oct-10 3:58
Henry.Ayoola25-Oct-10 3:58 
GeneralReason for my vote of 2 it's a good pract Pin
Herre Kuijpers23-Oct-10 3:18
Herre Kuijpers23-Oct-10 3:18 
Generalmy intension is mainly using string.Format() Method.. we can... Pin
TweakBird23-Oct-10 2:58
TweakBird23-Oct-10 2:58 
Generalsensible to SQL injection. Pin
Hiren solanki22-Oct-10 22:32
Hiren solanki22-Oct-10 22:32 
GeneralReason for my vote of 4 Nice use of string.Format Thanks for... Pin
Khaniya22-Oct-10 19:28
professionalKhaniya22-Oct-10 19:28 
GeneralYou must have mist this comic that humorously makes clear wh... Pin
E.F. Nijboer22-Oct-10 11:54
E.F. Nijboer22-Oct-10 11:54 
GeneralReason for my vote of 1 I gave you a 1 because your example ... Pin
SledgeHammer0122-Oct-10 10:09
SledgeHammer0122-Oct-10 10:09 
GeneralReason for my vote of 5 never knew stringbuilder is used ins... Pin
FadiYoosuf22-Oct-10 4:10
FadiYoosuf22-Oct-10 4:10 
GeneralReason for my vote of 4 Thanks for the info. It is really a ... Pin
Sivaraman Dhamodharan22-Oct-10 3:10
Sivaraman Dhamodharan22-Oct-10 3:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.