Coder Social home page Coder Social logo

trekhleb / state-of-the-art-shitcode Goto Github PK

View Code? Open in Web Editor NEW
5.3K 49.0 306.0 64 KB

💩State-of-the-art shitcode principles your project should follow to call it a proper shitcode

License: MIT License

best-practices best-practice styleguide style-guides principles coding code-quality javascript programming

state-of-the-art-shitcode's Introduction

State-of-the-Art Shitcode Principles

State-of-the-art Shitcode

This a list of state-of-the-art shitcode principles your project should follow to call it a proper shitcode.

Read this in other languages: 简体中文, 한국어

Get Your Badge

If your repository follows the state-of-the-art shitcode principles you may use the following "state-of-the-art shitcode" badge:

State-of-the-art Shitcode

Markdown source-code for the badge:

[![State-of-the-art Shitcode](https://img.shields.io/static/v1?label=State-of-the-art&message=Shitcode&color=7B5804)](https://github.com/trekhleb/state-of-the-art-shitcode)

The Principles

💩 Name variables in a way as if your code was already obfuscated

Fewer keystrokes, more time for you.

Good 👍🏻

let a = 42;

Bad 👎🏻

let age = 42;

💩 Mix variable/functions naming style

Celebrate the difference.

Good 👍🏻

let wWidth = 640;
let w_height = 480;

Bad 👎🏻

let windowWidth = 640;
let windowHeight = 480;

💩 Never write comments

No one is going to read your code anyway.

Good 👍🏻

const cdr = 700;

Bad 👎🏻

More often comments should contain some 'why' and not some 'what'. If the 'what' is not clear in the code, the code is probably too messy.

// The number of 700ms has been calculated empirically based on UX A/B test results.
// @see: <link to experiment or to related JIRA task or to something that explains number 700 in details>
const callbackDebounceRate = 700;

💩 Always write comments in your native language

If you violated the "No comments" principle then at least try to write comments in a language that is different from the language you use to write the code. If your native language is English you may violate this principle.

Good 👍🏻

// Закриваємо модальне віконечко при виникненні помилки.
toggleModal(false);

Bad 👎🏻

// Hide modal window on error.
toggleModal(false);

💩 Try to mix formatting style as much as possible

Celebrate the difference.

Good 👍🏻

let i = ['tomato', 'onion', 'mushrooms'];
let d = [ "ketchup", "mayonnaise" ];

Bad 👎🏻

let ingredients = ['tomato', 'onion', 'mushrooms'];
let dressings = ['ketchup', 'mayonnaise'];

💩 Put as much code as possible into one line

Good 👍🏻

document.location.search.replace(/(^\?)/,'').split('&').reduce(function(o,n){n=n.split('=');o[n[0]]=n[1];return o},{})

Bad 👎🏻

document.location.search
  .replace(/(^\?)/, '')
  .split('&')
  .reduce((searchParams, keyValuePair) => {
    keyValuePair = keyValuePair.split('=');
    searchParams[keyValuePair[0]] = keyValuePair[1];
    return searchParams;
  },
  {}
)

💩 Fail silently

Whenever you catch an error it is not necessary for anyone to know about it. No logs, no error modals, chill.

Good 👍🏻

try {
  // Something unpredictable.
} catch (error) {
  // tss... 🤫
}

Bad 👎🏻

try {
  // Something unpredictable.
} catch (error) {
  setErrorMessage(error.message);
  // and/or
  logError(error);
}

💩 Use global variables extensively

Globalization principle.

Good 👍🏻

let x = 5;

function square() {
  x = x ** 2;
}

square(); // Now x is 25.

Bad 👎🏻

let x = 5;

function square(num) {
  return num ** 2;
}

x = square(x); // Now x is 25.

💩 Create variables that you're not going to use.

Just in case.

Good 👍🏻

function sum(a, b, c) {
  const timeout = 1300;
  const result = a + b;
  return a + b;
}

Bad 👎🏻

function sum(a, b) {
  return a + b;
}

💩 Don't specify types and/or don't do type checks if language allows you to do so.

Good 👍🏻

function sum(a, b) {
  return a + b;
}

// Having untyped fun here.
const guessWhat = sum([], {}); // -> "[object Object]"
const guessWhatAgain = sum({}, []); // -> 0

Bad 👎🏻

function sum(a: number, b: number): ?number {
  // Covering the case when we don't do transpilation and/or Flow type checks in JS.
  if (typeof a !== 'number' && typeof b !== 'number') {
    return undefined;
  }
  return a + b;
}

// This one should fail during the transpilation/compilation.
const guessWhat = sum([], {}); // -> undefined

💩 You need to have an unreachable piece of code

This is your "Plan B".

Good 👍🏻

function square(num) {
  if (typeof num === 'undefined') {
    return undefined;
  }
  else {
    return num ** 2;
  }
  return null; // This is my "Plan B".
}

Bad 👎🏻

function square(num) {
  if (typeof num === 'undefined') {
    return undefined;
  }
  return num ** 2;
}

💩 Triangle principle

Be like a bird - nest, nest, nest.

Good 👍🏻

function someFunction() {
  if (condition1) {
    if (condition2) {
      asyncFunction(params, (result) => {
        if (result) {
          for (;;) {
            if (condition3) {
            }
          }
        }
      })
    }
  }
}

Bad 👎🏻

async function someFunction() {
  if (!condition1 || !condition2) {
    return;
  }
  
  const result = await asyncFunction(params);
  if (!result) {
    return;
  }
  
  for (;;) {
    if (condition3) {
    }
  }
}

💩 Mess with indentations

Avoid indentations since they make complex code take up more space in the editor. If you're not feeling like avoiding them then just mess with them.

Good 👍🏻

const fruits = ['apple',
  'orange', 'grape', 'pineapple'];
  const toppings = ['syrup', 'cream', 
                    'jam', 
                    'chocolate'];
const desserts = [];
fruits.forEach(fruit => {
toppings.forEach(topping => {
    desserts.push([
fruit,topping]);
    });})

Bad 👎🏻

const fruits = ['apple', 'orange', 'grape', 'pineapple'];
const toppings = ['syrup', 'cream', 'jam', 'chocolate'];
const desserts = [];

fruits.forEach(fruit => {
  toppings.forEach(topping => {
    desserts.push([fruit, topping]); 
  });
})

💩 Do not lock your dependencies

Update your dependencies on each new installation in uncontrolled way. Why stick to the past, let's use the cutting edge libraries versions.

Good 👍🏻

$ ls -la

package.json

Bad 👎🏻

$ ls -la

package.json
package-lock.json

💩 Always name your boolean value a flag

Leave the space for your colleagues to think what the boolean value means.

Good 👍🏻

let flag = true;

Bad 👎🏻

let isDone = false;
let isEmpty = false;

💩 Long-read functions are better than short ones.

Don't divide a program logic into readable pieces. What if your IDE's search breaks and you will not be able to find the necessary file or function?

  • 10000 lines of code in one file is OK.
  • 1000 lines of a function body is OK.
  • Dealing with many services (3rd party and internal, also, there are some helpers, database hand-written ORM and jQuery slider) in one service.js? It's OK.

💩 Avoid covering your code with tests

This is a duplicate and unnecessary amount of work.

💩 As hard as you can try to avoid code linters

Write code as you want, especially if there is more than one developer in a team. This is a "freedom" principle.

💩 Start your project without a README file.

And keep it that way for the time being.

💩 You need to have unnecessary code

Don't delete the code your app doesn't use. At most, comment it.

state-of-the-art-shitcode's People

Contributors

alexnodex avatar aoping avatar danielsychoo avatar herschel666 avatar trekhleb avatar tychenjiajun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

state-of-the-art-shitcode's Issues

Is it necessary to write comments in native language?

I know it's a ironical project, but I have question whether to write comments in native language.
Using Chinese will has trouble shifting input method, and it would lead to many problems between win and linux.
But using English will avoid these questions as well as practice Computer English, and it wouldn't cause any problems reading it.

Bad indenting

Don't indent, makes complex code take up more space in the editor.

感谢👍

加个星,虽然我现在是个学生,但是以后可能会用到。

Should i use variable names in native language?

Python and JavaScript allows it, so i think using English and changing layout hundreds of times is nonsense
Also some weird characters need struggling, so replace them via #define in c and do neater functions.

Write comments using a rare language

Always write comments in languages that are rarely used among team members. So that members can identify who wrote the code easily.

For example:

  • Always use Elvish if you know how to
  • If you're the only Chinese guy, then use Chinese
  • If you don't know other languages, use family jargons if possible

My game is very laggy

I followed all the principles of this repository, and yet my game is very laggy and uses a lot of CPU and RAM. I don't think I can optimize my code further, but maybe one of you guys can help?

Name variables in your native language

Nowadays many things support complex variable names. So why not name them in your native language? That saves your time thinking of a name, and time is important.

Good 👍

记录=提取记录(结果)

Bad 👎

processRecord = getRecord(Result)

No combined shitcode

Am I just blind, or is there no combined shitcode all in one file? I need this as an example.

Using Emoticons as Identifiers ಠ_ಠ

JavaScript allows for UTF8 variable names which allows use of some emoticons and some people even use emoji as identifiers in languages like C++ #39

Good : -

var ಠ_ಠ = 2022

Bad : -

var year = 2022

Shitcode : Enterprise Edition

Just a thought, what will happen when shitcode meets EE.

There's FizzBuzzEnterpriseEdition showing us how over-engineering and cargo cult would devastate your codebase in a hurmorous way.

It comes to me that shitcode is not only about how you write the code, but also how you choose the abstraction and implement it.

More "enterprise level shitcode" examples?

Important rule missing

There should be an item regarding secrets in code:

Good practice :

  • put your API keys and SSH keys in code. You will never forget them that way 😄

Bad practice:

  • encrypt sensitive values or keep them in a secret vault

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.