- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
sub company_stuff_add_array_elems
{
my $list = shift;
my $count = 0;
foreach ( @$list )
{
$count += $_;
}
return $count;
}
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
−172
sub company_stuff_add_array_elems
{
my $list = shift;
my $count = 0;
foreach ( @$list )
{
$count += $_;
}
return $count;
}
List::Util::sum
http://perldoc.perl.org/List/Util.html
−89
def set_path
if name && (!path || path == "/")
self.path = self.parent.present? ? "#{self.parent.path}/#{name}" : "/#{name}"
elsif !new_record? && name && path && name_was != name
parts = path.split("/")
parts.pop
self.path = [parts.join("/"), name].join("/")
elsif !new_record? && name && self.parent_id_changed?
self.path = self.parent.present? ? "#{self.parent.path}/#{name}" : "/#{name}"
elsif new_record? && name && path
self.path = [path, name].join("/")
end
if path && self.parent.blank?
parts = path.split("/")
self.name = parts.pop
parent_path = parts.join("/")
if parent_path.blank? || parent_path == "/"
self.parent = nil
else
possible_parent = site.asset_folders.find_by_path(parent_path)
self.parent = possible_parent.present? ? possible_parent : self.class.create(path: parent_path, site: site)
end
end
true
end
Как не нужно работать с путями в Rails-приложении. Это и еще примерно 500 строк было заменено на 11 строк кода, включая пустые.
+10
static int
_S_compare(size_type __n1, size_type __n2)
{
const difference_type __d = difference_type(__n1 - __n2);
if (__d > __gnu_cxx::__numeric_traits<int>::__max)
return __gnu_cxx::__numeric_traits<int>::__max;
else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
return __gnu_cxx::__numeric_traits<int>::__min;
else
return int(__d);
}
+71
public static Connection getDBConnectionReader() throws Exception {
Connection conn = null;
try {
conn = enrollmentsDataSource.getConnection();
} catch (SQLException e) {
LOGGER.warn("SQL Exception: get DB connection reader", e);
resetConnectionReader();
try {
conn = enrollmentsDataSource.getConnection();
} catch (SQLException e1) {
throw new Exception("Exception: get DB connection reader", e1);
}
}
return conn;
}
Фрактал...
+28
class atoi_func
{
public:
atoi_func(): value_() {}
inline int value() const { return value_; }
inline bool operator() (const char *str, size_t len)
{
value_ = 0;
int sign = 1;
if (str[0] == '-') { // handle negative
sign = -1;
++str;
--len;
}
switch (len) { // handle up to 10 digits, assume we're 32-bit
case 10: value_ += (str[len-10] - '0') * 1000000000;
case 9: value_ += (str[len- 9] - '0') * 100000000;
case 8: value_ += (str[len- 8] - '0') * 10000000;
case 7: value_ += (str[len- 7] - '0') * 1000000;
case 6: value_ += (str[len- 6] - '0') * 100000;
case 5: value_ += (str[len- 5] - '0') * 10000;
case 4: value_ += (str[len- 4] - '0') * 1000;
case 3: value_ += (str[len- 3] - '0') * 100;
case 2: value_ += (str[len- 2] - '0') * 10;
case 1: value_ += (str[len- 1] - '0');
value_ *= sign;
return value_ > 0;
default:
return false;
}
}
private:
int value_;
};
standard atoi()
79142 milliseconds
class atoi_func
131 milliseconds.
Если приходится велосипедить стандартные функции, то это камень в огород С++. Видать кресты писали гении ассемблерной оптимизации.
+27
inline double poly(double x, const double *c, int k) const {
double y = c[k];
switch (k) {
case 15: y = y * x + c[14];
case 14: y = y * x + c[13];
case 13: y = y * x + c[12];
case 12: y = y * x + c[11];
case 11: y = y * x + c[10];
case 10: y = y * x + c[ 9];
case 9: y = y * x + c[ 8];
case 8: y = y * x + c[ 7];
case 7: y = y * x + c[ 6];
case 6: y = y * x + c[ 5];
case 5: y = y * x + c[ 4];
case 4: y = y * x + c[ 3];
case 3: y = y * x + c[ 2];
case 2: y = y * x + c[ 1];
case 1: y = y * x + c[ 0];
case 0: break;
}
return y;
}
Схема Горнера для вычисления значения многочлена в точке
+151
function renderLayout(layout, column, colidx, restrictions) {
//...
if (!colidx) {
var flag = 1 << 0 | 1 << 1 | layout.title.type << 3;
if (layout.title.split) flag |= 1 << 2;
}
//...
}
Магические преобразования... или как стать незаменимым сотрудником!
+103
private static bool IsSourceField(DataRow row, string fieldName)
{
try
{
object fieldValue = row[fieldName];
return false;
}
catch
{
return false;
}
}
Бизнес логика.
−139
CREATE TABLE `forums_list`(
`forum_id` int unsigned NOT NULL AUTO_INCREMENT,
...
`created` datetime NOT NULL DEFAULT NULL,
`updated` datetime NOT NULL DEFAULT NULL,
+52
//--Корзина заказа
$misc_basket=array();
$misc_basket['num']=0;
//--проверка наличия в корзине товаров
$b_sum=0;
$b_num=0;
//var_dump($_SESSION['goods']);
if(isset($_SESSION['goods']))
{
// echo "11";
foreach(explode("^^^",$_SESSION['goods']) as $item)
{
$arr1=explode(":::",$item);
$type="";
if($arr1[2]=='disc')$dbs="disc";
elseif($arr1[2]=='tire') $dbs="shina";
else $dbs="truck_shina";
$iddb=abs($arr1[0]);
// echo "select `price` from `".PREFIX."$dbs` where `id`=$iddb";
$res=$db->query("select `price` from `".PREFIX."$dbs` where `id`=$iddb");
if($res->num_rows)
{
$arr=$res->fetch_array();
$b_sum+=intval($arr1[1])*correct_double($arr['price']);
$b_num+=$arr1[1];
}
}
}
$misc_basket['num']=$b_num;
$misc_basket['sum']=$b_sum;