- 1
- 2
decimal retValue = <...>
var span = TimeSpan.FromMinutes(double.Parse(retValue.ToString()));
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
Всего: 116
+118
decimal retValue = <...>
var span = TimeSpan.FromMinutes(double.Parse(retValue.ToString()));
Почему бы просто не (double) retValue...
−134
SELECT ISNULL(SUM(dbo.colors.setup_time_base + dbo.colors.setup_time_add), 0) AS setup_time, ISNULL(SUM(dbo.colors.setup_time_base), 0) AS setup_time_base,
ISNULL(SUM(dbo.colors.setup_time_add), 0) AS setup_time_add, ISNULL(SUM(dbo.colors.setup_length_base), 0) + 40 AS setup_lenght_base,
ISNULL(SUM(dbo.colors.setup_lenght_add), 0) AS setup_lenght_add, ISNULL(SUM(dbo.colors.setup_length_base + dbo.colors.setup_lenght_add), 0)
+ 40 AS setup_lenght, ISNULL(COUNT(dbo.dir_order_sections.color_id), 0) AS section_count, CASE WHEN COUNT(dbo.dir_order_sections.color_id)
= 0 THEN 0.5 WHEN COUNT(dbo.dir_order_sections.color_id) >= 1 AND COUNT(dbo.dir_order_sections.color_id)
<= 2 THEN 0.65 WHEN COUNT(dbo.dir_order_sections.color_id) >= 3 AND COUNT(dbo.dir_order_sections.color_id)
<= 6 THEN 1 WHEN COUNT(dbo.dir_order_sections.color_id) >= 7 AND COUNT(dbo.dir_order_sections.color_id)
<= 10 THEN 1.15 WHEN COUNT(dbo.dir_order_sections.color_id) >= 11 THEN 1.3 ELSE 0 END AS section_rate, CASE WHEN COUNT(dbo.dir_order_sections.color_id)
= 0 THEN 0.03 WHEN COUNT(dbo.dir_order_sections.color_id) >= 1 AND COUNT(dbo.dir_order_sections.color_id)
<= 2 THEN 0.04 WHEN COUNT(dbo.dir_order_sections.color_id) >= 3 AND COUNT(dbo.dir_order_sections.color_id)
<= 7 THEN 0.05 WHEN COUNT(dbo.dir_order_sections.color_id) >= 8 AND COUNT(dbo.dir_order_sections.color_id)
<= 10 THEN 0.07 WHEN COUNT(dbo.dir_order_sections.color_id) >= 11 THEN 0.07 ELSE 0 END AS role_rate, CASE WHEN COUNT(dbo.dir_order_sections.color_id)
= 0 THEN 0.03 WHEN COUNT(dbo.dir_order_sections.color_id) >= 1 AND COUNT(dbo.dir_order_sections.color_id)
<= 2 THEN 0.04 WHEN COUNT(dbo.dir_order_sections.color_id) >= 3 AND COUNT(dbo.dir_order_sections.color_id)
<= 7 THEN 0.05 WHEN COUNT(dbo.dir_order_sections.color_id) >= 8 AND COUNT(dbo.dir_order_sections.color_id)
<= 10 THEN 0.07 WHEN COUNT(dbo.dir_order_sections.color_id) >= 11 THEN 0.07 ELSE 0 END AS role_shift_rate_base, ISNULL(SUM(dbo.colors.role_shift_rate_add),
0) + CASE WHEN COUNT(dbo.dir_order_sections.color_id) = 0 THEN 0.03 WHEN COUNT(dbo.dir_order_sections.color_id) >= 1 AND
COUNT(dbo.dir_order_sections.color_id) <= 2 THEN 0.04 WHEN COUNT(dbo.dir_order_sections.color_id) >= 3 AND COUNT(dbo.dir_order_sections.color_id)
<= 7 THEN 0.05 WHEN COUNT(dbo.dir_order_sections.color_id) >= 8 AND COUNT(dbo.dir_order_sections.color_id)
<= 9 THEN 0.06 WHEN COUNT(dbo.dir_order_sections.color_id) >= 10 THEN 0.07 ELSE 0 END AS role_shift_rate, dbo.orders.order_n
FROM dbo.orders LEFT OUTER JOIN
dbo.dir_order_sections ON dbo.dir_order_sections.order_n = dbo.orders.order_n AND dbo.dir_order_sections.color_id <> 0 AND
dbo.dir_order_sections.color_id IS NOT NULL LEFT OUTER JOIN
dbo.colors ON dbo.dir_order_sections.color_id = dbo.colors.color_id
GROUP BY dbo.orders.order_n
+117
public class Foo {
public Foo() {
System.out.println("constructing foo: " + this);
throw new RuntimeException();
}
public void doEvil() {
System.out.println("Evil!Evil!Evil");
}
}
public class MyFoo extends Foo {
public static Foo x;
@Override
protected void finalize() throws Throwable {
x = this;
}
}
try {
Foo x = new MyFoo();
} catch(Exception e) {
System.out.println(""+e);
}
System.gc();
System.runFinalization();
System.out.println("MyFoo instance: " + MyFoo.x);
MyFoo.x.doEvil();
Вот так бесстрашные хакеры получают доступ к методу экземпляра объекта, конструктор которого выбрасывает исключение, а потом жалуются, что, мол, Java - решето.
А теперь вопрос на засыпку: как сделать то же самое, не используя finalize и gc?
+119
/**
* Imbues the given {@link Font} with support for fallback fonts,
* needed to display CJK characters in fonts that do not support them.
*
* This is an ugly mess that depends on internal Sun APIs. Use sparingly!
*
* @param font the font
* @return the composite font UI resource
*/
public static FontUIResource getCompositeFontUIResource(final Font font) {
try {
Class<?> klass;
try {
// Java 7
klass = Class.forName("sun.font.FontUtilities");
} catch (final ClassNotFoundException e) {
// Java 6
klass = Class.forName("sun.font.FontManager");
}
// Invoke static method that wraps the font
val method = klass.getMethod("getCompositeFontUIResource", Font.class);
return (FontUIResource) method.invoke(null, font);
} catch (final ClassNotFoundException e) {
// Long block of catches that cannot happen on a Sun JRE
throw new AssertionError(e);
} catch (final IllegalAccessException e) {
throw new AssertionError(e);
} catch (final IllegalArgumentException e) {
throw new AssertionError(e);
} catch (final InvocationTargetException e) {
throw new AssertionError(e);
} catch (final NoSuchMethodException e) {
throw new AssertionError(e);
} catch (final SecurityException e) {
throw new AssertionError(e);
}
}
+118
private String compileOwnerName() {
return Joiner.on(' ').skipNulls().
String firstName = ownerFirstName.getValue();
String lastName = ownerLastName.getValue();
final StringBuilder buffer = new StringBuilder();
boolean first = true;
if (firstName != null) {
firstName = firstName.trim();
if (!"".equals(firstName)) {
buffer.append(firstName);
first = false;
}
}
if (lastName != null) {
lastName = lastName.trim();
if (!"".equals(lastName)) {
if (!first) {
buffer.append(" ");
}
buffer.append(lastName);
}
}
return buffer.toString();
}
В итоге заменено однострочником на Perl:
return Joiner.on(' ').skipNulls().join(Strings.emptyToNull(ownerFirstName.getValue().trim()),
ownerLastName.getValue().trim()));
−138
-CREATE TABLE CN_RedoReason (<...>, UNIQUE U_CN_RRSN_NAME1 (name)) ENGINE = innodb;
-CREATE TABLE CN_RejectReason (<...>, UNIQUE U_CN_RRSN_NAME (name)) ENGINE = innodb;
+CREATE TABLE CN_RedoReason (<...>, UNIQUE U_CN_RRSN_NAME (name)) ENGINE = innodb;
+CREATE TABLE CN_RejectReason (<...>, UNIQUE U_CN_RRSN_NAME1 (name)) ENGINE = innodb;
Дифф коммита.
Автогенерация схемы такая автогенерация. Зависит от того, какая таблица первой под руку попадётся.
+121
@SuppressWarnings("deprecation")
// Stupid GWT doesn't have Calendar, so we have to use a deprecated method. :(
final int year = new Date().getYear() + 1900;
+124
BufferedReader file = null;
boolean moreData = true;
//set the parameters
this.textureDir = textureDir;
//open a file
try{
file = new BufferedReader(
new InputStreamReader(
(new FileInputStream(filename))), 50000);
} catch (Exception e) {System.err.println("kan model niet laden"); moreData = false;}
//read shapes until no more data
while(moreData)
{
Shape3D shape = readShape(file);
if(shape != null)
{
tg.addChild(shape);
}
else
{
moreData = false;
}
}
try{
file.close();
file=null;
} catch (IOException e)
{
System.err.println("file " + filename + " could not be closed!");
}
Интересный способ обработки ошибок ввода-вывода.
Естественно, при неудачном открытии файла вываливается по NullPointerException.
+125
// Handle Ctrl-1 ... Ctrl-9 keypresses
if (!event.isSpecialKey() && event.isControlKey()) {
final int code = event.getKeyCode();
if (code >= '1' && code <= '9') {
// Technically, the keyCode is not the ASCII code and should not
// be treated as such. Stupid JavaScript events. However, it
// *just so happens* that for number keys these codes match their
// ASCII codes. Luckily for us.
+111
// WARNING!
// This has to be optional=true so that OpenJPA uses
// LEFT JOIN instead of INNER JOIN. Otherwise, MySQL tries
// using a temporary table (horrible for performance) and dies.
@ManyToOne(optional = true)
@NotNull
private Font font;