travisthomson's avatar

Source diving vs. docs

I saw a prominent developer on X talking about how he source dives rather than reading the docs for packages/libraries.

I'm wondering if this is common. It seems like hard mode - and less efficient? Some of these libraries are quite large.

Does this actually turn out to be more efficient once you're used to it? If anyone here does this, do you have a systematic approach, or just start reading the source from top to bottom?

Thanks for any insight.

0 likes
4 replies
LaryAI's avatar
Level 58

Source diving versus reading documentation is a topic that often comes up among developers. Both approaches have their merits, and the choice between them can depend on various factors such as the complexity of the library, the quality of the documentation, and personal preference.

Source Diving

Pros:

  1. Deep Understanding: By reading the source code, you gain a deeper understanding of how the library works internally. This can be particularly useful for debugging or extending the library.
  2. Up-to-Date Information: Sometimes, documentation can be outdated or incomplete. The source code is the ultimate source of truth.
  3. Learning Opportunity: Reading well-written code can be a great learning experience, helping you to improve your own coding skills.

Cons:

  1. Time-Consuming: Diving into the source code can be time-consuming, especially for large libraries.
  2. Steep Learning Curve: It can be challenging to understand the code without any context or guidance, particularly if the codebase is complex.

Reading Documentation

Pros:

  1. Efficiency: Good documentation can provide a quick overview and examples, making it faster to get started.
  2. Guidance: Documentation often includes best practices, common use cases, and troubleshooting tips.
  3. Community Support: Documentation is usually written with the user in mind, making it easier to understand and follow.

Cons:

  1. Quality Varies: The quality of documentation can vary greatly. Poor documentation can be confusing or incomplete.
  2. Limited Depth: Documentation might not cover all edge cases or provide a deep understanding of the internal workings.

Systematic Approach to Source Diving

If you decide to dive into the source code, having a systematic approach can make the process more efficient:

  1. Start with the Entry Point: Identify the main entry point of the library. This is often the file or class that gets instantiated first.
  2. Follow the Flow: Trace the flow of execution from the entry point. Look at how different components interact with each other.
  3. Read Tests: If the library has a good test suite, reading the tests can provide insights into how the library is intended to be used.
  4. Use Debugging Tools: Tools like debuggers or logging can help you understand the flow of execution and the state of the application at various points.
  5. Take Notes: Document your findings as you go. This can help you keep track of important details and can be useful for future reference.

Example

Let's say you're diving into the source code of a popular PHP library like Guzzle (an HTTP client). Here's a step-by-step approach:

  1. Identify the Entry Point:

    // Typically, you start by looking at the main class or the factory method.
    $client = new \GuzzleHttp\Client();
    
  2. Trace the Flow:

    // Look at the constructor of the Client class to see what dependencies are being injected.
    public function __construct(array $config = [])
    {
        // Initialization code here
    }
    
  3. Read Tests:

    // Check the tests directory for usage examples.
    public function testCanSendRequest()
    {
        $client = new \GuzzleHttp\Client();
        $response = $client->request('GET', 'https://api.example.com');
        $this->assertEquals(200, $response->getStatusCode());
    }
    
  4. Use Debugging Tools:

    // Use a debugger to step through the code.
    xdebug_break();
    $client->request('GET', 'https://api.example.com');
    
  5. Take Notes:

    ## Guzzle Client Initialization
    - The Client class constructor accepts an array of configuration options.
    - Key components initialized include the handler stack, middleware, etc.
    

Conclusion

Both source diving and reading documentation have their place in a developer's toolkit. While source diving can provide a deeper understanding, reading documentation is often more efficient for getting started. A balanced approach, using both methods as needed, is usually the most effective strategy.

jlrdw's avatar

Does this actually turn out to be more efficient once you're used to it

Depends, I have a custom framework I update now and then. I have learned a lot from the source. Also the symfony request and response source.

Like:

return call_user_func_array(array($instance, $method), $args);

verses

return $instance->$method(...$args);

The latter I learn from the source. All the source is, is the actual methods that run laravel shortcuts.

But for a general user who is not interested in the behind the scenes, it's just a choice to make.

2 likes
puklipo's avatar

I look at both.

But what I look at most is the commit history.

2 likes
martinbean's avatar
Level 80

@travisthomson It’s not either/or. You should be comfortable diving in to and reading the source in additional to reading the docs.

The docs are good for showing you how you would implement features in an application. But source diving is more for learning how those features are actually code and how they actually work under the hood. If you‘re familiar with the source code, then you can become a more proficient developer as you have a better knowledge on how to extend the framework as use it to its full advantage. You may also discover some features that aren’t documented at all.

2 likes

Please or to participate in this conversation.