DEV Community

Cover image for From Good to Great Code with OptimizeIt
Majd Al Mnayer
Majd Al Mnayer

Posted on

From Good to Great Code with OptimizeIt

For my first assignment in the Topics in Open Source course, we are required to create a unique tool using LLP chat completions. My inspiration for the project stemmed from my most frequent use of ChatGPT. Primarily, I utilize ChatGPT for generating ideas on better code implementation and structure. Typically, I engage with it to explore new technologies through code snippets, gain conceptual understanding, and occasionally, receive suggestions to further optimize my code. However, repeatedly constructing prompts and specifying particular changes can be quite cumbersome. This realization led me to conceive a dedicated tool that I could personally use to streamline this process.

Introducing: OptimizeIt

OptimizeIt is a command-line tool crafted to assist developers in enhancing source code for both performance and readability. While no solution is entirely flawless, OptimizeIt excels in its domain. It benefits from meticulous prompt engineering and can even identify when it encounters a problematic file. Additionally, it features a comparison display of the source code before and after processing, providing users with a clear view of all modifications and enhancements.

OptimizeIt Demo

If you are interested, here is a thorough Video of OptimizeIt in action.

How Was OptimizeIt Built?

OptimizeIt was designed with simplicity and efficiency in mind, utilizing a minimal set of dependencies to maintain a straightforward implementation. The core packages include:

  • Eslint: Guarantees optimal code structure and adherence to coding standards.
  • Prettier: Ensures the code is elegantly formatted.
  • Groq-SDK: Facilitates communication with Groq LLMs.
  • Marked: Converts Markdown into HTML.

The development of OptimizeIt leveraged TypeScript to enhance robustness and mitigate build-time errors. Initially, I considered using C++, which could potentially offer faster execution. However, the primary latency in OptimizeIt stems from the response time of Groq LLMs, not from the performance of the tool itself. Given this, the benefits of C++ in terms of performance did not outweigh the simplicity and rapid development cycle offered by TypeScript.

OptimizeIt is also built with maintainability and scalability at its core. The codebase is well-organized and modular, making it straightforward to add new features or adapt existing functionalities.

Features

OptimizeIt boasts a variety of features, with plans to expand its capabilities even further. Currently, here are the command-line arguments it supports, along with examples of their usage:

Version

This flag prints the current version of OptimizeIt and the name of the tool, usage:

optimizeit --version
optimizeit -v
Enter fullscreen mode Exit fullscreen mode

OptimizeIt Version

HTML

This is a great feature which generates a changes.html html file in the output directory, displaying side-by-side the before and after of each processed file, usage:

OptimizeIt HTML

OptimizeIt HTML2

OptimizeIt HTML3

optimizeit file1 --html
optimizeit file1 file2 --html
Enter fullscreen mode Exit fullscreen mode

Markdown

This is a nifty feature, where optimizeit creates a changes.md file for you in the output directory, displaying the before and after of every file provided after execution, usage:

OptimizeIt Markdown

OptimizeIt Markdown2

optimizeit file1 --markdown
optimizeit file1 file2 --markdown
optimizeit file1 -md
optimizeit file1 file2 --md
Enter fullscreen mode Exit fullscreen mode

Output

This creates a file in the output folder containing all the changes, you may provide more than 1 output file per file given, usage:

OptimizeIt Output

And the resulting main.py file is:

def max_product(numbers):
    max_num = second_max_num = float('-inf')
    for num in numbers:
        if num > max_num:
            second_max_num = max_num
            max_num = num
        elif num > second_max_num and num != max_num:
            second_max_num = num
    return max_num * second_max_num
Enter fullscreen mode Exit fullscreen mode
optimizeit file1 --output file1
optimizeit file1 file2 --output file1 file2
optimizeit file1 -o file1
optimizeit file1 file2 -o file1 file2
Enter fullscreen mode Exit fullscreen mode

Model

This specifies the model name that OptimizeIt uses, you may choose any model name from groq, usage:

OptimizeIt Model

optimizeit file1 --model model_name
optimizeit file1 file2 --model model_name
optimizeit file1 -m model_name
optimizeit file1 file2 -m model_name
Enter fullscreen mode Exit fullscreen mode

Temperature

This specifies the temperature of the model that OptimizeIt is using, you may specify values from 0.1 to 2.0 although, the higher the temperature the more hallucinations, usage:

2.0 - Hallucination

OptimizeIt Temperature

0.5 - Regular

OptimizeIt Temperature2

optimizeit file1 --temperature 0.1
optimizeit file1 file2 --temperature 0.1
optimizeit file1 -t 0.1
optimizeit file1 file2 -t 0.1
Enter fullscreen mode Exit fullscreen mode

API Key

This allows you to set your own groq api key for OptimizeIt to use! This can be very helpful with pay-gated models that groq provides, usage:

OptimizeIt API Key

optimizeit file1 --api-key your_api_key
optimizeit file1 file2 --api-key your_api_key
optimizeit file1 -a your_api_key
optimizeit file1 file2 -a your_api_key
Enter fullscreen mode Exit fullscreen mode

Help

This flag displays all of the details and configurations that OptimizeIt has, usage:

OptimizeIt Help

optimizeit --help
optimizeit -h
Enter fullscreen mode Exit fullscreen mode

Getting Started

Getting started is super straight forward, with only three steps:

Step 1

Create a .env file, and provide your api key:

GROQ_API_KEY=YOUR_API_KEY_HERE
Enter fullscreen mode Exit fullscreen mode

Alternatively, you can use the API Key flag directly in your commands if you prefer.

Step 2

Build OptimizeIt:

npm run build
Enter fullscreen mode Exit fullscreen mode

Step 3

Create a symbolic link between the package and your project directory, allowing you to use OptimizeIt from anywhere in your terminal:

npm link
Enter fullscreen mode Exit fullscreen mode

Step 4

Everything's set up, and you're ready to optimize your code. What are you waiting for? Try it out and see the improvements OptimizeIt can bring to your projects!

What's Next?

OptimizeIt has many potential improvements, such as:

  • Project-Wide Optimization: Extends the tool to accept a directory path instead of individual files. The tool would recursively scan the directory, identifying all source files and optimizing them. This would be useful for large projects, allowing developers to optimize their entire codebase in one go.
  • Interactive Mode: Allows users to review suggested changes before they are applied, or ask for another suggestion which might be better. This provides more control over the optimization process.
  • Integration with Git: Automatically commit changes after optimization. This could also allow users to specify branches, review changes with diffs, or revert specific changes if needed.
  • Custom Optimization Profiles: Allow users to create and save custom optimization profiles with predefined settings (like models, temperatures, and flags) for different types of files or projects.

These planned improvements reflect a commitment to making OptimizeIt not just a tool, but a versatile companion for developers looking to enhance their coding efficiency and quality. Stay tuned for these updates and more as OptimizeIt continues to expand its capabilities!

Conclusion

OptimizeIt is freely available under the MIT license, inviting contributors to this super fun project. It's been an absolute pleasure working on OptimizeIt, with Groq, and setting my step in the open source community. This project will definitely see some upgrades in the near future, because I know that I will use it myself!

Top comments (0)