IntelliJ
camel case
underscore
toggling
programming tips

In Intellij, how do I toggle between camel case and underscore spaced?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In IntelliJ IDEA, developers often need to switch between different naming conventions such as camel case and underscore spaced (also known as snake case). This is particularly useful when coding in languages like Java or Python, which often require distinct naming styles for variables or methods.

Introduction

Modern integrated development environments (IDEs) like IntelliJ IDEA offer various features to enhance productivity, including tools for refactoring and naming conventions. One such feature is the capability to toggle between camel case and underscore spaced. This transformation can save significant time and effort, especially in large codebases.

Understanding Naming Conventions

  • Camel Case: Used primarily in Java, this notation starts with a lowercase letter and capitalizes the first letter of subsequent concatenated words (e.g., exampleVariableName).
  • Underscore Spaced (Snake Case): Commonly used in Python, each word is separated by an underscore and generally written in lowercase (e.g., example_variable_name).

How to Toggle in IntelliJ

IntelliJ IDEA does not natively provide a direct shortcut or feature to toggle between camel case and underscore spaced. However, you can achieve this using several different approaches:

Using Live Templates

  1. Create a Live Template:
    • Navigate to File > Settings > Editor > Live Templates.
    • Click on + to create a new template and define an abbreviation such as cc2us (camel case to underscore spaced).
    • Enter a Groovy script to transform the text between the two conventions.
  2. Groovy Script Example:
    • For camel case to underscore:
groovy
     def result = _1.replaceAll(/([a-z])([A-Z]+)/, '$1_$2').toLowerCase()
     result
  • For underscore to camel case:
groovy
1     def words = _1.split('_')
2     def result = words.collect { it.capitalize() }.join('')
3     result = result[0].toLowerCase() + result.substring(1)
4     result
  • Now you can use these templates by typing cc2us or us2cc followed by Tab on the selected text.

Using Third-Party Plugins

  1. String Manipulation Plugin:
    • Go to File > Settings > Plugins and search for "String Manipulation".
    • Install the plugin and restart IntelliJ IDEA.
    • Select the text you want to transform.
    • Right-click and choose String Manipulation > Toggle Style to switch between the styles.

Using Custom Code

If you are comfortable with Java or Kotlin, you can develop and use an IntelliJ plugin to automate this process further. This involves understanding IntelliJ's plugin architecture and potentially writing action classes to handle transformations.

Use Cases and Benefits

  • Readability: Depending on the language and team conventions, toggling between these styles can make code more readable and maintainable.
  • Consistency: Automated transformations help maintain consistency across a codebase.
  • Productivity: Reduces manual effort and eliminates the need for a developer to individually rename identifiers across a project.

Key Points Summary

FeatureDescription
Camel CaseexampleVariableName: concatenated with upper-case initials.
Underscore Spacedexample_variable_name: lowercase words separated by _.
Transformation MethodsLive Templates, Third-party Plugins, Custom Code.
ToolsGroovy in Live Templates, String Manipulation Plugin.
Use CasesReadability, consistency in naming, improved productivity.

Conclusion

Although IntelliJ IDEA does not provide an out-of-the-box toggle for camel case and underscore spaced transformations, using live templates, third-party plugins, or custom plugins can effectively accomplish this task. These solutions can significantly improve code consistency and efficiency, allowing developers to focus more on logic and architecture than on code formatting.


Course illustration
Course illustration

All Rights Reserved.