Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Principles of Clean Code | Foundations of Code Quality
Code Quality and Refactoring in Python

bookPrinciples of Clean Code

メニューを表示するにはスワイプしてください

Writing clean code is essential for making your Python programs easy to read, understand, and maintain. Clean code is not just about making your code look nice — it's about making it obvious what your code does, reducing the chance of errors, and making future changes simpler. Some of the most important principles include using meaningful names for variables and functions, keeping functions small, ensuring each function or piece of code has a single responsibility, and avoiding unwanted side effects that can make code unpredictable.

Using meaningful names means choosing variable and function names that clearly describe what they represent or do. This helps anyone reading your code — including your future self — quickly grasp its purpose without needing extra comments. Small functions are easier to understand and test. Each function should do one thing and do it well, which is the heart of the single responsibility principle. Finally, avoiding side effects means making sure that functions do not unexpectedly change variables or states outside their own scope. This makes your code more reliable and easier to debug.

12345678910111213141516171819
def calculate_total_price(items): """ Calculate the total price of a list of items. Each item is a dictionary with 'price' and 'quantity'. """ total = 0 for item in items: item_total = item['price'] * item['quantity'] total += item_total return total # Example usage cart = [ {'price': 9.99, 'quantity': 2}, {'price': 5.49, 'quantity': 1}, {'price': 3.50, 'quantity': 4} ] print(f"Total price: ${calculate_total_price(cart):.2f}")
copy

1. What is the main benefit of using meaningful variable names?

2. Which principles contribute to clean code in Python? (Select all that apply.)

question mark

What is the main benefit of using meaningful variable names?

正しい答えを選んでください

question mark

Which principles contribute to clean code in Python? (Select all that apply.)

すべての正しい答えを選択

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  3

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  3
some-alt