Skip to content

Instantly share code, notes, and snippets.

@mr-nano
mr-nano / SomeBadComponent.js
Created September 3, 2021 17:15
Using state derived from props in constructor is typically a code smell in react
import React from 'react';
class BadComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
message : this.props.message
};
}
@mr-nano
mr-nano / index.html
Last active July 18, 2021 16:15
Example of clear-fix with floats
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
section {
float:left;
@mr-nano
mr-nano / java
Created January 12, 2021 04:10
Is this recommended use case for static
// Need to count how many instances have been created of SomeClass
public class SomeClass {
public static int numberOfInstances = 0;
public SomeClass() {
numberOfInstances++;
}
}
@mr-nano
mr-nano / gist:7fec48f705c23de3fe0feb985e2effd5
Created January 12, 2021 03:26
This code is mostly an example of what...
public class LargeConsumer {
public static void main(String[] args) {
new Thread() {
@Override
public void run() {
System.out.println("It Works");
}
}.start();
}
@mr-nano
mr-nano / java
Created January 11, 2021 19:41
What are the code smells here
public class TotalCalculator {
static int price;
static boolean isRegisteredUser;
static int quantity;
public static double getTotal() {
double totalPrice = price * quantity;
if(isRegisteredUser) {
return 0.85 * price * quantity;
@mr-nano
mr-nano / java
Created January 11, 2021 19:26
What's missing here
package com.thoughtworks.rectangle;
public class Rectangle {
private int length;
private int breadth;
public Rectangle(int length, int breadth) {
this.length = length;
this.breadth = breadth;
@mr-nano
mr-nano / java
Created January 11, 2021 19:18
What's the problem with this contract
package com.thoughtworks.rectangle;
public class Rectangle {
public int area(int length, int breadth) {
return length * breadth;
}
}
@mr-nano
mr-nano / gist:20297417243877ff460a1fc21f2c9508
Created January 11, 2021 18:42
What's wrong with this test?
class RectangleTest {
@Test
void areaOfAUnitRectangleShouldBeOne() {
Rectangle rectangle = new Rectangle(1, 1);
int actualArea = rectangle.area();
int expectedArea = 10;
assertEquals(expectedArea, actualArea);
}
@mr-nano
mr-nano / java
Created January 11, 2021 18:25
What's wrong with this test
class RectangleTest {
@Test
void areaOfAUnitRectangleShouldBeOne() {
Rectangle rectangle = new Rectangle(1, 1);
int actualArea = rectangle.area();
int expectedArea = 1;
assertEquals(expectedArea, expectedArea);
}
@mr-nano
mr-nano / java
Created January 11, 2021 18:15
OOP Rectangle
public class Rectangle {
private final int length;
private final int breadth;
public Rectangle(int length, int breadth) {
this.length = length;
this.breadth = breadth;
}