.net code readability and maintainability

There Currently is a local debate as to which code is more readability

We have one programmer who comes from a c background and when that programmer codes it looks like

string foo = "bar";

if (foo[foo.Length - 1] == 'r')


We have another programmer that doesn't like this methodology and would rather use

if (foo.EndsWith("r"))


which way of doing these types of operations is better?


EndsWidth is more readable to someone who has never seen C or C++, C#, or any other programming language.

The second one is more declarative in style but I can't tell you objectively if it is more readable sine readability is very subjective. I personally find the second one more readable myself but that is just my opinion.

Here is an excerpt from my article:

Most C# developers are very familiar with writing imperative code (even though they may not know it by that name). In this article, I will introduce you to an alternative style of programming called declarative programming. Proper declarative code is easier to read, understand, and maintain.

As professionals, we should be striving to write better code each day. If you cannot look at code you wrote three months ago with a critical eye and notice things that could be better, then you have not improved and are not challenging yourself. I challenge you to write code that is easier to read and understand by using declarative code.

Number 2 is better to read and to mantain. Example: Verify the last 2 characters ...

Option 1)

if (foo[foo.Length - 1] == 'r' && foo[foo.Length - 2] == 'a')


Option 2)

if (foo.EndsWith("ar"))


last 3? last 4?...

I come from a C/C++ background and I vote for Endswith!

Readability rules, especially if it implies intent.

With the first example I must discover the intent - which is left for interpretation. If it appears to have a bug, how do I know that's not intentional?

The second example is telling me the intent. We want to find the end character. Armed with that knowledge I can proceed with evaluating the implementation.

I think the second way is better because it is more easy to read and because the first one duplicates logic of EndsWith method which is bad practice.

I think the right answer would be the one that is actually correct. EndsWith properly returns false for empty string input whereas the other test will throw an exception trying to index with -1.

Not only is EndWith more readable, but also more 'correct'. As a rule, if there is a framework method provided to do the job ... use it.

What if foo == string.Empty?

IMO, the intent of the original author is clearer in the second example. In the first, the reader must evaluate what the author is trying to accomplish by pulling the last index. It is not difficult, but requires more effort on the part of the reader.

Both approaches are valid, but the endswith method is easier to read in my opinion. It also does away with the potential to make typing mistakes etc with the more complicated form..

EndsWith is probably safer. But the indexer is probably faster.

Endswith probably checks to see if the input string is empty. They will probably both throw null reference exceptions. And the indexer will fail is the length is 0.

As for readability, they both say the same thing to me, but I have been programming for a while. The .EndsWith(...) is probably faster to grasp without considering context.

It pretty much does the same thing. However, it gets more complicated with more than one character in the endswith argument. However, the first example is slightly faster as it uses no actual functions and thus requires no stack. You might want to define a macro which can be used to simply make everything uniform.

I think the main criteria should be which of these most clearly says what the developer wants to do. What does each sample actually say?

1)Access the character at position one less than the length, and check if it equals the character 'r'

2)Check if it ends with the string "r"

I think that makes it clear which is the more maintainable answer.

Unless and until it does not affect the program performance, no problem you can use either way. But adding code comments is very important for conveying what is being accomplished.

"Code is written to be read by humans and incidently run by computers" SICP

EndsWith FTW!!



From an error handling standpoint, EndsWith.

I much prefer the second (EndsWith) version. It's clear enough for even my manager to understand!

The best practice is to write code that is easily readable. If you used the first one, developers that are debugging your code may say, "What is this dev trying to do?" You need to utilize methods that are easily explained. If a method is too complicated to figure out, retract several methods out of it.

I would definitely say the second one, legibility and simplicity are key!

Also, if the "if" statement has one line, DONT BOTHER USING BRACES, USE A SINGLE INDENTION

Remember that in classic C, the only difference between a "string" and an array of characters is that terminating null character '\0', so we had to more actively treat them accordingly and to make sure that we did not run off the end of the array. So the first block of code bases its thought process on the concept of an array of characters.

The second block of code bases the thought process on how you handle a string more abstractly and with less regard to its implementation under the covers.

So in a nutshell, if you are talking about processing characters as the main idea behind your project, go with the first piece. If you are talking about preparing a string for something greater and that does not necessarily need to focus on the mechanics of the ways that strings are built -- or you just want to keep it simple -- go with the second style.

Some of this might summarize others' contributions at this point, but more analogously put, are you playing "Bingo();" or are you "playing a game with a two-dimensional array of random integers, etc.?"

Hopefully this helps.


Need Your Help

Increment image name if already exists

php regex file filenames suffix

I'm using following code format to change image name if it already exists on server. I want incremental image names if they already exist with the same name.

How can I create a weekly calendar view for an Android Honeycomb application?

android layout android-layout android-3.0-honeycomb android-xml

I am working on an Android (v3.0) application that has a requirement of mimicking the weekly calendar layout found on Google Calendar: