Post AUpm1zXEnh293Q1dnE by lesley@mastodon.gamedev.place
(DIR) More posts by lesley@mastodon.gamedev.place
(DIR) Post #AUpm1yZgN7OY4iA4xs by nicoespeon@toot.legacycode.rocks
2023-04-19T18:08:46Z
0 likes, 0 repeats
It's common for legacy code to have deeply nested statementsThey make code harder to read/think about (cognitive overload) 😵💫Did you know "Guard Clauses" can help you simplify these? Now you know 💂Here's a JavaScript example 👇
(DIR) Post #AUpm1zXEnh293Q1dnE by lesley@mastodon.gamedev.place
2023-04-20T03:08:39Z
0 likes, 0 repeats
@nicoespeon Unfortunately, many people still preach "you should always have one return in a function."If you ask them, they usually can only give nebulous reasons like "readability" or "reduce complexity." The original proponents of "one return" did have some point, but the original rationales are most lost in history and no longer relevant in modern settingshttps://softwareengineering.stackexchange.com/questions/118703/where-did-the-notion-of-one-return-only-come-from
(DIR) Post #AUpm20RxOoP5tKYwCW by bortzmeyer@mastodon.gougere.fr
2023-04-20T05:54:05Z
0 likes, 0 repeats
@lesley @nicoespeon Curently programming in Elixir where "return" does not exist so my functions do not have even one return :-)
(DIR) Post #AUqPqgGm5LqOHgUDY0 by nicoespeon@toot.legacycode.rocks
2023-04-20T13:20:11Z
0 likes, 0 repeats
@bortzmeyer @lesley indeed. But I think the philosophy with Elixir is to rely on pattern matching more, so you have shorter functions for each of the casesIn this example, you would pattern-match on the `employee.isSeparated` and `employee.isRetired` so you don't have the if/else in a single function(also do-able in TS with ts-pattern, but less idiomatic)