- 1
<span style="display: block">
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
+139
<span style="display: block">
div? не, не слышал
+103
void str2ip_s_Before( unsigned char* ptrIP, unsigned short* ptrPort, const wchar_t* src, size_t count )
{
int i;
if( !ptrIP )
return;
while( src && count && !iswdigit( *src ) )//skip separators and spaces
++src, --count;
for( i = 0; i < 4 && count; ++i )//IP address
{
ptrIP[ i ] = ( unsigned char )_wtoi( src );
while( src && count && iswdigit( *src ) )//skip current number
++src, --count;
while( src && count && !iswdigit( *src ) )//skip separators and spaces
++src, --count;
}
if( ptrPort && src && count )
*ptrPort = ( unsigned short )_wtoi( src );
}
int str2ip_sAfter( unsigned char* ptrIP, unsigned short* ptrPort, const wchar_t* src, size_t count )
{
int i, iRadix;
wchar_t* endptr;
if( !ptrIP || !src || 0 == count )
return 0;
for( i = 0; i < 4; ++i )
ptrIP[ i ] = 0;
if( ptrPort )
*ptrPort = 0;
while( 0 != count && !iswdigit( *src ) ) //skip separators and spaces
++src, --count;
for( i = 0; i < 4 && 0 != count; ++i ) //IP address
{
if( count > 1 && src[ 0 ] == L'0' )
{
if( src[ 1 ] != L'x' && src[ 1 ] != L'X' )
iRadix = 8;
else
iRadix = 16;
}
else
iRadix = 10;
ptrIP[ i ] = ( unsigned char )wcstol( src, &endptr, iRadix );
if( src == endptr )
return 0;
count -= endptr - src;
src = endptr;
while( 0 != count && iswspace( *src ) ) //skip spaces
++src, --count;
if( i < 3 )
{
if( L'.' == *src ) //skip point
++src, --count;
else
return 0;
}
while( 0 != count && iswspace( *src ) ) //skip spaces
++src, --count;
}
if( i < 4 )
return 0;
if( ptrPort )
{
if( 0 == count || L':' != *src ) //skip point
return 0;
++src, --count;
while( 0 != count && iswspace( *src ) ) //skip spaces
++src, --count;
*ptrPort = ( unsigned short )wcstol( src, &endptr, 10 );
if( src == endptr )
return 0;
else
return 1;
}
else
return 1;
}
Пуресишник зарефакторил
−157
class SomeModel < ActiveRecord::Base
after_save :some_method
def some_method
self.save
end
end
Рекурсивная рекурсия рекурсивна.
+57
function GetOrderSkidka(&$arrData)
{
if($this->flag_opt){
$arrData['skidka'] = ($this->admin_mode) ? $arrData['skidka'] : 0;
$arrData['allsum'] = $arrData['sum'] - $arrData['skidka'];
return;
}
if(!$this->flag_in_action){
$arrData['cnt_s_prod'] = $arrData['cnt'];
}elseif(in_array($this->flag_action_type,array(2,3))){
$this->calcCntProd($arrData);
}else{
$arrData['cnt_s_prod'] = 0;
}
$this->discount->GetOrderSkidka($arrData);
}
Работаю с сайтом, в котором все методы классов работают со своими параметрами таким образом.
Метод может ничего не возвращать, а вызывать другие методы (которые также могут вызывать какие-то методы),
которые в зависимости от множества условий меняют переданные по ссылке параметры.
Итог работы модифицированный параметр- массив. Только XDebug выручает.
+121
try {
repository.saveAll(entities);
} catch (final ConstraintViolationException e) {
throw new SettingsServiceException(ErrorFormatter.formatValidationErrors(
"Errors have been detected when saving", e));
} catch (final PersistenceException e) {
for (val t: Throwables.getCausalChain(e)) {
if (t instanceof EntityExistsException) {
val failedObject = (IEntity) ((ExceptionInfo) t).getFailedObject();
// failedObject is returned with the state in which it is in the database.
// Let's find the version that we tried to save, instead.
for (val entity: entities) {
if (entity.getId() != null && entity.getId().equals(failedObject.getId())) {
throw new SettingsServiceException("Attempted to save duplicate value: " + entity);
}
}
}
}
throw e;
}
Попытка вернуть пользователю человеко-читаемое сообщение о том, где, собственно, дубликат.
Костыли вы мои, костыли...
+54
<?php
class SimpleController extends Zend_Controller_Action
{
public function init()
{
$action = $this->_request->getActionName();
$is_action_available = in_array($action, explode(',', 'terms,privacy,about,copyright,support,legal,features,help,refund'));
$is_incorrect_action = CR_Settings::siteIsFree() && $action == 'refund';
if (!$is_action_available || $is_incorrect_action)
$this->_helper->redirector->setGotoRoute(array('action' => 'index'), 'default', true);
}
public function termsAction(){
}
public function privacyAction(){
}
public function aboutAction(){
}
public function copyrightAction(){
}
public function supportAction(){
}
public function legalAction(){
}
public function featuresAction(){
}
public function helpAction(){
}
public function refundAction(){
}
}
Zend Framework проверка на существование action :)
+26
result.push_back(TVector<2>
(
(A-√(D))/C,
(E-Line.K()*√(D))/C
));
result.push_back(TVector<2>
(
(A+√(D))/C,
(E+Line.K()*√(D))/C
));
+131
if (http_status_code == 200 || http_status_code == 500) {
.... //success!!
} else {
.... // error!!
}
Было замечено в одном андроид приложении.
+141
var number = new Random();
if (number.Next(10)%2 == 0)
{...}
else
{...}
Нашёл такой шедевр в индокоде, которые мне дали модернизировать.
+57
function SetImage($row=array())
{
foreach($row as $property=>$value)
{
eval("\$this->".$property." = '".$value."';");
}
}