c# - Is for keyword obsolete like goto in modern object-oriented languages? -


is for keyword obsolete or may become obsolete goto in languages c# or java? in few years, strange , suspicious see object-oriented code uses for, today, it's suspicious see gotos?

in other words, there reason know , use for in programs?


i notice there bunch of weaknesses for doesn't exist when using foreach:

'for' used , need it:

don't know scientific development, general stuff, software products , websites not dealing calculus, for used extremely rarely. i've seen , done many projects there no loop in thousands of lines of code. when dealing calculus, manipulating arrays, collections or matrices or ranges more frequent, elegant , useful loop.

a few places when seems for required, in fact, not, , collection-oriented solution may used instead.

reading source code of beginner developers, find for used .net framework does. example, fill array same value, repeated n times, people use loop, when must rather use enumerable.repeat().

sometimes, see loop used walk through array, collection or enumerable. foreach more elegant solution, , shorter write.

'for' not jimmy-proof:

probably i'm bad developer, often, when use for, make same mistake again , again: putting other predefined variable in loop range, this:

int somevalue {         {         // resources-expensive operation goes here.     } }  (int = 0; < this.somevalue; i++) {     // code here. }  // instead, less stupid developer write: int somevalue = this.somevalue; (int = 0; < somevalue; i++) {     // ... } 

of course, what's wrong this.somevalue called @ each iteration, wasting resources. example, evening, found terrible mistake i've done: loop used 0..n range n property making query database. surprising see in sql profiler the same query repeated 10 000 times.

'for' ugly:

maybe it's subjective, when manipulating collections time, isn't second solution more natural write/read?

// solution 1. c style. (int = 0; < 10; i++) {     // something. }  // solution 2. enumerable-oriented. foreach (var in enumerable.range(0, 10)) {     // something. } 

by way, find second 1 easier understand. difference between:

for (i = 0; < 10; i++) (i = 0; <= 10; i++) (i = 1; < 10; i++) (i = 1; <= 10; i++) 

easy see? whereas enumerable.range() accepts 2 parameters, extremely clear: first 1 - start, , number of elements.

what more elaborate code?

foreach (string containingtwo in enumerable.range(0, 10).     where(c => c.tostring().contains('2')).     select(c => c.tostring().padleft(8))) {     console.write(containingtwo); } 

'for' rigid:

it's difficult extend for. let's want display list of pages on website without using asp.net paging control. obvious solution display numbers for (int = 1; <= countpages; i++).

now, if requirements change , don't want display every page, first two, last two, current , nearest pages current?

if using collection, easy change. actually, loop, not.

'for' not linq friendly:

there plenty of things linq can do, avoiding writing hand. when use for, there no place linq. if there filtering do, must ourselves. if results must converted, must done on separate line. yes, of course, lambda expressions can used inside simple loop too, there no easy-to-read way write stuff in single, short line of code. sum or average not available, join or except or groupby.

in java (and other languages), still use for keyword "for each" loop:

for (string s : mylistofstring) {      // code code code } 

so might want rephrase question, if mean "do think we'll stop using for (int i=0; i<something; i++) loops, might right. in python, equivalent of for (int i=0;... loops (for in range(len(mylist))) discouraged iterating on collection (see delnan's comment -- put), more or less reasons mentioned in original question.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -