Bad Coding Practices

This password validation function in Javascript looks really awful to be honest 😃 Thoughts ?


 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
function checkPassword() {
    const val = document.querySelector('.session-input > #user_password').value
    const confirm = document.querySelector('.session-input > #user_password_confirmation').value
    const email = document.querySelector('.session-input > #user_email').value.split('@')[0]

    if (val && email) {
        if (val.length < 16) {
            document.querySelector('.pw-length').classList.remove('success')
            document.querySelector('.pw-length').classList.add('error')
        } else {
            document.querySelector('.pw-length').classList.remove('error')
            document.querySelector('.pw-length').classList.add('success')
        }

        if (val.search(/[a-z]/) < 0 || val.search(/[A-Z]/) < 0) {
            document.querySelector('.lower-upper').classList.remove('success')
            document.querySelector('.lower-upper').classList.add('error')
        } else {
            document.querySelector('.lower-upper').classList.remove('error')
            document.querySelector('.lower-upper').classList.add('success')
        }

        if (val.search(/[0-9]/) < 0 || val.search(/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/) < 0) {
            document.querySelector('.num-char').classList.remove('success')
            document.querySelector('.num-char').classList.add('error')
        } else {
            document.querySelector('.num-char').classList.remove('error')
            document.querySelector('.num-char').classList.add('success')
        }

        if (val != confirm) {
            document.querySelector('.pw-match').classList.remove('success')
            document.querySelector('.pw-match').classList.add('error')
        } else {
            document.querySelector('.pw-match').classList.remove('error')
            document.querySelector('.pw-match').classList.add('success')
        }

        if (checkIdenticalConsecutive()) {
            document.querySelector('.consecutive').classList.remove('success')
            document.querySelector('.consecutive').classList.add('error')
        } else {
            document.querySelector('.consecutive').classList.remove('error')
            document.querySelector('.consecutive').classList.add('success')
        }

        if (val.search(/hydra/) >= 0 || val.toLowerCase().search(email.toLowerCase()) >= 0) {
            document.querySelector('.name-email-hydra').classList.remove('success')
            document.querySelector('.name-email-hydra').classList.add('error')
        } else {
            document.querySelector('.name-email-hydra').classList.remove('error')
            document.querySelector('.name-email-hydra').classList.add('success')
        }
    }
}
S

Member

The senior front enders in the company I recently joined are trying to convince me that it's ok to write code like this


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
divisionHelperTriangle(length, score) {
    if (score === 2) {
      return length / 28
    }
    if (score === 3) {
      return length / 26
    }
    if (score === 4) {
      return length / 26.5
    }
    if (score === 5) {
      return length / 27.5
    }
    if (score === 6) {
      return length / 26
    }
    if (score >= 7) {
      return length / 25
    }
    return length / 30
  }


Great approach to remove all numbers from a string in PHP via regular expression. PRO level regular expression in PHP 😀 Instead of simple [0-9] or \d look:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$words = preg_replace('/0/', '', $words ); // remove numbers
$words = preg_replace('/1/', '', $words ); // remove numbers
$words = preg_replace('/2/', '', $words ); // remove numbers
$words = preg_replace('/3/', '', $words ); // remove numbers
$words = preg_replace('/4/', '', $words ); // remove numbers
$words = preg_replace('/5/', '', $words ); // remove numbers
$words = preg_replace('/6/', '', $words ); // remove numbers
$words = preg_replace('/7/', '', $words ); // remove numbers
$words = preg_replace('/8/', '', $words ); // remove numbers
$words = preg_replace('/9/', '', $words ); // remove numbers


Check if string is palindrome on the Python language

1
2
3
4
5
6
7
8
def ispalindrome(string):
	decide=1
	i=0
	while i<=int(len(string)/2) and decide==1:
		if string[i]!=string[-(i+1)]:
			decide=0
		i+=1
	return decide


by keanu.kilback , in category: Python , a year ago
no answers

How to reverse string in Python 😃

1
2
3
4
def reverse(s, rs=''):
    for i in range(1, len(s)):
        rs = rs + s[len(s)-i]
    return rs+s[0]


Nice code in Model of Django Python based framework

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# In Django model
def processed_description(self):
    text = self.description. \
           replace('<table', '<div class="table">'
                             '<div class="bgtop"></div>'
                             '<div class="overflow"><table'). \
           replace('</table>', '</table></div>'
                               '<div class="bgbottom"></div>'
                               '</div>'). \
           replace('<th', '<th width="50%" align="left"')
    return text


Member

by zoie , in category: Python , a year ago
no answers

How you do not need programming 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
25
26
27
28
29
30
31
if cells[x-1,y-1] == 1: pos+=1
else:
  if cells[x-1,y-1] == -1: neg+=1
if cells[x-1,y] == 1: pos+=1
else:
  if cells[x-1,y] == -1: neg+=1
if cells[x-1,y+1] == 1: pos+=1
else:
  if cells[x-1,y+1] == -1: neg+=1


if cells[x,y-1] == 1: pos+=1
else:
  if cells[x,y-1] == -1: neg+=1
if cells[x,y] == 1: pos+=1
else:
  if cells[x,y] == -1: neg+=1
if cells[x,y+1] == 1: pos+=1
else:
  if cells[x,y+1] == -1: neg+=1


if cells[x+1,y-1] == 1: pos+=1
else:
  if cells[x+1,y-1] == -1: neg+=1
if cells[x+1,y] == 1: pos+=1
else:
  if cells[x+1,y] == -1: neg+=1
if cells[x+1,y+1] == 1: pos+=1
else:
  if cells[x+1,y+1] == -1: neg+=1


Member

by leta , in category: Python , a year ago
no answers

Summer intern Python code this summer 🤗

1
2
3
4
5
6
7
8
try:
 # do some stuff
except:
  e0 = sys.exc_info()[0]
  e1 = sys.exc_info()[1]
  e2 = sys.exc_info()[2]
  print '*** failed due to: %s %s %s' % (e0, e1, e2)
  sys.exit(3)


Goroutines and loop together in Golang 🥰

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
for i := 0; i < fanout; i++ {
	go func() {
		for {
			c.Dec(19)
			time.Sleep(300e6)
		}
	}()
	go func() {
		for {
			c.Inc(47)
			time.Sleep(400e6)
		}
	}()
}


How to write detailed TODO comments in Python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# TODO: fix this hell
def pretty_print(w, p):
    w = truncate(w)
    p = truncate(p)
    my_matrix = zip(p, w)
    print  "\n".join(["\t\t".join(["\t".join(map(str, r)) for r in t]) for t in my_matrix])        


# TODO: and this
def truncate(m):
    for i in range(len(m)):
        for j in range(len(m[0])):
            if(len(str(m[i][j])) > 5):
                m[i][j] = "%.3f" % m[i][j]
    return m


Some interesting source code from Android 1.5 🤨

 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
32
33
34
35
36
37
38
39
40
41
42
43
if (f.mColor != null) {
  int c = -1;


  if (f.mColor.equalsIgnoreCase("aqua")) {
      c = 0x00FFFF;
  } else if (f.mColor.equalsIgnoreCase("black")) {
      c = 0x000000;
  } else if (f.mColor.equalsIgnoreCase("blue")) {
      c = 0x0000FF;
  } else if (f.mColor.equalsIgnoreCase("fuchsia")) {
      c = 0xFF00FF;
  } else if (f.mColor.equalsIgnoreCase("green")) {
      c = 0x008000;
  } else if (f.mColor.equalsIgnoreCase("grey")) {
      c = 0x808080;
  } else if (f.mColor.equalsIgnoreCase("lime")) {
      c = 0x00FF00;
  } else if (f.mColor.equalsIgnoreCase("maroon")) {
      c = 0x800000;
  } else if (f.mColor.equalsIgnoreCase("navy")) {
      c = 0x000080;
  } else if (f.mColor.equalsIgnoreCase("olive")) {
      c = 0x808000;
  } else if (f.mColor.equalsIgnoreCase("purple")) {
      c = 0x800080;
  } else if (f.mColor.equalsIgnoreCase("red")) {
      c = 0xFF0000;
  } else if (f.mColor.equalsIgnoreCase("silver")) {
      c = 0xC0C0C0;
  } else if (f.mColor.equalsIgnoreCase("teal")) {
      c = 0x008080;
  } else if (f.mColor.equalsIgnoreCase("white")) {
      c = 0xFFFFFF;
  } else if (f.mColor.equalsIgnoreCase("yellow")) {
      c = 0xFFFF00;
  } else {
      try {
          c = XmlUtils.convertValueToInt(f.mColor, -1);
      } catch (NumberFormatException nfe) {
          // Can't understand the color, so just drop it.
      }
  }


Using Javascript setTimeout directly from PHP 😃

1
2
3
4
$file=$GLOBALS["LOGFILE"];
if(!is_file($file)){echo "<script>setTimeout(\"Procedure2$t()\",1000);</script>";return;}
$data=@file_get_contents($file);
if(strlen($data)<10){echo "<script>setTimeout(\"Procedure2$t()\",1000);</script>";return;}


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

Thread join implementation in Java

1
2
3
4
5
6
7
try {
     while (sender.isAlive()) {
         this.sleep(100);//static field usage(!)
     }
} catch (java.lang.InterruptedException e) {
     Log.log(Log.ERROR, this, e);
}


Member

by letha , in category: PHP , a year ago
no answers

Render Javascript directly from PHP code. 😜

 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
$html="
  var TIMER$t=0;
  function StartLoadjs$t(){
  	
  		YahooWin3('998','$page?popup=yes&t=$t$OnlyRoutes','$title');
  	
  	}
  	
  function GetLogs$t(){
  	Loadjs('$page?logs=yes&t=$t&setTimeout={$_GET["setTimeout"]}');
  	if(document.getElementById('IMAGE_STATUS_INFO-$t')){
  		Loadjs('admin.tabs.php?refresh-status-js=yes&nocache=yes');
  	}
  }

  function Procedure2$t(){
  	LoadAjax('procedure2-$t','$page?procedure2=yes&t=$t$OnlyRoutes');
  }

  function Procedure3Error$t(){
  	document.getElementById('procedure3-text$t').value=document.getElementById('procedure3-text$t').value+'\\n'+'Please wait...';
  	setTimeout(\"Procedure3$t()\",1000);
  }

  function Procedure3$t(){
  	document.getElementById('title-$t').innerHTML='$please_wait_building_network';
  	LoadAjax('procedure3-$t','$page?procedure3=yes&t=$t$OnlyRoutes','Procedure3Error$t()');
  }

  function finish$t(){
  	if(document.getElementById('table-$t')){
  		$('#table-$t').flexReload();
  	}
  	
  	if(document.getElementById('tabs_listnics2')){
  		RefreshTab('tabs_listnics2');
  	}

  }

  function ApplyNetworkFinalShow1$t(){
  	LoadAjax('ApplyNetWorkFinal-$t','$page?ApplyNetWorkFinal-tests=yes&t=$t$OnlyRoutes');
  	ApplyNetworkFinalShow$t();
  }

  function ApplyNetworkFinalShow$t(){
  	if(TIMER$t==0){
  		document.getElementById('title-$t').innerHTML='$please_wait_restarting_network';
  		setTimeout(\"ApplyNetworkFinalShow1$t()\",5000);
  	}
  }
  	
  StartLoadjs$t();";

echo $html;


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

How to return boolean result in Java 🤗

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
/**
 * Delete. * 
 * @param entity the entity 
 * @return true, if successful 
 */ 
public boolean delete(T entity) {
 	boolean deleted=false; 
	getSession().delete(entity); 
	flush(); 
	//when no hibernate runtime exception has arised...set to true.
	deleted = true;
	return deleted;
}