Writing Great Code

Torvalds’ quote about good programmer

It might help to consider what Torvalds said right before that:

git actually has a simple design, with stable and reasonably well-documented data structures. In fact, I’m a huge proponent of designing your code around the data, rather than the other way around, and I think it’s one of the reasons git has been fairly successful […] I will, in fact, claim that the difference between a bad programmer and a good one is whether he considers his code or his data structures more important.

What he is saying is that good data structures make the code very easy to design and maintain, whereas the best code can’t make up for poor data structures.

If you’re wondering about the git example, a lot of version control systems change their data format relatively regularly in order to support new features. When you upgrade to get the new feature, you often have to run some sort of tool to convert the database as well.

For example, when DVCS first became popular, a lot of people couldn’t figure out what about the distributed model made merges so much cleaner than centralized version control. The answer is absolutely nothing, except distributed data structures had to be much better in order to have a hope of working at all. I believe centralized merge algorithms have since caught up, but it took quite a long time because their old data structures limited the kinds of algorithms they could use, and the new data structures broke a lot of existing code.

In contrast, despite an explosion of features in git, its underlying data structures have barely changed at all. Worry about the data structures first, and your code will naturally be cleaner.


Torvalds is not alone in this, by the way: “Show me your flowchart and conceal your tables, and I shall continue to be mystified. Show me your tables, and I won’t usually need your flowchart; it’ll be obvious.” – Fred Brooks, The Mythical Man-Month. “Show me your code and conceal your data structures, and I shall continue to be mystified. Show me your data structures, and I won’t usually need your code; it’ll be obvious.” and “Smart data structures and dumb code works a lot better than the other way around.” – Eric S. Raymond, The Cathedral and The Bazaar.


“A wise engineering solution would produce—or better, exploit—reusable parts.” - Doug McIlroy

Other References

Reading Code Written by Brilliant Engineers

Views and Comments

Great code should be an expression of what needs to be done, not how to do it, abstracts away implementation details.

Values don’t have side effects.

Functional Programming Design Patterns

Source: Functional Programming Design Patterns

Courses

Code Review

Call Stack

The Factory Method Pattern and Its Implementation in Python

Source: https://realpython.com/factory-method-python/

Strategy Design Pattern

  • https://refactoring.guru/design-patterns/strategy/python/example
  • https://refactoring.guru/design-patterns/strategy
  • https://pytorch-lightning.readthedocs.io/en/stable/extensions/strategy.html

Dependency Injection

  • https://en.wikipedia.org/wiki/Dependency_injection#:~:text=In%20software%20engineering%2C%20dependency%20injection,leading%20to%20loosely%20coupled%20programs.

Principle of Least Power

  • https://www.lihaoyi.com/post/StrategicScalaStylePrincipleofLeastPower.html

The Call Stack and Stackoverflow

The stack is the place in memory where all your

  • local variables
  • function arguments

go

The program’s address space looks like this:

The Program's address space

How the stack grows:

How the Call Stack Changes

  • Everytime a function is called, it gets its own StackFrame in the stack.
  • A stackframe is a chunk of memory we added in the stack for a function call.
  • This StackFrame holds function arguments and local variables for a function call as well as the return address.
    • The return address is the place in code the program has to jump back to when the function is finished executing.
  • When another function is called, we overwrite the old StackFrames with new StackFrames.
  • Frame pointer helps us keep track of where the different StackFrames begin and end.
  • Stackoverflow occurs when we allow our stack to get too big and it overflows the memory the OS is willing to give us.
    • Common causes are very deep recursion and very large stack variables (eg. creating local array variables that are too large).

Looking at stack traces is really handy for helping us see how we got where we are. It’s useful when debugging.

Source: The Call Stack and Stack Overflows (example in C)

Race Conditions Explained

Source: https://www.baeldung.com/cs/race-conditions

Multithreading: Critical Section in Synchronization

Source: https://www.geeksforgeeks.org/g-fact-70/

Opportunistic Programming

Opportunistic Programming is a method of software development that emphasizes speed and ease of development over code robustness and maintainability.

Coding in this way allows individuals to explore many ideas quickly, which has an important place in the overall sofware engineering process.

Steps like prototyping, ideation and discovery are often best accomplished by building a functional piece of software quickly and easily without the typical concerns of a broader-scale software engineering project. Through fieldwork and a laboratory study, we are focusing on five characteristics of opportunistic programming: building software from scratch using high-level tools, adding new functionality through copy-and-paste, atypically rapid iteration, considering code to be impermanent, and facing a unique set of debugging challenges. Using results from these two projects, we plan on developing tools aimed at opportunistic programming that focus on debugging, code foraging and reuse, and documentation.

Source: https://hci.stanford.edu/research/opportunistic/

Pure function

Pure functions don’t have any side effects. They always return the result if the same arguments are passed.

Partial Functions

A partial function is a function that is not defined for all possible arguments of the specified type.

Eg.

def div(x, y):
    return x/y

The above function blows up when y = 0, so it is not defined for that case.

Rule of least power

The Rule of Least Power is the notion that a programmer should use the least powerful programming language required to code for a given requirement.

Design Smell: Temporal Coupling

https://blog.ploeh.dk/2011/05/24/DesignSmellTemporalCoupling/

Functional Programming Guide

https://mostly-adequate.gitbook.io/mostly-adequate-guide/

How to Learn Software Design and Architecture - a Roadmap

https://www.freecodecamp.org/news/software-design/

Making Illegal States Unrepresentable

  • https://oleb.net/blog/2018/03/making-illegal-states-unrepresentable/
  • https://khalilstemmler.com/articles/enterprise-typescript-nodejs/functional-error-handling/
  • https://khalilstemmler.com/blogs/typescript/when-to-use-a-private-constructor/
  • https://khalilstemmler.com/articles/typescript-domain-driven-design/make-illegal-states-unrepresentable/

Strategy Design Pattern

  • https://www.geeksforgeeks.org/strategy-method-python-design-patterns/
  • https://auth0.com/blog/strategy-design-pattern-in-python/
  • https://refactoring.guru/design-patterns/strategy/python/example
  • https://medium.com/nerd-for-tech/strategy-design-pattern-python-896f2d38012d
  • https://levelup.gitconnected.com/design-patterns-in-python-strategy-pattern-2189c540756d
  • https://refactoring.guru/design-patterns/strategy

Design Pattern

  • https://refactoring.guru/design-patterns/what-is-pattern

12 Steps to Production-Quality Data Science Code

https://towardsdatascience.com/12-steps-to-production-quality-data-science-code-35ae2f868003

The Rule of Least Power

  • https://www.w3.org/2001/tag/doc/leastPower.html

Single-Responsibility Principle

  • https://medium.com/@zackbunch/applying-the-single-responsibility-principle-in-python-462449fa52c7
  • https://towardsdatascience.com/the-single-responsibility-principle-in-python-d0ab0a681853
  • https://www.linkedin.com/pulse/solid-design-principles-python-examples-hiral-amodia
  • https://www.pythontutorial.net/python-oop/python-single-responsibility-principle/

Boundaries - Talk by Gary Bernardt

  • Source: https://www.youtube.com/watch?v=eOYal8elnZk&ab_channel=NextDayVideo

Videos to learn from

11 Best Domain-Driven Design Books

Source: https://realtoughcandy.com/domain-driven-design-books/

  1. https://www.amazon.ca/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215
  2. https://www.amazon.ca/Implementing-Domain-Driven-Design-Vaughn-Vernon/dp/0321834577
  3. https://www.amazon.ca/Domain-Modeling-Made-Functional-Domain-Driven/dp/1680502549
  4. https://www.amazon.ca/Learning-Domain-Driven-Design-Aligning-Architecture/dp/1098100131
  5. https://www.amazon.ca/Domain-Driven-Design-PHP-Carlos-Buenosvinos/dp/1787284948
  6. https://www.amazon.ca/Patterns-Principles-Practices-Domain-Driven-Design/dp/1118714709
  7. https://www.amazon.ca/Hands-Domain-Driven-Design-NET/dp/1788834097
  8. https://www.amazon.ca/Domain-Driven-Design-Distilled-Vaughn-Vernon/dp/0134434420
  9. https://www.amazon.ca/Applying-Domain-Driven-Design-Patterns-Examples/dp/0321268202
  10. https://www.amazon.ca/Practical-Domain-Driven-Design-Enterprise-Java/dp/1484245423
  11. https://www.amazon.ca/JavaScript-Domain-Driven-Design-Philipp-Fehre-dp-1784394327/dp/1784394327