Maximum Bug Checking - Using Zig error checking to find bugs in my chess code!

  Рет қаралды 726

Coding with Tom

Coding with Tom

Күн бұрын

Пікірлер: 5
@Nellak2011
@Nellak2011 11 күн бұрын
A few refactoring tricks that are imperative language agnostic will help you. 1. Early return. --- old if (cond) { logic } else { logic return } --- new if (!cond) return .. logic 2. And instead of nested if. --- old if (cond) { if (cond2) { logic } } --- new if (cond && cond2) { logic } 3. Extraction of pure code. --- old const main = () => { pure calculation side effect pure calculation side effect } --- new // you can unit test calc1 and 2 instead of performing an integration test only on main const calc1 = (...params) => { logic } const calc2 = (...params) => { logic } const main = () => { r1 = calc1(...) side effect(r1) r2 = calc2(...) side effect(r2) } 4. Converting functions that call outside of themselves to ones that are self contained. (Only do this if possible and if it makes sense to, in rare cases in mutable languages you actually must write it in the poor style of external mutation, if you must do that, isolate it as much as you can to mitigate uncontrolled side-effects) --- old let i = 0 const f = i => { i++ } f(i) // i is mutated from f, making f non-deterministic and hard to debug --- new const f = i => i + 1 f(i) // i + 1 is returned but i itself is never mutated out from under you 5. Eliminating global variables and instead using block scoped variables. 6. Reducing mutable variables through higher order functions if applicable, such as using map, filter, and reduce. This simply reduces bug surface area, however, not all imperative languages have performant support for this. ==== General refactoring approach 1. Apply above transformations and more with the goal of concise code that is modular and easy to reason about. Clean code is easier to manipulate that ugly and overly mutable code. 2. Create Integration tests for functionality and unit tests for any pure functions you were able to extract. I recommend using property based testing approaches so you don't have to enumerate a billion example tests. 3. With the tests in place, you start slowly refactoring out with the goal of breaking up everything into self-contained functions. Functions should have 1 responsibility. Meaning that a function should do as few things as possible. The reason for this is so that you can make your system Composable rather than fragile. 4. With your composable building blocks tested, you can then build up more easily. ==== Overall, you should see a massive reduction in the lines of code and your code will be far better if you follow my advice. === Update I have seen very often people using switch statements which is super annoying and bloated. It is equivalent but more terse to use an object instead (atleast in JS, make sure your language you do this in supports hash maps). --- old switch (thing) { case ..: .. case .. : .. .... } --- new (JS syntax) const cases = { 'case1' : () => logic, 'case2' : value or whatever, ... } cases[thing] Alternatively: { 'case1' : () => logic, 'case2' : value or whatever, ... }[thing] // Note: usually you want the cases in your object literal to have a value and not be a function so that you can assign the result to a variable then do further logic // like so: const value = { 'case1' : value, 'case2' : value, ... }[thing] logic on that value ...
@requestfx5585
@requestfx5585 13 күн бұрын
Why not use zig build system, "zig init" command helps
@CodingWithTom-tn7nl
@CodingWithTom-tn7nl 12 күн бұрын
It's probably better for a large project to use the build system, especially if you have multiple platform targets. For demonstrating things and just getting the code to run quickly, it's more cumbersome. To find the exe, you need to look through folders and normally I just want it to run in the console window. It's mostly preference and I don't feel like I need it. There is barely any different to typing: zig run main.zig vs zig build run
@requestfx5585
@requestfx5585 12 күн бұрын
@CodingWithTom-tn7nl valid. I still prefer the build system, zig init and zig build run and it already works. The second you need some module, dependency, c code stuff the build system becomes very useful
@dawid0115
@dawid0115 6 күн бұрын
also zig build system provides caching where zig run does not
10 Signs Your Software Project Is Heading For FAILURE
17:59
Continuous Delivery
Рет қаралды 10 М.
I Coded a Chess Engine in 7 Languages to test Performance!
25:07
Coding with Tom
Рет қаралды 13 М.
99.9% IMPOSSIBLE
00:24
STORROR
Рет қаралды 31 МЛН
Sigma Kid Mistake #funny #sigma
00:17
CRAZY GREAPA
Рет қаралды 30 МЛН
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 158 МЛН
I Beat Minecraft From One Grass Block
35:27
Beppo
Рет қаралды 7 МЛН
Why You Shouldn't Nest Your Code
8:30
CodeAesthetic
Рет қаралды 2,8 МЛН
I made Tetris in C, this is what I learned
15:15
Austin Larsen
Рет қаралды 30 М.
Inside the V3 Nazi Super Gun
19:52
Blue Paw Print
Рет қаралды 2,8 МЛН
Shredding Paper with Lego Gears (ver 2)
6:12
Brick Experiment Channel
Рет қаралды 10 МЛН
Why Ghostty is written in Zig (not Rust or Go)
3:42
Changelog
Рет қаралды 50 М.
The Genius Way Computers Multiply Big Numbers
22:04
PurpleMind
Рет қаралды 316 М.
zig will change programming forever
9:34
Low Level
Рет қаралды 408 М.
My 10 “Clean” Code Principles (Start These Now)
15:12
Conner Ardman
Рет қаралды 322 М.
99.9% IMPOSSIBLE
00:24
STORROR
Рет қаралды 31 МЛН