This is an extremely grey area that honestly comes down to the patent laws of your country, the uniqueness of the code you're reusing or porting, and your intended use for that code.
This is where it becomes important to use open licenses on public projects, as it lets people know "It's okay to use this code" instead of simply putting the source out there and leaving it's intellectual ownership as an ambiguous question.
In my opinion, there is nothing wrong with using code from others' projects or works as long as you're educating yourself or, if used commercially, your use of said code is solving a different problem than the project you took it from. This is just an opinion with no real consideration given towards legal implications.
This is where it becomes important to use open licenses on public projects, as it lets people know "It's okay to use this code" instead of simply putting the source out there and leaving it's intellectual ownership as an ambiguous question.
In my opinion, there is nothing wrong with using code from others' projects or works as long as you're educating yourself or, if used commercially, your use of said code is solving a different problem than the project you took it from. This is just an opinion with no real consideration given towards legal implications.