পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Monday, January 30, 2017

D - Contract Programming

Contract programming in D programming is focused on providing a simple and understandable means of error handling.Contract programming in D are implemented by three types of code blocks:
  • body block
  • in block
  • out block

body block

Body block contains the actual functionality code of execution. The in and out blocks are optional while the body block is mandatory. A simple syntax is shown below.
return_type function_name(function_params)
in
{
   // in block
}
out (result)
{
   // in block
}
body
{
   // actual function block
}

in block for pre conditions

In block is for simple pre conditions that verify whether the input parameters are acceptable and in range that can be handled by the code. A benefit of an in block is that all of the entry conditions can be kept together and separate from the actual body of the function. A simple precondition for validating password for its minimum length is shown below.
import std.stdio;
import std.string;

bool isValid(string password)
in
{
   assert(password.length>=5);
}
body
{
   // other conditions
   return true;
}

void main()
{
   writeln(isValid("password"));
}
When the above code is compiled and executed, it reads the file created in previous section and produces the following result:
true

out blocks for post conditions

The out block takes care of the return values from the function. It validates the return value is in expected range. A simple example containing both in and out is shown below that converts months, year to a combined decimal age form.
import std.stdio;
import std.string;

double getAge(double months,double years)
in
{
   assert(months >= 0);
   assert(months <= 12);
}
out (result)
{
   assert(result>=years);
}
body
{
   return years + months/12;
}

void main ()
{
   writeln(getAge(10,12));
}
When the above code is compiled and executed, it reads the file created in previous section and produces the following result:
12.8333

No comments:

Post a Comment