Conditions | 6 |
Paths | 10 |
Total Lines | 72 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import printInput from './printInput' |
||
9 | export default function printBlock (ctx, root) { |
||
10 | var res = '' |
||
11 | var precontrib = false |
||
12 | if (root.precontrib != null) { |
||
13 | precontrib = root.precontrib |
||
14 | } |
||
15 | |||
16 | if(ctx[0].block != null && ctx[0].block !== '') { |
||
17 | res += `<div class="form-group"> |
||
18 | <label class="title">${ctx[0].block}</label> |
||
19 | <div class='single-block well well-sm'>` |
||
20 | Array.prototype.forEach.call(ctx, (item) => { |
||
21 | if (precontrib) item.value = '' |
||
|
|||
22 | res += printInput(item, root) |
||
23 | }) |
||
24 | res += '</div></div>' |
||
25 | }else if(ctx[0].key.indexOf('[') > -1) { |
||
26 | var ctxBlock = ctx[0].key.split('[')[0] |
||
27 | res += `<div class="form-group"> |
||
28 | <div class="list-group" data-block="${ctxBlock}" > |
||
29 | <label> |
||
30 | ${ctxBlock} |
||
31 | <button type="button" class="btn btn-success add-block" title="Add new block" > |
||
32 | <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> |
||
33 | </button> |
||
34 | </label>` |
||
35 | |||
36 | var arrItem = [] |
||
37 | Array.prototype.forEach.call(ctx, (item) => { |
||
38 | var index = item.key.match(/[^\[\]]+?(?=\])/) |
||
39 | if(arrItem[index] == null) { |
||
40 | arrItem[index] = [] |
||
41 | } |
||
42 | arrItem[index].push(item) |
||
43 | }) |
||
44 | |||
45 | Array.prototype.forEach.call(Object.keys(arrItem), (i) => { |
||
46 | var key = arrItem[i][0].key.split('[')[0] |
||
47 | var display = '' |
||
48 | if(abeEngine.instance.content[key] == null |
||
49 | || abeEngine.instance.content[key].length === 0) { |
||
50 | display = 'style="display: none"' |
||
51 | } |
||
52 | res += `<div class="list-block" data-block="${key}${i}" ${display}> |
||
53 | <button type="button" class="btn btn-info collapsed" data-toggle="collapse" data-target="#${key}${i}" > |
||
54 | Section <span class='label-count'>${i}</span> : |
||
55 | <span class="glyphicon glyphicon-chevron-down" aria-hidden="true"></span> |
||
56 | </button> |
||
57 | <button type="button" class="btn btn-danger remove-block" title="Delete block" > |
||
58 | <span class="glyphicon glyphicon-trash" aria-hidden="true"></span> |
||
59 | </button> |
||
60 | <div id="${key}${i}" class="collapse" > |
||
61 | ` |
||
62 | Array.prototype.forEach.call(arrItem[i], (item) => { |
||
63 | if (precontrib) item.value = '' |
||
64 | res += printInput(item, root) |
||
65 | }) |
||
66 | res += '</div></div>' |
||
67 | }) |
||
68 | |||
69 | res += ` |
||
70 | </div> |
||
71 | </div>` |
||
72 | }else { |
||
73 | if (precontrib) ctx[0].value = '' |
||
74 | res += printInput(ctx[0], root) |
||
75 | } |
||
76 | |||
77 | // var template = cmsTemplates.Handlebars.compile(res) |
||
78 | // return new cmsTemplates.Handlebars.SafeString(template(ctx, {data: {intl: config.intlData}})) |
||
79 | return res |
||
80 | } |
||
81 |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.