www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - [OT] What is more readable?

reply simendsjo <simen.endsjo pandavre.com> writes:
Continuing "my what is more readable" thread (just shut me up, but I 
don't always agree with i, j, k etc...):

std.string.count:

size_t count(string s, string sub)
{
     size_t i;
     int j;
     int count = 0;

     for (i = 0; i < s.length; i += j + sub.length)
     {
         j = indexOf(s[i .. s.length], sub);
         if (j == -1)
             break;
         count++;
     }
     return count;
}




size_t count(string s, string sub)
{
     int result = 0;

     int subStart = 0;
     for (size_t restStart = 0; restStart < s.length; restStart += 
subStart + sub.length)
     {
         auto rest = s[restStart .. $];
         subStart = indexOf(rest, sub);

         bool notFound = (subStart == -1);
         if (notFound)
             break;

         result++;
     }

     return result;
}
Aug 09 2010
parent reply Jonathan M Davis <jmdavisprog gmail.com> writes:
On Monday, August 09, 2010 17:20:07 simendsjo wrote:
 Continuing "my what is more readable" thread (just shut me up, but I
 don't always agree with i, j, k etc...):
Personally, I think that i is just fine in many cases where it's quite clear what you're doing. e.g. the standard for loop: for(size_t i = 0; i < a.length; ++i) //whatever we do with a[i]... foreach does reduce how often that sort of thing is necessary though. However, once you get beyond i, and maybe j, it just gets confusing (not to mention the fact that i and j look a fair bit alike). So, personally, I avoid going beyond i, and I don't use i unless it's quite clear what I'm doing. Other than that, I find clearly named variables make the code much easier to read and understand - especially if someone else wrote the code, or you haven't read it in a while. - Jonathan M Davis
Aug 09 2010
parent reply simendsjo <simen.endsjo pandavre.com> writes:
On 10.08.2010 02:40, Jonathan M Davis wrote:
 On Monday, August 09, 2010 17:20:07 simendsjo wrote:
 Continuing "my what is more readable" thread (just shut me up, but I
 don't always agree with i, j, k etc...):
Personally, I think that i is just fine in many cases where it's quite clear what you're doing. e.g. the standard for loop: for(size_t i = 0; i< a.length; ++i) //whatever we do with a[i]... foreach does reduce how often that sort of thing is necessary though. However, once you get beyond i, and maybe j, it just gets confusing (not to mention the fact that i and j look a fair bit alike). So, personally, I avoid going beyond i, and I don't use i unless it's quite clear what I'm doing. Other than that, I find clearly named variables make the code much easier to read and understand - especially if someone else wrote the code, or you haven't read it in a while. - Jonathan M Davis
I also use "i" in my for loops. Always! But I still find it difficult to understand when there are more than one index in use. The thing here is that I couldn't understand the function top to bottom.. "j" was used both by the incrementer in the for loop, in the indexOf and the if statement. I had to get to the indexOf before I could understand what "j" was. Of course, I'm just pointing out very small areas of potential improvement as I've only covered 5% of the spec and 1% of the stdlib :)
Aug 09 2010
parent reply simendsjo <simen.endsjo pandavre.com> writes:
On 10.08.2010 02:53, simendsjo wrote:
 On 10.08.2010 02:40, Jonathan M Davis wrote:
 On Monday, August 09, 2010 17:20:07 simendsjo wrote:
 Continuing "my what is more readable" thread (just shut me up, but I
 don't always agree with i, j, k etc...):
Personally, I think that i is just fine in many cases where it's quite clear what you're doing. e.g. the standard for loop: for(size_t i = 0; i< a.length; ++i) //whatever we do with a[i]... foreach does reduce how often that sort of thing is necessary though. However, once you get beyond i, and maybe j, it just gets confusing (not to mention the fact that i and j look a fair bit alike). So, personally, I avoid going beyond i, and I don't use i unless it's quite clear what I'm doing. Other than that, I find clearly named variables make the code much easier to read and understand - especially if someone else wrote the code, or you haven't read it in a while. - Jonathan M Davis
I also use "i" in my for loops. Always! But I still find it difficult to understand when there are more than one index in use. The thing here is that I couldn't understand the function top to bottom.. "j" was used both by the incrementer in the for loop, in the indexOf and the if statement. I had to get to the indexOf before I could understand what "j" was. Of course, I'm just pointing out very small areas of potential improvement as I've only covered 5% of the spec and 1% of the stdlib :)
And on the other hand; I doubt anything of what I write will ever get into phobos! Of course.. Nothing should without careful reviewing :), but after I've just looked at a small part of the source, I hear it screams at me! What I've seen so far (very, very little) is actually very good, short, concise, readable code. I'm pretty sure I'll learn a lot from the phobos source! But as any codebases it shows age. There are many different coding styles, commented out code, i/j/k/etc. I'm just trying to put focus on the little things as I walk through the source.
Aug 09 2010
parent bearophile <bearophileHUGS lycos.com> writes:
simendsjo:
 And on the other hand; I doubt anything of what I write will ever get 
 into phobos!
If you exercise writing D code for few months you will be able contribute to Phobos2. Bye, bearophile
Aug 10 2010