Is it harmful to motors that manipulating the motor in this way?

firstly,the motor rotates clockwise such as digitalwrite(i1, HIGH). then the motor rotates counter-clockwise such as digitalwrite(i1,LOW). there is not any delay during the rotating. is it harmful to the motor?

A DC motor could be damaged. Speed would be a contributing factor.
Think of a car going from forward to reverse.

Stepping motors can do this, but it is a completely different beast.

You can easily mangle a gear box this way. Turn the motor power off or down for a brief period before reversing.

You can arrange that the power supply is weak enough not to damage the mechanics - basically the current
limits the torque, and its the torque that will overload the mechanical side of things.

Where you need to worry about this possibility the simplest solution is to limit the acceleration in your motor
driver code (or rate of change of PWM, which is what controls the speed).

Most motors are mechanically OK to take full stall braking, its usually the gears and the load that aren't
robust enough. However the larger the motor the more important the issue is as the forces are higher
and the design margins less.

Shorting the terminals of a DC motor at full speed is not recommended as this is the worst
possible overload - never underestimate the power of electromagnetic braking.