50 Coding Laws That Would Make You A Decent Programmer
50 Coding Laws That Would Make You A Decent Programmer
Listen Share
There are hundreds or probably thousands of Python best practices out there and
depending on who you ask, you would get some slight variation on a given practice.
The internet has given everyone the right to voice an opinion. Including even me.
But in this article, we will be dealing with 50 Python best practices that are set in
stone.
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 1/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
These are practices that even God himself can’t tweak. These practices differentiate
the pro from the
You amateur andSign
are signed out. a lot of them
in with can also
your member be adapted for various
account
programming(mn__@p__.com)
languages. to view other member-only stories. Sign in
Most Python developers need somewhere to quickly test their code or debug errors. I
developed a website called python-fiddle.com which you can use to quickly test out a code
and it uses AI/LLMs to help find solutions to possible errors.
My Linkedln: https://www.linkedin.com/in/alexanderobidiegwu
LAW 1: Avoid Comments At All Cost
Comments have a way of being falsely true. They deviate the mind of the reader
from what the code is actually doing to what someone else says it’s doing.
This can become very problematic as time passes and the code receives updates or
changes. At some point, the comment becomes a lie and everyone now has to
observe the truth through the lens of the lie.
Comments must be avoided at all costs. A comment forces the reader to inherit your
thinking which at best is in the past. When a function or class changes, most likely,
its comments do not change along with it. Most likely, they block the reader from
thinking forward.
A comment signifies that the writer was mentally incapable of providing a well-
descriptive class, function, or variable name. It exposes the lackluster attitude of the
programmer and forces the team to inherit such an attitude.
In Law 14 and 15, you would learn when and when not to use a comment.
This can be quite redundant especially if the variable is intuitively a string. A name
But when the variable type isn’t intuitive, rather than specifying the type when
naming the variable, the best way to do this is to use type annotations.
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 2/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
It also makes the code very readable and less redundant. For example,
Goat.get_horn_length() instead of GetGoat.get_horn_length() .
It also eradicates the need for comments and allows any developer to mentally
conceptualize without having to look at the raw code.
This would help both you and the devs in your team know what to expect without
always having to use print statements to get a visual understanding.
Bad Practice
Run code
Good Practice
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 3/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
Run code
(Note that the methods and attributes are just for example purposes.)
Python fiddle
This checks if the address is valid and after checking returns the latitude and
longitude. The function does two things. Checks if the address is valid and returns
the geolocation of the address.
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 4/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
https://python-fiddle.com/saved/bQD3sPFuC4BWeTx1gdLr
Knowing exactly what is “one” functionality can be a bit difficult, especially for new
devs. You need to be very specific on what you want a function to do.
Usually, if you can extract or group some of the actions in a function as a whole
other function, then it’s probably doing more than one thing.
Another way you can tell is if it has more than one level of abstraction…
In other words, the function should focus on a specific level of detail or complexity,
and all the functions’ operations should operate at that same level.
Good Practice
https://python-fiddle.com/saved/ezGiYtWK72HXiVm65azM?run=true
This function has statements that are of a low level of abstraction. Things like sum ,
len, etc.
Bad Practice
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 5/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
LAW 8: A Function and It’s Argument(s) Should Be Like Brothers And Sisters
A function name should be closely tied to its arguments. You don’t want to have a
function that seems to do one thing and the arguments passed do not correlate with
the function name.
Bad Practice
write(True)
Good Practice
write(name)
The second one is more descriptive of what exactly the function is doing. It is clear
to whoever reads this that we are writing a name.
The first one isn’t as explicit as the second. You have to make guesses and possibly
even have to view the entire function.
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 6/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
https://python-fiddle.com/saved/BfPEC7YYSEHQhz2xgGTW
My Linkedln: https://www.linkedin.com/in/alexanderobidiegwu
But what is a clean code? A clean code is well structured and arranged.
A clean code doesn’t hide bugs. It exposes anywhere a bug could hide to the
programmer and makes room for an easy fix without complete refactoring.
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 7/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
https://python-fiddle.com/saved/6kfW6eccopYfotqaKlcR?run=true
This fails to adhere to OCP because whenever there’s a new country, we would need
to write a new if statement to complement that. This might seem simple now but
imagine we have 100 or more countries to take into account. How would that look?
https://python-fiddle.com/saved/AEbfUZvVQ0k55RyHcypB
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 8/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
This is a more robust solution because now we don’t need to modify either the class
or its functions.
YouIfare
we everout.
signed wantSignto
in consider a country
with your member and its capital, we can simply
account
(mn__@p__.com) to view other member-only stories. Sign in
adjust our capital dictionary.
For example:
This is a wrong method because anytime we want to add a new payment method, we
would always need to modify the PaymentProcessor class.
This way, whenever we need to add a new payment option, say crypto or paypal, we
wouldn’t need to edit or modify any class to achieve this. we could simply do this:
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 9/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
class PaymentProcessor:
@abstractmethod
def pay_tax(amount, crypto):
pass
Then when calling each payment processor, we declare the crypto argument as a
None type or give it a default value to avoid passing in any argument if not needed.
Both cases fail to adhere to the Liskov Substitution Principle.
This is because the parent class or the abstract class, contains an argument that isn’t
relevant to most of our subclasses
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 10/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
The Liskov Substitution principle (LSP) states that “Objects of a superclass should be
replaceable with objects
You of its
are signed subclasses
out. Sign in withwithout affecting
your member the correctness of the program”.
account
(mn__@p__.com) to view other member-only stories. Sign in
To adhere to LSP, we would need to define the cryptocurrency within the
CryptoPaymentProcessor class to avoid any irrelevant clash with other subclasses.
class CryptoPaymentProcessor(PaymentProcessor):
def __init__(self, crypto):
self.crypto = crypto
def pay_tax(amount):
print(f'Your {self.crypto} wallet is being processed for tax payment')
print(f'You are to be charged {amount}')
Informative comments
Making comments informative can always help express the code to the reader. A
comment that highlights the return value of a function, for instance, would provide
more clarity. But comments like this can be made redundant through the use of
well-descriptive functions or variable names.
TODO comments
Comments like this help other programmers know that this is an unfinished
function/task or it requires modification. Maybe there’s a better way to implement a
certain function that wasn’t taken advantage of. Maybe the code fails periodically.
Regardless of your reason, comments like this provide more value than they take.
TODO comments are also less likely to be left untouched as the code changes or
improves because they are usually remembered to be removed once the task has
been completed or modified properly.
Warning of consequences
Sometimes, we want to tell other developers about potential landmines. Stepping on
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 11/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
these landmines could have some unforeseen consequences. And we want to all
survive through
Youthe day. Comments
are signed can
out. Sign in with save
your the day
member in this situation.
account
(mn__@p__.com) to view other member-only stories. Sign in
Suppose a certain code takes time or has the possibility of overloading certain
systems, a warning like #CONSUMES A LOT OF COMPUTING RESOURCES would be beneficial
to a reader or other programmers.
Unobvious comments
We often write comments that seem obvious to us but not to someone else. The
connection between your comments and the function referenced must be clear.
They must both follow the same step or procedure at sync. You don’t want your
comment to need to have its comment as well.
Short Functions
Most likely we do not need a comment for a short function. The shorter/smaller the
function, the more likely it can be described with a good name. Hence, they are
usually self-descriptive.
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 12/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
obvious benefits such as reusability and readability. They are also easier to maintain
and update since wesigned
You are spend less
out. Signtime scrolling
in with andaccount
your member trying to connect the dots.
(mn__@p__.com) to view other member-only stories. Sign in
LAW 17: Know When To Use Blank Lines
Blank lines are a way to tell the reader we are progressing into a new and separate
concept. Each group of lines represents a complete thought. It helps readers
understand when a thought ends.
LAW 18: Keep Related Lines Of Code/Functions/Classes Close
Can you remember when you had to scroll all the way to the top of the script, just to
find out what a function does and how it relates to where it’s being called?
The worst part of this is you can’t understand the relation in one glance. It’s a back-
and-forth movement. Once you’ve experienced this, you’ll understand how valuable
it is to keep related code to each other.
A function/Variable should be as close to where it’s needed the most or has the most
importance. It should be furthest away from where it has the least importance.
def create(name):
print(name)
There exists no whitespace between the parenthesis between the function and the
name variable. If there was, it would look very disjointed and uncoordinated…
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 13/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
Arguments passed into a function, should be separated to show that the arguments
are separate. You are signed out. Sign in with your member account
(mn__@p__.com) to view other member-only stories. Sign in
LAW 20: Obey Team Rules
Almost every developer has his/her style. From the way we name our files, to the
way we write print statements.
But when it comes to working with other devs, you want to dumb down those
personal preferences and adopt the team’s preferences. Not everyone might be able
to see beauty in your code the way you do.
For example, let’s say you have a Page that displays the last 50 Orders in a “Your
Open in app Sign up Sign in
Orders” Overview Page. 50 is the Magic Number here because it’s not set through
standard or convention, it’s a number that you made up for reasons outlined in the
Search
spec.
Now, what you do is you have the 50 in different places — your SQL script ( SELECT
TOP 50 * FROM orders ), your Website (Your Last 50 Orders), your order login ( for (i
# Bad
SELECT TOP 50 * FROM orders
# Good
NUM_OF_ORDERS = 50
SELECT TOP NUM_OF_ORDERS * FROM orders
# Bad
if x:
if y:
do_something()
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 14/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
# Good
You are signed out. Sign in with your member account
if x and y:
(mn__@p__.com) to view other member-only stories. Sign in
do_something()
# Bad
temp_result = calculate(x, y)
final_result = temp_result * 2
# Good
final_result = calculate(x, y) * 2
# Bad
def calc(x, y):
pass
# Good
def calculate_total_price(quantity, unit_price):
pass
# Bad
file_path = "/path/to/file.txt"
# Good
import os
file_path = os.getenv("FILE_PATH")
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 15/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
When writing code, it’s always best to include error handling also. This can help
speed up the debugging process
You are signed out. Signand increase
in with the account
your member sophistication of the code while
(mn__@p__.com) to view other member-only stories. Sign in
keeping it clean and manageable.
You want to use try-catch statements when a certain code is more likely to return an
error.
Things like API requests, file handling, etc tend to fail or raise errors due to one
reason or the other. Using try-catch statements for multiplication or division is just
uncalled for and creates more problems than it solves.
Create informative error messages along with your exceptions which can be done
when printing the error. Mention the context in which the operation failed and the
type of failure.
try:
pass
except ValueError:
pass
except TypeError:
pass
except IndexError:
pass
except KeyError:
pass
except FileNotFoundError:
pass
This is extremely bogus and takes away (in verbosity, complexity, and maintenance)
the additional help it provides in error handling.
It’s often better to use a more general exception to catch any sort of error we might
come across. This type of exception, by default, includes the type of error we got.
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 16/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
try: You are signed out. Sign in with your member account
pass (mn__@p__.com) to view other member-only stories. Sign in
except Exception:
pass
Only be specific about the type of error you want to catch, when you want all other
errors to pass through.
LAW 29: A function must either mutate or return something, but not both.
Whenever we are creating a function, we should keep in mind what exactly that
function is supposed to do. Does it mutate the arguments passed? Or does it need to
return something?
If the function mutates the argument(s) passed, we only need to do that in that
function and nothing else.
But what do I mean by mutation? If the function changes the contents of the
argument(s) or changes the data type of the arguments, it mutates.
def changed(array):
array.append('hello')
If the argument(s) is used to create another variable, then it doesn’t mutate. For
instance, if an argument called time was used to calculate a distance , then it
doesn’t mutate, which means distance can be returned in that function.
But there’s a way to get the best of both worlds. You can copy the argument(s) of the
function and perform mutation on those. This way we avoid any side effects.
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 17/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
def changed(array):
You are signed out. Sign in with your member account
array_copy = array[:]
(mn__@p__.com) to view other member-only stories. Sign in
array_copy.append(4)
return array_copy
If a function returns something without mutating its arguments, the function’s name
should be a noun. If a function mutates the arguments and returns none, it should
be a verb.
This is a common convention built into Python itself. Methods such as sort, and
append are verbs because they mutate the data type and return None while methods
like sorted, sum, product are all nouns because they don’t mutate any arguments
passed and return a new copy of the data.
There are obviously exceptions to this and whenever you feel you’ve encountered
one, feel free to fall back on using verbs.
LAW 31: Classes should be small
Yep! Classes should be as small as possible. Just like functions.
The only difference is that in functions, size is determined by the number of lines in
that function while in classes, it is determined by the number of responsibilities in
that class.
Usually, a class name represents the kind of responsibilities it might possess but
when the name is ambiguous or too general, most likely we are giving it too much
responsibility.
This takes us back to SRP (single responsibility principle) which states that a class
should only have one reason — one responsibility — to change.
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 18/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
class Animal:
You are signed out. Sign in with your member account
def __init__(self,
(mn__@p__.com) name):
to view other member-only stories. Sign in
self.name = name #instance variable
If all our functions are related to the class responsibility, there’s no reason why we
should have a lot of instance variables.
When we begin to have tons of instance variables, is when certain functions in the
classes deviate away from the core responsibility of the class.
These functions tend to come with their own variables that other functions in the
class do not need.
# Bad
file = open("example.txt", "r")
data = file.read()
file.close()
# Good
with open("example.txt", "r") as file:
data = file.read()
# Bad
result = "even" if number % 2 == 0 else "odd" if number % 3 == 0 else "neither"
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 19/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
# Good
if number % 2 == 0:
resultYou
= are signed out. Sign in with your member account
"even"
(mn__@p__.com)
elif number % 3 == 0: to view other member-only stories. Sign in
result = "odd"
else:
result = "neither"
LAW 36: Use ‘is’ and ‘is not’ for Identity Comparison
Most of the time, we use == to check the comparison between two variables. This is
usually okay for immutable data types like strings or integers because immutable
objects with the same value are usually stored in the same memory location, so a
memory location check is not needed.
But when dealing with mutable data types, such as list, dicts and custom objects,
it’s often better to use the is comparator because it checks the subtype of the
variable and the memory location.
The memory location of mutable objects are usually not the same due to the way
Python works. Python stores mutable objects in different memory locations because
they can be changed at any time and each of them must be independent of one
another even though.
# Preferred: Using is
if list1 is list2:
print("Lists are the same object")
# Note: In this case, list1 and list2 are different objects with the same value
# so using `is` would give a different result than `==`.
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 20/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
It states that high-level modules should not depend on low-level modules, but both
should depend on abstractions.
In other words, classes should depend on interfaces or abstract classes rather than
on concrete implementations.
# Bad
class Logger:
def log(self, message):
with open('log.txt', 'a') as f:
f.write(message + '\n')
class Calculator:
def __init__(self):
self.logger = Logger()
In the example above, we define the class Logger and directly create a new instance
of it in our Calcuator class. This means that Calculator now depends on Logger
class and if for any reason we change Logger class, we now also have to modify
Calculator class.
And as you can see this also fails to adhere to the open closed principle (open for
extension, closed for modification).
This tight coupling makes the code harder to test because we can no longer simply
use a fake logger class when testing.
# good
from abc import ABC, abstractmethod
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 21/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
class LoggerInterface(ABC):
@abstractmethod
You are signed
def log(self, out. Sign in with your member account
message):
(mn__@p__.com) to view other member-only stories. Sign in
pass
class Logger(LoggerInterface):
def log(self, message):
with open('log.txt', 'a') as f:
f.write(message + '\n')
class Calculator:
def __init__(self, logger: LoggerInterface):
self.logger = logger
# Bad
assert x > 0, "x should be positive"
# Good
if x <= 0:
raise ValueError("x should be positive")
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 22/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
def calculate_discount(price):
You are signed out. Sign in with your member account
discount = price * 0.1
(mn__@p__.com) # other
to view 10% discount
member-only stories. Sign in
return price - discount
The example below uses the hard-coded number 0.1 to represent a 10% discount.
def calculate_discount(price):
TEN_PERCENT_DISCOUNT = 0.1
discount = price * TEN_PERCENT_DISCOUNT
return price - discount
The improved code replaces the hard-coded number with a named constant
TEN_PERCENT_DISCOUNT . The name instantly conveys the meaning of the value, making
the code more self-documenting.
LAW 40: Follow the DRY (Don’t Repeat Yourself) Principle
Avoid writing the same code more than once. Instead, reuse your code using
functions, classes, modules, libraries, or other abstractions. This makes your code
more efficient, consistent, and maintainable.
It also reduces the risk of errors and bugs as you only need to modify your code in
one place if you need to change or update it.
# Bad
# Good
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 23/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
class Order:
def __init__(self, customer):
self.customer = customer
def get_customer_name(self):
# Violation: Order knows too much about the customer's structure
return self.customer.get_profile().get_name()
In this example, the Order class directly accesses the customer’s profile to retrieve
the customer’s name. This violates the Law of Demeter because Order is reaching
into the internal structure of the Customer object to access its profile and name.
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 24/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
It has gone past an immediate neighbor and now knows too much about the
customer’s object.
You are signed out. Sign in with your member account
(mn__@p__.com) to view other member-only stories. Sign in
class Order:
def __init__(self, customer):
self.customer = customer
def get_customer_name(self):
# Adherence: Order only interacts with its immediate collaborator
return self.customer.get_name()
In this adherent example, the Order class only interacts with its immediate
collaborator, the Customer object, and calls a method directly on it to retrieve the
customer’s name.
It does not reach into the internal structure of the Customer object, thus following
the Law of Demeter.
That is why the readability of code is always more important than its conciseness
when it comes to software development.
There is no point in writing concise code if other developers cannot understand it.
Imagine you want to write a function with the name get_file . You click g and your
IDE recommends you a list of functions/classes/variables that start with the letter g.
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 25/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
This becomes more of a pain when you want to call that function. Your function
name could beYou
lost
arein between
signed the
out. Sign recommendations
in with and now your IDE becomes
your member account
(mn__@p__.com)
more of a problem to view other
than an efficient member-only stories. Sign in
solution.
# Bad
from module import *
# Good
from module import symbol1, symbol2
This often means separating the startup process, — which is when our dependencies
and objects are wired together — , from the run time logic, — which is when the
logic of the application executes based on inputs from the user or other triggers — .
One common way to separate construction from its use is to construct the
application logic in a file/function/module called main .
The main function builds the objects necessary for the application to run smoothly.
This frees other modules from being tightly coupled to the application and
promotes reusability and modularity.
LAW 47: Simple Design Contains All These Rules
Runs all the tests: A system might have a perfect design on paper but if there’s no
way to verify that the system actually works as intended, then the design on paper
becomes questionable.
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 26/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
# Bad
try:
try:
# Code that might raise errors
pass
except ValueError:
# Handle ValueError
pass
except Exception as e:
# Handle any other unexpected errors
pass
# Good
try:
# Code that might raise errors
pass
except ValueError:
# Handle ValueError
pass
except Exception as e:
# Handle any other unexpected errors
pass
It’s also very easy to write clean code when implementing very faulty concurrency
functionality. Usually, you might not even be aware that it is faulty until a lot of
stress is put on the system.
There are multiple reasons why your concurrency code might fail. Here are some:
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 27/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
Deadlocks: Deadlocks occur when two or more threads or processes are blocked
indefinitely, waiting for each other to release resources that they need. This can
happen when each process holds one resource and waits for another resource held
by another process, creating a cyclic dependency.
LAW 50: Follow The 49 Rules
These laws are only there to guide you on your journey as a software engineer. You
should abide by them whenever needed.
But as you grow in experience and skills, you want to be able to decide when it’s best
to follow a certain rule and when it’s not.
This gut feeling/intuition only comes to those who have mastered their craft and if
you’re a newbie or you just started your career 2 years ago, it’s usually best you
follow these laws like it’s your only ticket to heaven.
Most Python developers need somewhere to quickly test their code or debug errors. I
developed a website called python-fiddle.com which you can use to quickly test out a code
and it uses AI/LLMs to help find solutions to possible errors.
Follow
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 28/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
Alexander obidiegwu
10 Hard Python Projects For Intermediates To Boost Your Python Skills &
Portfolio.
Timeline to finish each project — 1 month
1K 6
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 29/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
Alexander obidiegwu
63
Alexander obidiegwu
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 30/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
You would most likely fail as a Python programmer without understanding this ground concept.
Asynchronous Programming.
You are signed out. Sign in with your member account
(mn__@p__.com) to view other member-only stories. Sign in
· 8 min read · Dec 15, 2023
381 1
Alexander obidiegwu
260 1
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 31/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
LORY
2K 34
1.3K 31
Lists
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 33/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
9.99K 128
You are signed out. Sign in with your member account
(mn__@p__.com) to view other member-only stories. Sign in
4.8K 43
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 34/35
27/05/2024, 14:48 50 Coding Laws That Would Make You A Decent Programmer. | by Alexander obidiegwu | Medium
6.6K 186
4.2K 22
https://medium.com/@alexobidiegwu/50-laws-of-best-practices-in-python-6942c7cafd56 35/35