Skip to Content
0 likes
Python dataclasses: A Class Without Writing __init__ by Hand Level: Intermediate

If a class mostly holds data, @dataclass writes __init__, a readable printout and equality for you. Use field(default_factory=list) for lists, __post_init__ for checks and frozen=True for values that must not change.

0 likes
JavaScript Classes With Getters and Setters Level: Intermediate

Getters compute a value when you read it, so totals never go stale. Setters check a value when you assign it, so a negative price throws instead of saving. With private fields, nobody can go around them.

0 likes
Custom Error Classes You Can Actually Catch Level: Intermediate

Stop checking error messages with includes(). Extend Error, set name, add the fields the handler needs, catch with instanceof, and wrap low-level errors with cause so the real reason isn't lost.

0 likes
private Fields vs #private: Which One Fits Level: Intermediate

Run the same class with the private keyword and with a hash field, and compare the output, the errors and what outside code can still reach. After this, you can pick one and say why.