DEV Community

Philip Perry
Philip Perry

Posted on • Originally published at programming-decoded.com on

Moving between programming languages

Recently I’ve been reading the book “Think like a Programmer” by V. Anton Spraul. I decided it would be a good idea to do the coding exercises in all 4 programming languages that I know (PHP, JavaScript, C# and Python) in order to get better at keeping them apart. In many ways, the syntax is very similar, but it’s easy to get tripped up by small differences. You can view the code here: https://github.com/phil4literacy/coding_challenges

Here are some things I learned:

Output:

  • Javascript doesn’t come with a way to console.log new output to the same line, so I needed to add the output to a variable and print after I created the final output. In Python you have to use print(item, end=””) in order for the output to be printed on the same line.
  • Each language uses a different function name for printing out to the terminal (PHP: echo, JavaScript: console.log, C#: Console.Write, Python: print).

For loops:

  • c# needs to have the variable defined (e.g. as string or integer). In Python one uses “range” instead of the counting syntax.

String utilities:

  • Repeating a string is done totally different in all languages. In PHP it can be done with str_repeat(). I like how in Python one can just do print(string * times).

Classes:

  • All 4 programming languages use “class” to declare a class, but methods inside a JavaScript class are not declared using “function”. You simply write the function name.
  • To call a method in JavaScript, C# and Python you use a dot, while in PHP you use an arrow. C#: myHashDrawing.createHalfX(row); PHP: $myHashDrawing->createHalfX($row);
  • Constructors are created using the same name as for the class in C#. In PHP one uses public function __construct(), in JavaScript it’s constructor() and in Python it’s def __init__(self)
  • Python doesn’t use the new keyword for creating an object.

Of course, there are a lot more differences and similarities between the languages and sometimes it can take a bit of time to warm up when switching from another language.

Top comments (1)

Collapse
 
ziczacsg profile image
Vo Dac Bao

thanks Philip for sharing knowleage