Bad Coding Practices

Member

braden_wiegand

Do not check for int type like this in PHP πŸ˜€

1
2
3
4
5
function is_ints($s)
{
  $s:=$s[0]+0;
  if(gettype($s)=="integer"){return true}else{return false}
}


Member

braden_wiegand

How to generate a random password in PHP 😁

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
function randomPassword($length = 8, $seed = '') {
	$password = "";
	$possible = "0123456789";
	$i = 0;
	mt_srand(($seed == '') ? rand() : $seed);
	while ($i < $length) {
		$char = substr($possible, mt_rand(0, strlen($possible) - 1), 1);
		if (!strstr($password, $char)) {
			$password .= $char;
			$i++;
		}
	}
	return $password;
}


Member

zora.stokes

How to get difference between dates in Java 😁

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public static long getDifference(Date date1, Date date2, int unit) {
	if (date1 == null || date2 == null) {
		throw new IllegalArgumentException("Date cannot be null!");
	}

	if (date1.after(date2)) {
		Date swap = date1;
		date1 = date2;
		date2 = swap;
	}
		
	long result = 0;
	
	long diff = date2.getTime() - date1.getTime() + (60 * 60 * 1000L);
		
	switch (unit) {
	case Calendar.YEAR:
		result = diff / (60 * 60 * 24 * 365 * 1000L);
		break;
	case Calendar.MONTH:
		result = diff / (60 * 60 * 24 * 30 * 1000L);
		break;
	case Calendar.DAY_OF_YEAR:
		result = diff / (60 * 60 * 24 * 1000L);
		break;
	default:
		throw new UnsupportedOperationException("Impossible to get time difference in coerced unit.");
	}
		
	return result;
}


Member

zora.stokes

by zora.stokes , in category: Java , a day ago
no answers

How to debug in JAVA 😝

1
2
3
4
5
if(false || DEBUG) 
{
   System.out.print("analysis: "+Integer.toBinaryString(analysis));
   System.out.println(", "+WalkerFactory.getAnalysisString(analysis));
}

Member

zora.stokes

by zora.stokes , in category: SQL , a day ago
no answers

Select statement with many null in SQL

1
2
3
4
SELECT NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
       type_table_of_name_tag_value ()
  FROM DUAL
 WHERE dummy = 'Z'

Member

lou_lindgren

Design log event table including ALL keys in SQL

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
CREATE TABLE `log_event` (
  `id` bigint(20) NOT NULL auto_increment,
  `logtime` datetime default NULL,
  `etype` text,
  `module` text,
  `edata` text,
  `session` text,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `id` (`id`),
  KEY `id_2` (`id`)
)


Member

makayla.beahan

Initialize multiple times instead of using comma in jQuery

1
2
3
4
5
6
7
8
9
$(document).ready(function () {
	$("a").easyTooltip()
});
$(document).ready(function () {
	$("img").easyTooltip()
});
$(document).ready(function () {
	$("span").easyTooltip()
});


Member

mark.berge

When one setTimeout in Javascript is not enough 😁

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
setTimeout(()=>{

  $('.arrowDashed, .OG1ficon, .OG1LeaderLeft, .OG1LeaderRight').css({'transition':'1s', 'opacity':'1'});
  map.opacity.inside([1,0,0], 1);

  setTimeout(()=>{
    map.animate.og1Results(50, 2);
    $('#anotherIdToCall6').css({'transition':'1s', 'opacity':'1'})

    setTimeout(()=>{
      map.opacity.main([1,1,0], 1)
      $('.IGTopContestWrap').css({'transition':'0s', 'opacity':'0'});
      $('.IGBottomContestWrap').css({'transition':'0s', 'opacity':'0'});
    },650)

  }, 1250)

}, 100)


Member

letha

I think too many parentNode in JavaScript 😁

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
for(var i=obj.parentNode.parentNode.childNodes.length;i>=0;i--)
{
  if(obj.parentNode.parentNode.childNodes[i]!=undefined)
  {
    if(obj.parentNode.parentNode.childNodes[i].tagName=="TR")
    {
      if (obj.parentNode.parentNode.childNodes[i].childNodes[0]!=undefined){
        for (var j=obj.parentNode.parentNode.childNodes[i].childNodes.length;j>=0;j--)
        {
          if(obj.parentNode.parentNode.childNodes[i].childNodes[j]!=undefined){
            if(obj.parentNode.parentNode.childNodes[i].childNodes[j].childNodes[0].tagName=="TABLE"){
              obj.parentNode.parentNode.childNodes[i].childNodes[j].childNodes[0].childNodes[0].childNodes[0].childNodes[0].attributes.getNamedItem("background").value = "images/category_n.png";
            }
          }
        }
      }
    }
  }
}


Member

letha

by letha , in category: JavaScript , 7 days ago
no answers

Check hash function in Javascript

1
2
3
4
5
6
7
function checkHash(){
    if(getCookie("needHash") != null){
        if(getCookie("needHash").length>0){
            ensureHash(getCookie("needHash"));
        }
    }
}


Member

carey

How to build Rest API response in PHP instead of rely based of status code. 😁

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
final class Response {

    public static function warning($msg) {

        exit(json_encode('Warning: ' . $msg));

    }

    public static function error($msg) {

        exit(json_encode('Error: ' . $msg));

    }

    public static function json($str, $options = 0) {

        exit(json_encode($str, $options));

    }

}


Member

mark.berge

by mark.berge , in category: JavaScript , 12 days ago
no answers

Preload images in Javascript 😁

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
function newImage(arg) {
    if (document.images) {
        rslt = new Image();
        rslt.src = arg;
        return rslt;
    }
}

function preloadImages() {
          newImage("bigphoto/kran.jpg");
          newImage("bigphoto/z1.jpg");
          newImage("bigphoto/z2.jpg");
          newImage("bigphoto/truba.jpg");
          newImage("bigphoto/put.jpg");
}


Member

mark.berge

by mark.berge , in category: JavaScript , 12 days ago
no answers

Javascript code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
if((value == '284') && (jQuery('#elt_31').val() != 0))
{
       setTimeout(function(){jQuery('#elt_31_fake span[name="287"]').mousedown();},500);
}
if((value == '285') && (jQuery('#elt_31').val() != 0))
{
       setTimeout(function(){jQuery('#elt_31_fake span[name="288"]').mousedown();},500);
}     
if((value == '286') && (jQuery('#elt_31').val() != 0))
{
       setTimeout(function(){jQuery('#elt_31_fake span[name="289"]').mousedown();},500);
}


Member

braden_wiegand

by braden_wiegand , in category: Golang , 13 days ago
no answers

Check if sum of numbers are equal 😁

1
2
3
func sumEqual(u, v, w float64) bool {
	return (u+v == w) && (u == w-v) && (v == w-u)
}


Member

zora.stokes

by zora.stokes , in category: SQL , 13 days ago
no answers

How to round numbers and remove 0 in SQL

1
SELECT REPLACE(RTRIM(REPLACE(REPLACE(RTRIM(REPLACE(LTRIM('  850.0000'),'0',' ')),' ','0'),'.',' ')),' ','.')


Member

zora.stokes

by zora.stokes , in category: SQL , 13 days ago
no answers

User Administrator relation in SQL

1
2
3
4
CREATE TABLE IF NOT EXISTS `ls_user_administrator` (
  `user_id` int(11) unsigned NOT NULL,
  UNIQUE KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


Member

jerald_rippin

by jerald_rippin , in category: SQL , 14 days ago
no answers

Is Number function in SQL 😚

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
create function is_numeric (
	@a varchar(100)
) returns int as begin
	declare @retval int
	set @a = replace(@a, '0', '')
	set @a = replace(@a, '1', '')
	set @a = replace(@a, '2', '')
	set @a = replace(@a, '3', '')
	set @a = replace(@a, '4', '')
	set @a = replace(@a, '5', '')
	set @a = replace(@a, '6', '')
	set @a = replace(@a, '7', '')
	set @a = replace(@a, '8', '')
	set @a = replace(@a, '9', '')
	set @a = replace(@a, '.', '')
	if len(rtrim(ltrim(@a))) > 0 set @retval = 0
	else set @retval = 1
	return @retval
end


Member

vivien_maggio

When one setTimeout is not enough in JavaScript😐

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
setTimeout(function() {

  graph.removeLink('3_3', '2_1')
  keepNodesOnTop()

}, nextval())

setTimeout(function() {

  graph.removeLink('5_2', '9_1')
  keepNodesOnTop()

}, nextval())

setTimeout(function() {

  graph.removeLink('2_2', '4_1')
  keepNodesOnTop()

}, nextval())

Member

vivien_maggio

When you exactly know how animation works in JavaScript 😁

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
let header = document.querySelector('.header');
let elem = document.querySelector('.elem');

setTimeout(() => elem.style.display = 'block', 7500);
setTimeout(() => elem.style.opacity = '0.2', 7700);
setTimeout(() => elem.style.opacity = '0.3', 7800);
setTimeout(() => elem.style.opacity = '0.4', 7900);
setTimeout(() => elem.style.opacity = '0.5', 8000);
setTimeout(() => elem.style.opacity = '0.6', 8100);
setTimeout(() => elem.style.opacity = '0.7', 8200);
setTimeout(() => elem.style.opacity = '0.8', 8300);
setTimeout(() => elem.style.opacity = '0.9', 8400);
setTimeout(() => elem.style.opacity = '1', 8500);


Member

carey

by carey , in category: Python , 14 days ago
no answers

Calculate factorial in Python πŸ™ƒ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def factorial(x):
    """ Work out x! (with a little help from the google calculator...) """

    import re
    import urllib
    import time

    time.sleep(2)

    class AppURLopener(urllib.FancyURLopener):
        def __init__(self, *args):
            # *Cough* *Cough*
            self.version = 'Mozilla 1.3'
            urllib.FancyURLopener.__init__(self, *args)

    opener = AppURLopener()
    page = opener.open('http://www.google.com/search?q=%d!' % x).read()

    result = re.findall('<b>%d ! = (.*?)</b>' % x, page)

    if result:
        return int(result[0].replace('<font size=-2> </font>', ''))
    else:
        raise Exception, "Google not willing today!:\n\n %s" % page