Alex Rivera | Logout

.net code readability and maintainability

Asked 2010-04-06T14:43:06.810
12

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?

Edit
Report

4 Answers

8

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

answered 2010-04-06T14:44:39.147
2

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.

answered 2010-04-06T14:45:42.483
0

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.

answered 2010-04-06T14:47:06.840
0

From an error handling standpoint, EndsWith.

answered 2010-04-06T14:52:11.530

Your Answer