bmen-jekyll/_posts/2016-07-23-a-better-hello-w...

52 lines
1.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
layout: post
title: A better Hello World
tags: [Software Development]
style: fill
color: dark
description: A different “hello world” to demonstrate language features better.
---
Source: [RICO STA. CRUZ](https://ricostacruz.com/til/a-better-hello-world)
The “Hello world” program is usually the first introduction to any programming language. It looks like this in the C programming language:
```c
/* hello.c */
#import <stdio.h>
int main(int argc, char *argv[]) {
printf("Hello, world!");
return 0;
}
```
It demonstrates the minimum amount you need to write a C program. In more modern languages however, this example isnt as useful anymore. Heres the same example in Python:
```python
# hello.py
print "Hello, world!"
```
## A better hello world
In todays world of more succint programming languages, we need a different “hello world” to demonstrate language features better. Heres what I propose:
```
// hello.js
function getGreeting (name) {
return `Hello, ${name}!`
}
const message = getGreeting('world')
console.log(message)
```
This simple example demonstrates a few more things than printing strings:
- How to write a function with an argument
- Returning values from functions
- How to use variables
- The naming convention for functions (camelCase versus snake_case)
- String concatenation
- Comments