Giter Site home page Giter Site logo

geometrylibtask's Introduction

Test Task using Source Generators

Generates AreaUtils class which provides helpers to calculate area of geometry shapes defined in project. It allows to:

  • Easely add new shapes and then automaticaly area helpers for new shapes will be generated
  • Avoid heap allocation when only need to calculate area

Generating alghorithm:

  1. Find classes derived from base class Shape
  2. Get all properties used in GetArea method
  3. Get constructor which initialize all this properties
  4. Get constructor parameters which wil be user as htlper method parameters
  5. Get all statements from constructor (validation & assignment)
  6. Get method body depending is it block or lambda
  7. Build helper method using pattern:
public static double Get{*className*}Area({constructor parameters})
{
  { all statements from constructor }
  { GetArea method body }
}

Generated file looks like this:

using GeometryLib.Helpers;

namespace AreaLib.Utils
{
    public static class AreaUtils
    {
        public static double GetCircleArea(double radius)
        {
            var Radius = Guard.AgainstNegativeOrZero(radius); 
            return Math.PI * Math.Pow(Radius, 2);
        }
        public static double GetTriangleArea(double a, double b, double c)
        {
            var A = Guard.AgainstNegativeOrZero(a);
            var B = Guard.AgainstNegativeOrZero(b);
            var C = Guard.AgainstNegativeOrZero(c);
            Guard.AgainstIncorrectTriangleSides(a, b, c); 
            double p = (A + B + C) / 2;
            return Math.Sqrt(p * (p - A) * (p - B) * (p - C));
        }
    }
}

geometrylibtask's People

Watchers

 avatar

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.