How to Code


Yanyan Jiang

Overview

Life is short.

  • Use command line.
  • Use Git.
  • Use Python.

Life is Short. Use Command Line.

Why Use Command Line?

Shell is a Turing-Complete programming language.

That gives you sufficient expressiveness for daily work

  • UNIX Philosophy: keep it simple, stupid
  • everything is a file; write things to work together using text interface

How to Use Command Line?

UNIX is User-Friendly...

It's just choosy about who its friends are!


Modernize your command line!

Other Tips

Learn a lot of programming languages

  • shell
  • vim (vim keybinding is everywhere!)
  • modern regular expressions
  • ...

Use modern variants

  • use modern tools like tldr, fzf, rg, ...
  • use Markdown (and a lot of tools)
  • ...

Short Summary

Ask yourself: did you know (almost all) these before?

  • Yes → you're on the right track
  • No → rethink your life!

Life is Short. Use Git.

Why Use Git?

What happens if you rush into my office and smash my computer?

  • Nothing
  • I always push to remote when I leave my office!
    • $>2$ up-to-date replicas
    • more (slightly) outdated replicas

What is Git?

I asked this question on postgraduate interview (all “top” students).

I frequently got “something for downloading code from Internet”.


Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. (git-scm.com)

What is Git?

A simplified model

  • A lot of directory snapshots (commits)
    • saved in .git folder
  • Each commit is associated with a commit message
  • Creating new commits by
    1. changing an existing commit
    2. merging two existing commits
    3. synchronizing with remote commits

How to Use Git?

For small projects (only you and your supervisor)

  1. Never commit generated files (properly ignore them)
  2. Commit early, commit often (with meaningful messages)
  3. Make good use of branches
  4. Stash incomplete work

Further Reading

Life is Short. Use Python.


(You may have your own preferences: Javascript, Haskell, ...)

Why Use Python?

Sufficiently simple and powerful for daily tasks

  • borrow meeting rooms
  • send spam mails...
def send(name, email, subject, md_text):
    html, msg = markdown.markdown(md_text), EmailMessage()
    msg['Subject'] = subject
    msg['From'] = Address('Yanyan Jiang', *MAIL.split('@'))
    msg['To'] = Address(name, *email.split('@'))
    msg.set_content(html, subtype='html')
    server.send_message(msg)
server = smtplib.SMTP_SSL('smtp.exmail.qq.com', 465) 
server.login(MAIL, PASSWORD) # import getpass
for row in list(csv.reader(open('students.csv'))):
    id, name, token = row
    send(name, f'{id}@smail.nju.edu.cn', TITLE, TEMPLATE.format(*row))
server.quit()

Use Modern Features

Useful features

  • 3.3: yield from xs, venv
  • 3.4: asyncio, enum, pathlib
  • 3.5: async/await, typing
  • 3.6: f'{a}+{b}={a+b}', [i async for i in aiter() if i % 2]
  • 3.7: @dataclass
  • 3.8: (x := read()) + 1, f'{x=}'
  • 3.9: s.removesuffix(), val: list[int] = [], graphlib
  • 3.10, prerelease
    • with (A() as a, B() as b)
    • match/case
    • def f(x: int | float) -> int | float:

Summary

How to Code

Life is short.

  • Use command line.
  • Use Git.
  • Use Python.

End.