Coding Convention Conversion

Jolf, 35 bytes

Saves 1 byte thanks to @Cᴏɴᴏʀ O'Bʀɪᴇɴ. This is encoded in ISO 8859-7.

? hI'_ΜGI'_dpyH0pxRGIL0"(?=[A-Z])'_

Woohoo my first Jolf program!

Explanation

   // I = input
? hI'_                              // If input contains _
       GI'_                          // Split on _
      Μ    d                         // Loop, then join
            pyH0                     // Make the first character uppercase
                                    // ELSE...
                  RGIL0"(?=[A-Z])    // Split *after* all uppercase chars
                                 '_  // join with _ 
                px                   //Make lowercase

Try it online


Retina, 37

Thanks to @ MartinBüttner for saving 4 bytes!

^|[A-Z]
_$0
T`Ll`lL`_.
^_|_(?=[A-Z])

(Note the trailing newline.)

Try it online. Note this includes extra m` to configure a couple of lines to treat each input line separately so all testcases may be run in one go. This is not a requirement of the question, so these are not counted in the score.

  • Lines 1 and 2 insert _ either at the beginning of input or before uppercase letters. All words are now _-separated, regardless of case.
  • Line 3 swaps case of the first letter in each word.
  • Lines 4 and 5 remove _ either at the start of input, or when followed by an uppercase letter.

GNU Sed, 46

Thanks to @TobySpeight for saving 2 bytes!

Score includes +1 for -E (or -r) option to sed.

s/(^|_)([a-z])/\u\2/g
t
s/[A-Z]/_\l&/g
s/^_//

Try it online.

Fairly straightforward sed:

  • Line 1 substitutes beginning of line or _, followed by a lowercase letter with the upper case of that letter. The g flag to s performs this substitution for each instance found
  • t jumps to the : unnamed label if there were any matches for the above substitution. This label is implicitly at the end.
  • Otherwise all uppercase letters are substituted with _ the lower case of that letter
  • This leaves a leading _ before the first letter. s/^_// removes that.