GithubHelp home page GithubHelp logo

leticia-maria / mathcase Goto Github PK

View Code? Open in Web Editor NEW
7.0 1.0 0.0 5 KB

This is a repository for MathCase notation for naming variables taken from math equations.

Home Page: https://github.com/Leticia-maria/MathCase

License: MIT License

best-practices naming-conventions naming-pattern naming-schemes

mathcase's Introduction

MathCase

A new convention for naming variables taken from math formulas.

Tips for Creating Good Names for Variables

Since names are the smallest building block of code, they should follow some rules to be good.

  • Use intention-revealing names
  • Avoid disinformation
  • Make meaningful distinctions
  • Use pronounceable names
  • Use searchable names
  • Avoid encondings
  • Avoid mental mapping

But, when we are talking about math formulas, some of these rules are difficult to follow. For example, take a look at this code:

for i in range(n):
    for j in range(m):
        for k in range(l):
            temp_value = X[i][j][k] * 12.5
            new_array[i][j][k] = temp_value + 150

If you were trying to modify or debug this code, you'd be at a loss unless you could read the author's mind. Even if you were the author, a few days after writing this code you might not remember what it does because of the unhelpful variable names and use of magic numbers.

Looking at scientific programming codes, it is often to see examples like above (or worse): code with variable names such as X, y, xs, x1, x2, tp, tm, cif, reg, xi, yi, ii and numerous unnamed constant values.

What Makes a Bad Variable Name?

Summarizing that, most problems with naming variables stem from:

  • A desire to keep variable names short
  • A direct translation of formulas into code

On this first point, while languages like Fortran did limit the length of variable names (to six characters), modern programming languages have no restrictions so don't feel forced to use contrived abbreviations. But it is not recommended to use long variable names (always thinking about readability).

Let's see an example that makes both mistakes. Say we have a polynomial equation for finding the price of a house from a model. You may be tempted to write the mathematical formula directly in code.

y = m_1 * x_1 + m_2 * x_2^2 + b

temp = m1 * x1 + m2 * (x2 ** 2)
final = temp + b

This code looks like it was written by a machine for a machine. While a computer will ultimately run your code, it'll be read by humans, so write code intended for humans. To do this, we need to think not about the formula itself (the how) and consider the real-world objects being modeled (the what). Then, rewriting the last example:

house_price = price_per_room * rooms + \ price_per_floor_squared  * (floors ** 2) 
house_price = house_price + expected_mean_house_price

mathcase's People

Contributors

leticia-maria avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

mathcase's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.