iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🌞

Coding Assistance Directly from Your Terminal! Features and Usage of Claude Code

に公開

Claude Code is a command-line tool provided by Anthropic. It allows developers to access Claude directly from their terminal to assist with coding tasks.

Key Features

  • Terminal-native interface: Directly accessible from the terminal, the developer's natural working environment.
  • Code understanding and generation: Understands complex codebases and generates new code.
  • File system operations: Can read, create, and update files and directories within a project.
  • Intelligent task management: Understands the developer's intent and executes complex tasks autonomously.

Use Cases

  • Support for implementing new features
  • Bug diagnosis and fixing
  • Refactoring existing code
  • Assistance with understanding complex code
  • Automation of test creation

Installation

Here are the requirements for execution:

  • Operating System
    • macOS 10.15+
    • Ubuntu 20.04+/Debian 10+
    • Windows via WSL
  • Hardware: Minimum 4GB RAM
  • Software: Node.js 18 or higher
node -v

Check your Node.js version with the above command. If it is 18 or higher, you can install it using the following command:

npm install -g claude-code

Once installed, try running the claude command immediately.

claude

Various prompts will appear; for now, you can just press Enter. It's complete once the prompt input screen appears.

Code Explanation

To test Claude Code, I wrote a simple, quick prime number checking code. It doesn't have to be exactly the same, but I'll provide it here for those who want to follow along in the same environment. I have intentionally included a bug.

Program
def is_prime(n):
    if n % 2 == 0:
        return False
    for i in range(3, n):
        if n % i == 0:
            return False
    else:
        return True

n = int(input())
print(is_prime(n))

First, I create CLAUDE.md with /init. The contents were as follows:

# CLAUDE.md - Coding Assistant Guidelines

## Commands
- Run Python script: `python prime.py`
- Run tests: `pytest`
- Run single test: `pytest path/to/test.py::test_function_name`
- Lint code: `flake8`
- Type check: `mypy *.py`

## Code Style Guidelines
- **Imports**: Group standard library imports first, then third-party, then local
- **Formatting**: Follow PEP 8 guidelines (4 spaces indentation)
- **Types**: Add type hints to function parameters and return values
- **Naming**:
  - snake_case for variables and functions
  - CamelCase for classes
  - UPPER_CASE for constants
- **Error Handling**: Use try/except blocks with specific exceptions
- **Functions**: Small, single-purpose functions with docstrings
- **Comments**: Use docstrings for functions; inline comments only when necessary
- **Testing**: Write tests for all new functionality

CLAUDE.md is a document generated when running the claude init command using Claude Code, serving as a guideline for defining project code styles and best practices. The content of CLAUDE.md consists of a project overview, command usage, code style guidelines, best practices, and more.

Next, let's ask for a more detailed explanation of the content.

claude "explain this project"

Entering this will display the following:

This is a simple Python project that checks if a number is prime. 
It has a single file (prime.py) with a function that determines if a number is prime, then takes user input and prints the result.

In this way, Claude Code summarizes the project's overview and explains its structure and functionality.
There are some issues with this code, so let's fix them next.

I'm seeing an error when I run this code.

Entering this causes Claude Code to identify the errors and display suggestions for fixes.

There are several issues with the prime number checking algorithm:

  1. It incorrectly returns False for n=2 (which is prime)
  2. It doesn't handle n≤1 (which aren't prime)
  3. The range should go up to sqrt(n), not n
  4. The for-else construct isn't necessary

What specific error are you seeing when you run the code?

When explaining the error:

2 is a prime number, but it is printed as False.

Entering this, Claude Code provides a fix suggestion.

When I pressed "yes," it applied the fix. Let's test if it's working correctly.

add test cases for edge conditions in prime.py
run the new tests and fix any failures

Entering this, Claude Code adds test cases, runs the tests, and displays the results.
At first, it produced errors, but after pressing the Enter key a few times, the tests passed.

You can check the cost incurred for the execution so far with /cost.
This time it was $0.2034. Hmm, it uses more than I expected.
If you are concerned about costs, it seems helpful to use it with the following in mind:

  • Keep conversations compact: Use /compact when the context size grows.
  • Write specific queries: Avoid ambiguous requests that cause unnecessary scanning.
  • Decompose complex tasks: Break down large tasks for more focused interaction.
  • Clear history between tasks: Use /clear to reset the context.

It seems beneficial to keep these points in mind when using the tool.

Summary

In this post, I tried out Claude Code.
I thought it was great that everything can be completed entirely within the terminal.
I believe similar workflows are possible with tools like Cursor or Aider, but I haven't used them myself, so if you have, please let me know your impressions.
I didn't try it this time, but since it can also perform tasks like creating PRs and configuring MCP servers, I'd like to explore those features as well.
Claude Code is still in the research preview stage, and I'm looking forward to its future improvements.

Discussion