回答編集履歴

6 modify

退会済みユーザー

退会済みユーザー

2018/04/17 21:11  投稿

You should use 'guard' solution. That is the way to reach the world of freaking geeks!
```Javascript
/*************************************************************
*
* main process
*
*************************************************************/
$(elem).each(function()
{
   const CASE = {'case1' : 1, 'case2' : 2, 'case_unmatch': 0};
   
   var aaa = "moji", bbb = "moji",
       aa = true, bb = true,
       a  = 1, b = 1, c = 1, d = 1, e = 1;
   
   try
   {
       // When aa isn't true AND aaa isn't much "hoge", throw 'Illegal Case'
       if(!isRegular(aa, aaa)) { throw 'Illegal Case'; }
       // get a CASE CODE depending on conditions
       switch(getCase(CASE, a, b))
       {
           // When (a >= 0 && b >= 0) or (a <= 0 && b <= 0)
           case CASE.case1:
               //処理1
               break;
           // When (a <= 0 && b >= 0)
           case CASE.case2:
               // When bb isn't true OR b > c, exit this scope at once.
               if (!canProcess2(bb, b, c)){ break; }
               // Information!
               // subProcess1(): create A and sum up total with c and d, then alert "Hello!"
               // subProcess2(): delete an element #input and submit all inputs to server.
               (bbb.match("fuga"))? subProcess1(e) : subProcess2(e);
               break;
           // other case as Unexpected conditions.
           default:
               throw 'Unknown Case';
       }
   }
   catch(e)
   {
       // UnExpected processing.
       
       // console.log("An error has occurred. " + e);
       // return false;   // to exit jQuery.each() loop.
   }
   
   return true;
});
/*************************************************************
*
* functions
*
*************************************************************/
/*
* return true when the condition is satisfied.
*/
function isRegular(aa, aaa)
{
   if(aa)                 { return true; }
   if(aaa.match("hoge"))  { return true; }
   return false;
}
/*
* return CASE CODE depending on conditions.
*/
function getCase(CASE, a, b)
{
   if(a >= 0 && b >= 0)   { return CASE.case1; }
   if(a <= 0 && b <= 0)   { return CASE.case1; }
   if(a <= 0 && b >= 0)   { return CASE.case2; }
   return CASE.case_unmatch;
}
/*
* When bb isn true AND b <= c, return true. otherwise return false
* When bb is true AND b <= c, return true. otherwise return false
*/
function canProcess2(bb, b, c)
{
   if(bb && b <= c)   { return true; }
   
   return false;
}
/*
* create A and sum up total with c and d, then alert "Hello!"
*/
function subProcess1(e)
{
   //処理3
   
   if (e % 2 != 0) { return; }
   //処理4
}
/*
* delete an element #input and submit all inputs to server.
*/
function subProcess2(e)
{
   //処理1 ← Duplicated?
   
   if (e % 2 != 0) { return; }
   
   //処理2
}
```
5 modify

退会済みユーザー

退会済みユーザー

2018/04/17 15:19  投稿

You should use 'guard' solution. That is the way to reach the world of freaking geeks!
```Javascript
/*************************************************************
*
* main process
*
*************************************************************/
$(elem).each(function()
{
   const CASE = {'case1' : 1, 'case2' : 2, 'case_unmatch': 0};
   
   var aaa = "moji", bbb = "moji",
       aa = true, bb = true,
       a  = 1, b = 1, c = 1, d = 1, e = 1;
   
   try
   {
       // When aa isn't true AND aaa isn't much "hoge", throw 'Illegal Case'
       if(!isRegular(aa, aaa)) { throw 'Illegal Case'; }
       // get a CASE CODE depending on conditions
       switch(getCase(CASE, a, b))
       {
           // When (a >= 0 && b >= 0) or (a <= 0 && b <= 0)
           case CASE.case1:
               //処理1
               break;
           // When (a <= 0 && b >= 0)
           case CASE.case2:
               // When bb isn't true OR b > c, exit this scope at once.
               if (!canProcess2(bb, b, c)){ break; }
               // Information!
               // subProcess1(): create A and sum up total with c and d, then alert "Hello!"
               // subProcess2(): delete an element #input and submit all inputs to server.
               (bbb.match("fuga"))? subProcess1(e) : subProcess2(e);
               break;
           // other case as Unexpected conditions.
           default:
               throw 'Unknown Case';
       }
   }
   catch(e)
   {
       // UnExpected processing.
       
       // console.log("Un error has occurred. " + e);
       // console.log("An error has occurred. " + e);
       // return false;   // to exit jQuery.each() loop.
   }
   
   return true;
});
/*************************************************************
*
* functions
*
*************************************************************/
/*
* return true when the condition is satisfied.
*/
function isRegular(aa, aaa)
{
   if(aa)                 { return true; }
   if(aaa.match("hoge"))  { return true; }
   return false;
}
/*
* return CASE CODE depending on conditions.
*/
function getCase(CASE, a, b)
{
   if(a >= 0 && b >= 0)   { return CASE.case1; }
   if(a <= 0 && b <= 0)   { return CASE.case1; }
   if(a <= 0 && b >= 0)   { return CASE.case2; }
   return CASE.case_unmatch;
}
/*
* When bb isn true AND b <= c, return true. otherwise return false
*/
function canProcess2(bb, b, c)
{
   if(bb && b <= c)   { return true; }
   
   return false;
}
/*
* create A and sum up total with c and d, then alert "Hello!"
*/
function subProcess1(e)
{
   //処理3
   
   if (e % 2 != 0) { return; }
   //処理4
}
/*
* delete an element #input and submit all inputs to server.
*/
function subProcess2(e)
{
   //処理1 ← Duplicated?
   
   if (e % 2 != 0) { return; }
   
   //処理2
}
```
4 modify

退会済みユーザー

退会済みユーザー

2018/04/17 13:07  投稿

You should use 'guard' solution. That is the way to reach the world of freaking geeks!
```Javascript
/*************************************************************
*
* main process
*
*************************************************************/
$(elem).each(function()
{
   const CASE = {'case1' : 1, 'case2' : 2, 'case_unmatch': 0};
   
   var aaa = "moji", bbb = "moji",
       aa = true, bb = true,
       a  = 1, b = 1, c = 1, d = 1, e = 1;
   
   try
   {
       // When aa isn't true AND aaa isn't much "hoge", throw 'Illegal Case'
       if(!isRegular(aa, aaa)) { throw 'Illegal Case'; }
       // get a CASE CODE depending on conditions
       switch(getCase(CASE, a, b))
       {
           // When (a >= 0 && b >= 0) or (a <= 0 && b <= 0)
           case CASE.case1:
               //処理1
               break;
           // When (a <= 0 && b >= 0)
           case CASE.case2:
               // When bb isn't true OR b > c, exit this scope at once.
               if (!canProcess2(bb, b, c)){ break; }
               // Information!
               // subProcess1(): create A and sum up total with c and d, then alert "Hello!"
               // subProcess2(): delete an element #input and submit all inputs to server.
               (bbb.match("fuga"))? subProcess1(e) : subProcess2(e);
               break;
           // other case as Unexpected conditions.  
           default:
               throw 'Unknown Case';
       }
   }
   catch(e)
   {
       // UnExpected processing.
       
       // console.log("Un error has occurred. " + e);
       // return false;   // to exit jQuery.each() loop.
   }
   
   return true;
});
/*************************************************************
*
* functions
*
*************************************************************/
/*
* return true when the condition is satisfied.
*/
function isRegular(aa, aaa)
{
   if(aa)                 { return true; }
   if(aaa.match("hoge"))  { return true; }
   return false;
}
/*
* return CASE CODE depending on conditions.
*/
function getCase(CASE, a, b)
{
   if(a >= 0 && b >= 0)   { return CASE.case1; }
   if(a <= 0 && b <= 0)   { return CASE.case1; }
   if(a <= 0 && b >= 0)   { return CASE.case2; }
   return CASE.case_unmatch;
}
/*
* When bb isn true AND b <= c, return true. otherwise return false
*/
function canProcess2(bb, b, c)
{
   if(bb && b <= c)   { return true; }
   
   return false;
}
/*
* create A and sum up total with c and d, then alert "Hello!"
*/
function subProcess1(e)
{
   //処理3
   
   if (e % 2 != 0) { return; }
   //処理4
}
/*
* delete an element #input and submit all inputs to server.
*/
function subProcess2(e)
{
   //処理1 ← Duplicated?
   
   if (e % 2 != 0) { return; }
   
   //処理2
}
```
3 modify

退会済みユーザー

退会済みユーザー

2018/04/17 13:04  投稿

You should use 'guard' solution. That is the way to reach the world of freaking geeks!
```Javascript
/*************************************************************
*
* main process
*
*************************************************************/
$(elem).each(function()
{
   const CASE = {'case1' : 1, 'case2' : 2, 'case_unmatch': 0};
   
   var aaa = "moji", bbb = "moji",
       aa = true, bb = true,
       a  = 1, b = 1, c = 1, d = 1, e = 1;
   
   try
   {
       // When aa isn't true and aaa isn't much "hoge", throw 'Illegal Case'
       // When aa isn't true AND aaa isn't much "hoge", throw 'Illegal Case'
       if(!isRegular(aa, aaa)) { throw 'Illegal Case'; }
       // get a CASE CODE depending on conditions
       switch(getCase(CASE, a, b))
       {
           // When (a >= 0 && b >= 0) or (a <= 0 && b <= 0)
           case CASE.case1:
               //処理1
               break;
           // When (a <= 0 && b >= 0)
           case CASE.case2:
               // When bb isn't true OR b > c, exit this scope at once.
               if (!canProcess2(bb, b, c)){ break; }
               // Information!
               // subProcess1(): create A and sum up total with c and d, then alert "Hello!"
               // subProcess2(): delete an element #input and submit all inputs to server.
               (bbb.match("fuga"))? subProcess1(e) : subProcess2(e);
               break;
           default:
               throw 'Unknown Case';
       }
   }
   catch(e)
   {
       // UnExpected processing.
       
       // console.log("Un error has occurred. " + e);
       // return false;   // to exit jQuery.each() loop.
   }
   
   return true;
});
/*************************************************************
*
* functions
*
*************************************************************/
/*
* return true when the condition is satisfied.
*/
function isRegular(aa, aaa)
{
   if(aa)                 { return true; }
   if(aaa.match("hoge"))  { return true; }
   return false;
}
/*
* return CASE CODE depending on conditions.
*/
function getCase(CASE, a, b)
{
   if(a >= 0 && b >= 0)   { return CASE.case1; }
   if(a <= 0 && b <= 0)   { return CASE.case1; }
   if(a <= 0 && b >= 0)   { return CASE.case2; }
   return CASE.case_unmatch;
}
/*
* When bb isn true AND b <= c, return true. otherwise return false
*/
function canProcess2(bb, b, c)
{
   if(bb && b <= c)   { return true; }
   
   return false;
}
/*
* create A and sum up total with c and d, then alert "Hello!"
*/
function subProcess1(e)
{
   //処理3
   
   if (e % 2 != 0) { return; }
   //処理4
}
/*
* delete an element #input and submit all inputs to server.
*/
function subProcess2(e)
{
   //処理1 ← Duplicated?
   
   if (e % 2 != 0) { return; }
   
   //処理2
}
```
2 modify

退会済みユーザー

退会済みユーザー

2018/04/17 12:58  投稿

You should use 'guard' solution. That is the way to reach the world of freaking geeks!  
 
```Javascript
/*************************************************************
*
* main process
*
*************************************************************/
$(elem).each(function()
{
   const CASE = {'case1' : 1, 'case2' : 2, 'case_unmatch': 0};
   
   var aaa = "moji", bbb = "moji",
       aa = true, bb = true,
       a  = 1, b = 1, c = 1, d = 1, e = 1;
   
   try
   {
       // When aa isn't true and aaa isn't much "hoge", throw 'Illegal Case'
       if(!isRegular(aa, aaa)) { throw 'Illegal Case'; }
       // get a CASE CODE depending on conditions
       switch(getCase(CASE, a, b))
       {
           // When (a >= 0 && b >= 0) or (a <= 0 && b <= 0)
           case CASE.case1:
               //処理1
               break;
           // When (a <= 0 && b >= 0)
           case CASE.case2:
               // When bb isn't true OR b > c, exit this scope at once.
               if (!canProcess2(bb, b, c)){ break; }
               // Information!
               // subProcess1(): create A and sum up total with c and d, then alert "Hello!"
               // subProcess2(): delete an element #input and submit all inputs to server.
               (bbb.match("fuga"))? subProcess1(e) : subProcess2(e);
               break;
           default:
               throw 'Unknown Case';
       }
   }
   catch(e)
   {
       // UnExpected processing.
       
       // console.log("Un error has occurred. " + e);
       // return false;   // to exit jQuery.each() loop.
   }
   
   return true;
});
/*************************************************************
*
* functions
*
*************************************************************/
/*
* return true when the condition is satisfied.
*/
function isRegular(aa, aaa)
{
   if(aa)                 { return true; }
   if(aaa.match("hoge"))  { return true; }
   return false;
}
/*
* return CASE CODE depending on conditions.
*/
function getCase(CASE, a, b)
{
   if(a >= 0 && b >= 0)   { return CASE.case1; }
   if(a <= 0 && b <= 0)   { return CASE.case1; }
   if(a <= 0 && b >= 0)   { return CASE.case2; }
   return CASE.case_unmatch;
}
/*
* When bb isn true AND b <= c, return true. otherwise return false
*/
function canProcess2(bb, b, c)
{
   if(bb && b <= c)   { return true; }
   
   return false;
}
/*
* create A and sum up total with c and d, then alert "Hello!"
*/
function subProcess1(e)
{
   //処理3
   
   if (e % 2 != 0) { return; }
   //処理4
}
/*
* delete an element #input and submit all inputs to server.
*/
function subProcess2(e)
{
   //処理1 ← Duplicated?
   
   if (e % 2 != 0) { return; }
   
   //処理2
}
```
1 modify

退会済みユーザー

退会済みユーザー

2018/04/17 12:56  投稿

```Javascript
/*************************************************************
*
* main process
*
*************************************************************/
$(elem).each(function()
{
   const CASE = {'case1' : 1, 'case2' : 2, 'case_unmatch': 0};
   
   var aaa = "moji", bbb = "moji",
       aa = true, bb = true,
       a  = 1, b = 1, c = 1, d = 1, e = 1;
   
   try
   {
       // When aa isn't and aaa isn't much "hoge", throw 'Illegal Case'
       // When aa isn't true and aaa isn't much "hoge", throw 'Illegal Case'
       if(!isRegular(aa, aaa)) { throw 'Illegal Case'; }
       // get a CASE CODE depending on conditions
       switch(getCase(CASE, a, b))
       {
           // When (a >= 0 && b >= 0) or (a <= 0 && b <= 0)
           case CASE.case1:
               //処理1
               break;
           // When (a <= 0 && b >= 0)
           case CASE.case2:
               // When bb isn't true OR b > c, exit this scope at once.
               if (!canProcess2(bb, b, c)){ break; }
               // Information!
               // subProcess1(): create A and sum up total with c and d, then alert "Hello!"
               // subProcess2(): delete an element #input and submit all inputs to server.
               (bbb.match("fuga"))? subProcess1(e) : subProcess2(e);
               break;
           default:
               throw 'Unknown Case';
       }
   }
   catch(e)
   {
       // UnExpected processing.
       
       // console.log("Un error has occurred. " + e);
       // return false;   // to exit jQuery.each() loop.
   }
   
   return true;
});
/*************************************************************
*
* functions
*
*************************************************************/
/*
* return true when the condition is satisfied.
*/
function isRegular(aa, aaa)
{
   if(aa)                 { return true; }
   if(aaa.match("hoge"))  { return true; }
   return false;
}
/*
* return CASE CODE depending on conditions.
*/
function getCase(CASE, a, b)
{
   if(a >= 0 && b >= 0)   { return CASE.case1; }
   if(a <= 0 && b <= 0)   { return CASE.case1; }
   if(a <= 0 && b >= 0)   { return CASE.case2; }
   return CASE.case_unmatch;
}
/*
* When bb isn true AND b <= c, return true. otherwise return false
*/
function canProcess2(bb, b, c)
{
   if(bb && b <= c)   { return true; }
   
   return false;
}
/*
* create A and sum up total with c and d, then alert "Hello!"
*/
function subProcess1(e)
{
   //処理3
   
   if (e % 2 != 0) { return; }
   //処理4
}
/*
* delete an element #input and submit all inputs to server.
*/
function subProcess2(e)
{
   //処理1 ← Duplicated?
   
   if (e % 2 != 0) { return; }
   
   //処理2
}
```

思考するエンジニアのためのQ&Aサイト「teratail」について詳しく知る