Skip to content

Instantly share code, notes, and snippets.

View KiT106's full-sized avatar

Dzung KiT KiT106

View GitHub Profile
@KiT106
KiT106 / Enum.java
Last active May 10, 2016 09:00
Citigo smelling code
// Bad
public static final int MARITAL_SINGLE = 1;
public static final int MARITAL_MARRIED = 2;
public static final int MARITAL_DISVORCED = 3;
public static final int MARITAL_WIDOW = 4;
public static final String MARITAL_SINGLE_NAME = "Single";
public static final String MARITAL_MARRIED_NAME = "Married";
public static final String MARITAL_DISVORCED_NAME = "Disvorced";
public static final String MARITAL_WIDOW_NAME = "Widow[er]";
@KiT106
KiT106 / TypedQuery.java
Created May 10, 2016 09:12
Citigo Code smell
// Bad
public List<Employee> getEmployeesByManager(Employee manager){
String sql = "SELECT e FROM Employee e WHERE e.manager = :manager";
Query query = entityManager.createQuery(sql);
query.setParameter(...);
return query.getResultList(); // Warning "unchecked"
}
// Good
TypedQuery<Employee> query = entityManager.createQuery(sql, Employee.class);
@KiT106
KiT106 / GetOne.java
Created May 10, 2016 09:30
Citigo Code smell
// Bad
public Employee getSingleEmployee(params...){
String sql = ...;
TypedQuery<Employee> query = entityManager.createQuery(sql, Employee.class);
query.setParameter(...);
List<Employee> list = query.getResultList(); // can load more than one result
if (list == null || list.isEmpty()) {
return null;
@KiT106
KiT106 / CheckTruthy.js
Created May 12, 2016 03:05
Citigo Code smell
// Bad
if (foo !== undefined && foo != null && foo != '') {
// do something...
}
// Good
if (foo) {
// do something...
}
// will evaluate to true if value is not:
@KiT106
KiT106 / IfTrue.js
Created May 12, 2016 03:25
Citigo Code smell
// Bad
if (data.selected == true){
// do staff
}
if (data.selected != true){
// do staff
}
// Good
if ( data ) {
/**
* @file A sample file demo about JSDoc
* @author dungdm93
* @since 2016-05-26
*/
/**
* Person entity class
* @class
*/
ALTER TABLE revinfo
AUTO_INCREMENT = 3;
INSERT INTO revinfo
(REV, REVTSTMP)
VALUES (2, 146613965300);
UPDATE revinfo
SET REV = REV + 1
WHERE REVTSTMP > 1466139651507
@KiT106
KiT106 / index.html
Created October 21, 2016 10:43
AngularJS : 2 controllers inside a scope
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="utf-8">
<title>Egghead Videos</title>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/foundation/6.2.4/foundation.css">
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script type="text/javascript" src="main.js"></script>
</head>
<body ng-app="app" scope="{{$id}}">
@KiT106
KiT106 / main.js
Created October 22, 2016 10:31
AngularJS $routeProvider.resolve
var app = angular.module("app", ["ngRoute"]);
app.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: "app.html",
controller: "ViewCtrl",
resolve: {
first: function () {
return "Hello world";
@KiT106
KiT106 / customValidationMessage.js
Created February 4, 2017 19:15
HTML form custom validation message
$("input").on("invalid", function(e) {
if (this.validity.valueMissing){
this.setCustomValidity("valueMissing");
}
if (this.validity.tooShort){
this.setCustomValidity("tooShort");
}
// .... more custom message here.
});
$("input").on("input", function(e) {