Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!rice!uw-beaver!mit-eddie!andante!alice!bs From: bs@alice.att.com (Bjarne Stroustrup) Newsgroups: comp.object Subject: Re: Documenting OO Systems Keywords: operators Message-ID: <20147@alice.att.com> Date: 30 Mar 91 14:17:34 GMT References: <299@orbit.gtephx.UUCP> Organization: AT&T Bell Laboratories, Murray Hill NJ Lines: 61 pallas@eng.sun.com (Joseph Pallas) writes > In jls@rutabaga.Rational.COM (Jim Showalter) > writes: > > >Okay, what is this going to do?: > > > int j (int y, int m, int d) { > > int m_d[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; > > if (m<1 || m>12) return 0; > > int l = !(y%4) && y&400; > > if (d<1 || d>(m_d[m-1] + (m==2 && y))) return 0; > > int dd = 0; > > for (int i=0; i > return dd + d + (m>2 && y); > > } > > >You should be able to tell me right off the bat--you think English sucks > >as a programming language, and this is DEFINITELY not written in English. > > I can't believe I'm about to waste my time on this, but here goes: > > int j (int year, int month, int day) { > int month_days[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; > if (month<1 || month>12) return 0; > int leap = !(year%4) && year&400; > if (day<1 || day>(month_days[month-1] + (month==2 && year))) return 0; > int dayofmonth = 0; > for (int i=0; i return dayofmonth + day + (month>2 && year); > } > > I've left the name of the function cryptic, but the rest is the result > of a simple global substitution. It becomes immediately apparent not > only what the function is supposed to do, but that it contains at > least one error (surely you meant to check (month==2 && leap)). > > Funny thing is, the function is still written in C. > > Amazing, isn't it? Actually, we discover that the function was never written in C. Note the way variables are declared exactly where they are needed. This is C++. Also, total lack of indentation to make the code more obscure is not too common in the C/C++ world. int j (int year, int month, int day) { int month_days[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; if (month<1 || month>12) return 0; int leap = !(year%4) && year&400; if (day<1 || day>(month_days[month-1] + (month==2 && year))) return 0; int dayofmonth = 0; for (int i=0; i2 && year); } A comment or two would also help.