荒谬的代码或“如何不写”

最近,我分享了一篇帖子,其中收集了源代码中的有趣评论,并吸引了读者。然后我决定,为什么不对弯曲的代码进行类似的选择呢?谁在乎,欢迎来猫。



像上一次一样,我对这篇文章的启发是由于对Quora的一个非常普遍的问题,即:您见过的最荒谬的代码是什么?(原始。您见过的最荒谬的代码是什么?)


该问题是几年前发布的,但每个人都继续到达那里。我将在下面分享其中的一些。


Prashant Asthana的回复


“ ”


MNC. , « Java» « ». , .


: , . , ?
: .


10 , :


int largestNumber = 0;
for (int i = 0; i < array.length - 1; i++) {
 largestNumber =
      array[i] > array[i+1] ?
          array[i] : array [i+1];
}

System.out.println(largestNumber);

, .


: , , , ?


5 .


: , .
: ?
: 1,2,3,4,5
( ): .


5 .


: , , . .


int largestNumber = 0;
for (int i = array.length - 1; i > 0; i--) {
 largestNumber =
      array[i] > array[i-1] ?
         array[i] : array [i-1];
}

System.out.println(largestNumber);

: 5,4,3,2,1. .



: ?
: ...


Thomas Breckinridge


, . , , , .


, , haht. - . , hahthahthaht hahtHahthaht, hahthahthaHt hahthAhthahT.


, , ,


if (hahthAhthahT >= hahthahthaht ) then hahtHahthaht(hahtHahtHaht,HAhtHahthaht);
else
 hahTHahthaht(hahtHahtHaht,HAhtHahthaht);

bitbucket.


:
, Borland Delphi/Object Pascal, , Pascal , . , C ++ Builder, , , haht. Borland , VisualBasic, Win32 API C / C ++. , , .


Alan Chavez


- JavaScript:


var obj = "{\"firstname\":\"" + firstName + "\",
\"lastname\":\"" + lastName + "\"}";
var res = JSON.parse(obj);
return res;

, JSON… JavaScript!
JSON JavaScript. .
, , , , : « ».


1 36 , , (O’Conelly), . 2 , .
«VP of Engineering». .


Ross Dickey



, , :


  1. (« » , )
  2. CamelCase ( Python, C#)
  3. ,
  4. , if
  5. , ,
  6. 5 , > 16. . > = 17 ,
  7. ,
  8. (?!?)
  9. . Python, C++. .

, - ?


Yoseph Radding


, . , . . 10- , junior . , - , .


, :


function foo(a) {
   if (a) {
       return transform(a);
   }
   return transform(a);
}

. , . … .
, . 3 , .


1 , .


Ryan Lam


#!/usr/bin/sh
#  Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
#  All Rights Reserved

#  THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
#  The copyright notice above does not evidence any
#  actual or intended publication of such source code.

#ident  "@(#)true.sh  1.6  93/01/11 SMI"  /* SVr4.0 1.4  */

/ bin / true - UNIX-, AT&T. true — , . ( , , while true ..) “” .


, AT&T, - , / bin / true AT&T UNIX. , , .


, , . AT&T ….


Khaled Bakhit


rows= SELECT * FROM users
int count= 0
for each row in rows
   count= count + 1
return count

API .


( ), select . , , .


, !


, ( , ). Count, .


rows= SELECT * FROM users
int count= 0
for each row in rows
  count = count + 1

checkCount= SELECT count(*) FROM users

if count != checkCount
   throw Error
return count

该代码片段经常会导致异常,因为在执行第一个计数器时,表中已充满了更多记录,从而为第二个计数器赋予了不同的值...


真实的故事。我希望不是这样。


结论


此处阅读更多答案好吧,按照传统,分享您在实践中遇到的荒谬/愚蠢/奇怪代码的选择。我认为阅读不仅对我很有趣,对所有偶然发现本文的人也很有趣:)


All Articles