0% found this document useful (0 votes)
95 views44 pages

Practice Code

This document provides examples of JSON and XML representations of the same data. It includes several JSON examples of glossary definitions, menu items, and a widget with configuration parameters. For each JSON example, the equivalent XML representation is also shown.

Uploaded by

Damir Galas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views44 pages

Practice Code

This document provides examples of JSON and XML representations of the same data. It includes several JSON examples of glossary definitions, menu items, and a widget with configuration parameters. For each JSON example, the equivalent XML representation is also shown.

Uploaded by

Damir Galas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 44

JSON Example

This page shows examples of messages formatted using JSON (JavaScript Object
Notation).
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup
Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages
such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}

}
}
}
The same text expressed as XML:

<!DOCTYPE glossary PUBLIC "-//OASIS//DTD DocBook V3.1//EN">


<glossary><title>example glossary</title>
<GlossDiv><title>S</title>
<GlossList>
<GlossEntry ID="SGML" SortAs="SGML">
<GlossTerm>Standard Generalized Markup Language</GlossTerm>
<Acronym>SGML</Acronym>
<Abbrev>ISO 8879:1986</Abbrev>
<GlossDef>
<para>A meta-markup language, used to create markup
languages such as DocBook.</para>
<GlossSeeAlso OtherTerm="GML">
<GlossSeeAlso OtherTerm="XML">
</GlossDef>
<GlossSee OtherTerm="markup">
</GlossEntry>
</GlossList>
</GlossDiv>
</glossary>
{"menu": {
"id": "file",

"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
The same text expressed as XML:

<menu id="file" value="File">


<popup>
<menuitem value="New" onclick="CreateNewDoc()" />
<menuitem value="Open" onclick="OpenDoc()" />
<menuitem value="Close" onclick="CloseDoc()" />
</popup>
</menu>
{"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},

"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}}
The same text expressed as XML:

<widget>
<debug>on</debug>
<window title="Sample Konfabulator Widget">
<name>main_window</name>
<width>500</width>
<height>500</height>

</window>
<image src="Images/Sun.png" name="sun1">
<hOffset>250</hOffset>
<vOffset>250</vOffset>
<alignment>center</alignment>
</image>
<text data="Click Here" size="36" style="bold">
<name>text1</name>
<hOffset>250</hOffset>
<vOffset>100</vOffset>
<alignment>center</alignment>
<onMouseUp>
sun1.opacity = (sun1.opacity / 100) * 90;
</onMouseUp>
</text>
</widget>
{"web-app": {
"servlet": [
{
"servlet-name": "cofaxCDS",
"servlet-class": "org.cofax.cds.CDSServlet",
"init-param": {
"configGlossary:installationAt": "Philadelphia, PA",
"configGlossary:adminEmail": "[email protected]",
"configGlossary:poweredBy": "Cofax",
"configGlossary:poweredByIcon": "/images/cofax.gif",

"configGlossary:staticPath": "/content/static",
"templateProcessorClass": "org.cofax.WysiwygTemplate",
"templateLoaderClass": "org.cofax.FilesTemplateLoader",
"templatePath": "templates",
"templateOverridePath": "",
"defaultListTemplate": "listTemplate.htm",
"defaultFileTemplate": "articleTemplate.htm",
"useJSP": false,
"jspListTemplate": "listTemplate.jsp",
"jspFileTemplate": "articleTemplate.jsp",
"cachePackageTagsTrack": 200,
"cachePackageTagsStore": 200,
"cachePackageTagsRefresh": 60,
"cacheTemplatesTrack": 100,
"cacheTemplatesStore": 50,
"cacheTemplatesRefresh": 15,
"cachePagesTrack": 200,
"cachePagesStore": 100,
"cachePagesRefresh": 10,
"cachePagesDirtyRead": 10,
"searchEngineListTemplate": "forSearchEnginesList.htm",
"searchEngineFileTemplate": "forSearchEngines.htm",
"searchEngineRobotsDb": "WEB-INF/robots.db",
"useDataStore": true,
"dataStoreClass": "org.cofax.SqlDataStore",
"redirectionClass": "org.cofax.SqlRedirection",

"dataStoreName": "cofax",
"dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver",
"dataStoreUrl":
"jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon",
"dataStoreUser": "sa",
"dataStorePassword": "dataStoreTestQuery",
"dataStoreTestQuery": "SET NOCOUNT ON;select test='test';",
"dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log",
"dataStoreInitConns": 10,
"dataStoreMaxConns": 100,
"dataStoreConnUsageLimit": 100,
"dataStoreLogLevel": "debug",
"maxUrlLength": 500}},
{
"servlet-name": "cofaxEmail",
"servlet-class": "org.cofax.cds.EmailServlet",
"init-param": {
"mailHost": "mail1",
"mailHostOverride": "mail2"}},
{
"servlet-name": "cofaxAdmin",
"servlet-class": "org.cofax.cds.AdminServlet"},

{
"servlet-name": "fileServlet",
"servlet-class": "org.cofax.cds.FileServlet"},
{

"servlet-name": "cofaxTools",
"servlet-class": "org.cofax.cms.CofaxToolsServlet",
"init-param": {
"templatePath": "toolstemplates/",
"log": 1,
"logLocation": "/usr/local/tomcat/logs/CofaxTools.log",
"logMaxSize": "",
"dataLog": 1,
"dataLogLocation": "/usr/local/tomcat/logs/dataLog.log",
"dataLogMaxSize": "",
"removePageCache": "/content/admin/remove?cache=pages&id=",
"removeTemplateCache": "/content/admin/remove?cache=templates&id=",
"fileTransferFolder": "/usr/local/tomcat/webapps/content/fileTransferFolder",
"lookInContext": 1,
"adminGroupID": 4,
"betaServer": true}}],
"servlet-mapping": {
"cofaxCDS": "/",
"cofaxEmail": "/cofaxutil/aemail/*",
"cofaxAdmin": "/admin/*",
"fileServlet": "/static/*",
"cofaxTools": "/tools/*"},

"taglib": {
"taglib-uri": "cofax.tld",
"taglib-location": "/WEB-INF/tlds/cofax.tld"}}}

The same file expressed as XML:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
<web-app>
<servlet>
<servlet-name>
cofaxCDS
</servlet-name>
<servlet-class>
org.cofax.cds.CDSServlet
</servlet-class>

<init-param>
<param-name>configGlossary:installationAt</param-name>
<param-value>Philadelphia, PA</param-value>
</init-param>
<init-param>
<param-name>configGlossary:adminEmail</param-name>
<param-value>[email protected]</param-value>
</init-param>
<init-param>
<param-name>configGlossary:poweredBy</param-name>

<param-value>Cofax</param-value>
</init-param>
<init-param>
<param-name>configGlossary:poweredByIcon</param-name>
<param-value>/images/cofax.gif</param-value>
</init-param>
<init-param>
<param-name>configGlossary:staticPath</param-name>
<param-value>/content/static</param-value>
</init-param>

<init-param>
<param-name>templateProcessorClass</param-name>
<param-value>org.cofax.WysiwygTemplate</param-value>
</init-param>

<init-param>
<param-name>templateLoaderClass</param-name>
<param-value>org.cofax.FilesTemplateLoader</param-value>
</init-param>

<init-param>
<param-name>templatePath</param-name>
<param-value>templates</param-value>
</init-param>

<init-param>
<param-name>templateOverridePath</param-name>
<param-value></param-value>
</init-param>

<init-param>
<param-name>defaultListTemplate</param-name>
<param-value>listTemplate.htm</param-value>
</init-param>

<init-param>
<param-name>defaultFileTemplate</param-name>
<param-value>articleTemplate.htm</param-value>
</init-param>

<init-param>
<param-name>useJSP</param-name>
<param-value>false</param-value>
</init-param>

<init-param>
<param-name>jspListTemplate</param-name>
<param-value>listTemplate.jsp</param-value>
</init-param>

<init-param>

<param-name>jspFileTemplate</param-name>
<param-value>articleTemplate.jsp</param-value>
</init-param>

<init-param>
<param-name>cachePackageTagsTrack</param-name>
<param-value>200</param-value>
</init-param>

<init-param>
<param-name>cachePackageTagsStore</param-name>
<param-value>200</param-value>
</init-param>

<init-param>
<param-name>cachePackageTagsRefresh</param-name>
<param-value>60</param-value>
</init-param>

<init-param>
<param-name>cacheTemplatesTrack</param-name>
<param-value>100</param-value>
</init-param>

<init-param>
<param-name>cacheTemplatesStore</param-name>

<param-value>50</param-value>
</init-param>

<init-param>
<param-name>cacheTemplatesRefresh</param-name>
<param-value>15</param-value>
</init-param>

<init-param>
<param-name>cachePagesTrack</param-name>
<param-value>200</param-value>
</init-param>

<init-param>
<param-name>cachePagesStore</param-name>
<param-value>100</param-value>
</init-param>

<init-param>
<param-name>cachePagesRefresh</param-name>
<param-value>10</param-value>
</init-param>

<init-param>
<param-name>cachePagesDirtyRead</param-name>
<param-value>10</param-value>

</init-param>

<init-param>
<param-name>searchEngineListTemplate</param-name>
<param-value>forSearchEnginesList.htm</param-value>
</init-param>

<init-param>
<param-name>searchEngineFileTemplate</param-name>
<param-value>forSearchEngines.htm</param-value>
</init-param>

<init-param>
<param-name>searchEngineRobotsDb</param-name>
<param-value>WEB-INF/robots.db</param-value>
</init-param>

<init-param>
<param-name>useDataStore</param-name>
<param-value>true</param-value>
</init-param>

<init-param>
<param-name>dataStoreClass</param-name>
<param-value>org.cofax.SqlDataStore</param-value>
</init-param>

<init-param>
<param-name>redirectionClass</param-name>
<param-value>org.cofax.SqlRedirection</param-value>
</init-param>
<init-param>
<param-name>dataStoreName</param-name>
<param-value>cofax</param-value>
</init-param>

<init-param>
<param-name>dataStoreDriver</param-name>
<param-value>com.microsoft.jdbc.sqlserver.SQLServerDriver</param-value>
</init-param>

<init-param>
<param-name>dataStoreUrl</param-name>
<paramvalue>jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon</paramvalue>
</init-param>

<init-param>
<param-name>dataStoreUser</param-name>
<param-value>sa</param-value>
</init-param>

<init-param>
<param-name>dataStorePassword</param-name>
<param-value></param-value>
</init-param>

<init-param>
<param-name>dataStoreTestQuery</param-name>
<param-value>SET NOCOUNT ON;select test='test';</param-value>
</init-param>

<init-param>
<param-name>dataStoreLogFile</param-name>
<param-value>/usr/local/tomcat/logs/datastore.log</param-value>
</init-param>

<init-param>
<param-name>dataStoreInitConns</param-name>
<param-value>10</param-value>
</init-param>

<init-param>
<param-name>dataStoreMaxConns</param-name>
<param-value>100</param-value>
</init-param>

<init-param>

<param-name>dataStoreConnUsageLimit</param-name>
<param-value>100</param-value>
</init-param>
<init-param>
<param-name>dataStoreLogLevel</param-name>
<param-value>debug</param-value>
</init-param>

<init-param>
<param-name>maxUrlLength</param-name>
<param-value>500</param-value>
</init-param>

</servlet>

<servlet>
<servlet-name>
cofaxEmail
</servlet-name>
<servlet-class>
org.cofax.cds.EmailServlet
</servlet-class>

<init-param>
<param-name>mailHost</param-name>
<param-value>mail1</param-value>

</init-param>

<init-param>
<param-name>mailHostOverride</param-name>
<param-value>mail2</param-value>
</init-param>
</servlet>

<servlet>
<servlet-name>
cofaxAdmin
</servlet-name>
<servlet-class>
org.cofax.cds.AdminServlet
</servlet-class>
</servlet>

<servlet>
<servlet-name>
fileServlet
</servlet-name>
<servlet-class>
org.cofax.cds.FileServlet
</servlet-class>
</servlet>

<servlet>
<servlet-name>
cofaxTools
</servlet-name>
<servlet-class>
org.cofax.cms.CofaxToolsServlet
</servlet-class>

<init-param>
<param-name>templatePath</param-name>
<param-value>toolstemplates/</param-value>
</init-param>

<init-param>
<param-name>log</param-name>
<param-value>1</param-value>
</init-param>

<init-param>
<param-name>logLocation</param-name>
<param-value>/usr/local/tomcat/logs/CofaxTools.log</param-value>
</init-param>

<init-param>
<param-name>logMaxSize</param-name>
<param-value></param-value>

</init-param>

<init-param>
<param-name>dataLog</param-name>
<param-value>1</param-value>
</init-param>

<init-param>
<param-name>dataLogLocation</param-name>
<param-value>/usr/local/tomcat/logs/dataLog.log</param-value>
</init-param>

<init-param>
<param-name>dataLogMaxSize</param-name>
<param-value></param-value>
</init-param>

<init-param>
<param-name>removePageCache</param-name>
<param-value>/content/admin/remove?cache=pages&id=</param-value>
</init-param>

<init-param>
<param-name>removeTemplateCache</param-name>
<param-value>/content/admin/remove?cache=templates&id=</paramvalue>
</init-param>

<init-param>
<param-name>fileTransferFolder</param-name>
<param-value>/usr/local/tomcat/webapps/content/fileTransferFolder</paramvalue>
</init-param>

<init-param>
<param-name>lookInContext</param-name>
<param-value>1</param-value>
</init-param>

<init-param>
<param-name>adminGroupID</param-name>
<param-value>4</param-value>
</init-param>

<init-param>
<param-name>betaServer</param-name>
<param-value>true</param-value>
</init-param>

</servlet>

<servlet-mapping>
<servlet-name>
cofaxCDS

</servlet-name>
<url-pattern>
/
</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>
cofaxEmail
</servlet-name>
<url-pattern>
/cofaxutil/aemail/*
</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>
cofaxAdmin
</servlet-name>
<url-pattern>
/admin/*
</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>

fileServlet
</servlet-name>
<url-pattern>
/static/*
</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>
cofaxTools
</servlet-name>
<url-pattern>
/tools/*
</url-pattern>
</servlet-mapping>

<taglib>
<taglib-uri>cofax.tld</taglib-uri>
<taglib-location>/WEB-INF/tlds/cofax.tld</taglib-location>
</taglib>

</web-app>
The action and label values only need to be provided if they are not the same as the
id.

{"menu": {
"header": "SVG Viewer",

"items": [
{"id": "Open"},
{"id": "OpenNew", "label": "Open New"},
null,
{"id": "ZoomIn", "label": "Zoom In"},
{"id": "ZoomOut", "label": "Zoom Out"},
{"id": "OriginalView", "label": "Original View"},
null,
{"id": "Quality"},
{"id": "Pause"},
{"id": "Mute"},
null,
{"id": "Find", "label": "Find..."},
{"id": "FindAgain", "label": "Find Again"},
{"id": "Copy"},
{"id": "CopyAgain", "label": "Copy Again"},
{"id": "CopySVG", "label": "Copy SVG"},
{"id": "ViewSVG", "label": "View SVG"},
{"id": "ViewSource", "label": "View Source"},
{"id": "SaveAs", "label": "Save As"},
null,
{"id": "Help"},
{"id": "About", "label": "About Adobe CVG Viewer..."}
]
}}
The same message expressed as XML:

<menu>
<header>Adobe SVG Viewer</header>
<item action="Open" id="Open">Open</item>
<item action="OpenNew" id="OpenNew">Open New</item>
<separator/>
<item action="ZoomIn" id="ZoomIn">Zoom In</item>
<item action="ZoomOut" id="ZoomOut">Zoom Out</item>
<item action="OriginalView" id="OriginalView">Original View</item>
<separator/>
<item action="Quality" id="Quality">Quality</item>
<item action="Pause" id="Pause">Pause</item>
<item action="Mute" id="Mute">Mute</item>
<separator/>
<item action="Find" id="Find">Find...</item>
<item action="FindAgain" id="FindAgain">Find Again</item>
<item action="Copy" id="Copy">Copy</item>
<item action="CopyAgain" id="CopyAgain">Copy Again</item>
<item action="CopySVG" id="CopySVG">Copy SVG</item>
<item action="ViewSVG" id="ViewSVG">View SVG</item>
<item action="ViewSource" id="ViewSource">View Source</item>
<item action="SaveAs" id="SaveAs">Save As</item>
<separator/>
<item action="Help" id="Help">Help</item>
<item action="About" id="About">About Adobe CVG Viewer...</item>
</menu>

using System;
using System.IO;
using System.Security.Cryptography;
using System.Net.Sockets;

public class main


{
public static void Main(string[] args)
{
try
{
//Create a TCP connection to a listening TCP process.
//Use "localhost" to specify the current computer or
//replace "localhost" with the IP address of the
//listening process.
TcpClient TCP = new TcpClient("localhost",11000);

//Create a network stream from the TCP connection.


NetworkStream NetStream = TCP.GetStream();

//Create a new instance of the RijndaelManaged class


// and encrypt the stream.
RijndaelManaged RMCrypto = new RijndaelManaged();

byte[] Key = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
0x11, 0x12, 0x13, 0x14, 0x15, 0x16};

byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
0x11, 0x12, 0x13, 0x14, 0x15, 0x16};

//Create a CryptoStream, pass it the NetworkStream, and encrypt


//it with the Rijndael class.
CryptoStream CryptStream = new CryptoStream(NetStream,
RMCrypto.CreateEncryptor(Key, IV),
CryptoStreamMode.Write);

//Create a StreamWriter for easy writing to the


//network stream.
StreamWriter SWriter = new StreamWriter(CryptStream);

//Write to the stream.


SWriter.WriteLine("Hello World!");

//Inform the user that the message was written


//to the stream.
Console.WriteLine("The message was sent.");

//Close all the connections.


SWriter.Close();
CryptStream.Close();
NetStream.Close();
TCP.Close();
}
catch

{
//Inform the user that an exception was raised.
Console.WriteLine("The connection failed.");
}
}
}

class ExceptionTest
{
static double SafeDivision(double x, double y)
{
if (y == 0)
throw new System.DivideByZeroException();
return x / y;
}
static void Main()
{
// Input for test purposes. Change the values to see
// exception handling behavior.
double a = 98, b = 0;
double result = 0;

try
{
result = SafeDivision(a, b);
Console.WriteLine("{0} divided by {1} = {2}", a, b, result);

}
catch (DivideByZeroException e)
{
Console.WriteLine("Attempted divide by zero.");
}
}
}

int caseSwitch = 1;
switch (caseSwitch)
{
case 1:
Console.WriteLine("Case 1");
break;
case 2:
Console.WriteLine("Case 2");
break;
default:
Console.WriteLine("Default case");
break;
}

public void Test()


{
for (int i = 1; i < 101; i++)
{

if (i % 3 == 0 && i % 5 == 0)
{
Console.WriteLine("FizzBuzz");
}
else if (i % 3 == 0)
{
Console.WriteLine("Fizz");
}
else if (i % 5 == 0)
{
Console.WriteLine("Buzz");
}
else
{
Console.WriteLine(i);
}
}
}
What do you think? Do we need to make the code better?

Example 2:

Hide Copy Code


public void Check()
{
for (int i = 1; i <= 100; i++)

{
string output = "";
if (i % 3 == 0) { output = "Fizz"; }
if (i % 5 == 0) { output = output + "Buzz"; }
if (output == "") { output = i.ToString(); }
Console.WriteLine(output);
}
}
What do you think now ? Do we need to make the code even better?

Ok, let me help to make it better. Naming thing is one of the hardest job we have as
a software developer. Because we spend a lot of time naming things, there are so
many things to name properties, methods, classes, files, projects etc. We should
spend some energies for naming things because names can be meanings. Adding
meaning to code is readability all about.

Hide Copy Code


public void DoFizzBuzz()
{
for (int number = 1; number <= 100; number++)
{
var output = GetFizzBuzzOutput(number);
Console.WriteLine(output);
}
}

private static string GetFizzBuzzOutput(int number)


{

string output = string.Empty;


if (number%3 == 0)
{
output = "Fizz";
}
if (number%5 == 0)
{
output += "Buzz";
}
if(string.IsNullOrEmpty(output))
{
output = number.ToString();
}
return output;
}
ublic class Product
{
public void GetActiveProducts()
{
//...
}
public void CalculateProductAdditinalCost()
{
//...
}
}

Do use camelCasing for method arguments and local variables.

Hide Copy Code


public class ProductCategory
{
public void Save(ProductCategory productCategory)
{
// ...
}
}
Do not use Abbreviations.

Hide Copy Code


// Correct
ProductCategory productCategory;

// Avoid
ProductCategory prodCat;
Do not use Underscores in identifiers.

Hide Copy Code


// Correct
ProductCategory productCategory;

// Avoid
ProductCategory product_Category;

Do prefix interfaces with the letter I.

Hide Copy Code


public interface IAddress
{

}
Do declare all member variables at the top of a class, with static variables at the
very top.

Hide Copy Code


public class Product
{
public static string BrandName;

public string Name{get; set;}


public DateTime DateAvailable {get; set;}

public Product()
{
// ...
}
}
Do use singular names for enums. Exception: bit field enums.

Hide Copy Code


public enum Direction

{
North,
East,
South,
West
}
Do not suffix enum names with Enum.

Hide Copy Code


//Avoid
public enum DirectionEnum
{
North,
East,
South,
West
}

//avoid
public void Checkout(string shippingName, string shippingCity,
string shippingSate, string shippingZip, string billingName,
string billingCity, string billingSate, string billingZip)
{

}
Hide Copy Code

//DO
public void Checkout(ShippingAddress shippingAddress,BillingAddress
billingAddress)
{
}
We should introduce class instead of all parameters.

Avoid Complex Expressions

Hide Copy Code


if(product.Price>500 && !product.IsDeleted &&
!product.IsFeatured && product.IsExported)
{
// do something
}

Hide Copy Code


//avoid
if(product.Price>15)
{
return false;
}
else if(product.IsDeleted)
{
return false;
}
else if(!product.IsFeatured)

{
return false;
}
else if()
{
//.....
}
return true;
Hide Copy Code
//DO
var isValid = true;
if(product.Price>15)
{
isValid= false;
}
else if(product.IsDeleted)
{
isValid= false;
}
else if(!product.IsFeatured)
{
isValid= false;
}
return isValid;
You can imagine 4 exit points scattered around 20- 30 lines of code. That makes you
more harder to understand and see what is happening inside the method and what
will execute and when will execute.

I got too many responses, some agreeing but mostly disagreeing that it was a good
"standard" to enforce. To find out the potentiality I did some unit testing and found
that for complex method that have multiple exit points usually have a set of tests
for each of those paths.

Hide Copy Code


if( BADFunction() == true)
{
// expression
if( anotherFunction() == true )
{
// expression
return true;
}
else
{
//error
}
}
else
{
//error
}
return false;
Hide Copy Code
if( !GoodFunction())

{
// error.
return false
}
// expression
if( !GoodFunction2())
{
//error.
return false;
}
// more expression
return true;

namespace WebApplication1.Controllers
{
public class Customer
{
public string CustomerName { get; set; }
public string Address { get; set; }
}
public class TestController : Controller
{
public Customer GetCustomer()
{
Customer c = new Customer();
c.CustomerName = "Customer 1";

c.Address = "Address1";
return c;
}
}
}

public override string ToString()


{
return this.CustomerName+"|"+this.Address;
}

@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>MyView</title>
</head>
<body>

public ActionResult GetView()


{
if(Some_Condition_Is_Matching)
{

return View("MyView");
}
else
{
return View("YourView");
}
}

public ActionResult GetView()


{
if(Some_Condition_Is_Matching)
{
return View("MyView");
}
else
{
return Content("Hi Welcome");
}
}

public class Customer


{
private string _Code;
private int _Id;
private double _Amount;

public string CustomerCode


{
set
{
_Code = value;
}
get
{
return _Code;
}
}

public int Id
{
get
{
return _Id;
}
set
{
_Id= value;
}
}

public double Amount


{

set
{
_Amount = value;
}
get
{
return _Amount;
}
}
}
public class CustomerController : Controller
{
&hellip;..
&hellip;.
public ViewResult DisplayCustomer()
{
Customer objCustomer = new Customer();
objCustomer.Id = 12;
objCustomer.CustomerCode = "1001";
objCustomer.Amount = 90.34;

return View("DisplayCustomer",objCustomer);
}
}

public class CustomerController : Controller

{
&hellip;..
&hellip;.
public ViewResult DisplayCustomer()
{
Customer objCustomer = new Customer();
objCustomer.Id = 12;
objCustomer.CustomerCode = "1001";
objCustomer.Amount = 90.34;

return View("DisplayCustomer",objCustomer);
}
}

<form action="DisplayCustomer" method="post">


Enter customer id :- <input type="text" name="Id" /> <br />
Enter customer code :- <input type="text" name="CustomerCode" /><br />
Enter customer Amount :-<input type="text" name="Amount" /><br />
<input type="submit" value="Submit customer data" />
</form>

You might also like