<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical-reading-2-objectives">
<h2 class="hd hd-2 unit-title">Reading 2 Objectives</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_060e0f9a24c2">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="video" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_060e0f9a24c2">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Video"}
</script>
<h3 class="hd hd-2">Video</h3>
<div
id="video_video_060e0f9a24c2"
class="video closed"
data-metadata='{"saveStateEnabled": false, "speed": null, "ytMetadataEndpoint": "", "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_060e0f9a24c2/handler/transcript/available_translations", "captionDataDir": null, "completionEnabled": false, "poster": null, "generalSpeed": 1.0, "transcriptLanguages": {"en": "English"}, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_060e0f9a24c2/handler/transcript/translation/__lang__", "start": 0.0, "end": 0.0, "savedVideoPosition": 0.0, "lmsRootURL": "https://openlearninglibrary.mit.edu", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_060e0f9a24c2/handler/xmodule_handler/save_user_state", "ytApiUrl": "https://www.youtube.com/iframe_api", "autoAdvance": false, "ytTestTimeout": 1500, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_060e0f9a24c2/handler/publish_completion", "showCaptions": "true", "autohideHtml5": false, "streams": "1.00:qoixKv8KxM8", "prioritizeHls": false, "duration": 24.06, "transcriptLanguage": "en", "autoplay": false, "completionPercentage": 0.95, "recordedYoutubeIsAvailable": true, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT600512016-V001400_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V001400/MIT600512016-V001400.m3u8"]}'
data-bumper-metadata='null'
data-autoadvance-enabled="False"
data-poster='null'
tabindex="-1"
>
<div class="focus_grabber first"></div>
<div class="tc-wrapper">
<div class="video-wrapper">
<span tabindex="0" class="spinner" aria-hidden="false" aria-label="Loading video player"></span>
<span tabindex="-1" class="btn-play fa fa-youtube-play fa-2x is-hidden" aria-hidden="true" aria-label="Play video"></span>
<div class="video-player-pre"></div>
<div class="video-player">
<div id="video_060e0f9a24c2"></div>
<h4 class="hd hd-4 video-error is-hidden">No playable video sources found.</h4>
<h4 class="hd hd-4 video-hls-error is-hidden">
Your browser does not support this video format. Try using a different browser.
</h4>
</div>
<div class="video-player-post"></div>
<div class="closed-captions"></div>
<div class="video-controls is-hidden">
<div>
<div class="vcr"><div class="vidtime">0:00 / 0:00</div></div>
<div class="secondary-controls"></div>
</div>
</div>
</div>
</div>
<div class="focus_grabber last"></div>
<h3 class="hd hd-4 downloads-heading sr" id="video-download-transcripts_video_060e0f9a24c2">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_060e0f9a24c2">
<div class="wrapper-download-transcripts">
<h4 class="hd hd-5">Transcripts</h4>
<ul class="list-download-transcripts">
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_060e0f9a24c2/handler/transcript/download" data-value="srt">Download SubRip (.srt) file</a>
</li>
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_060e0f9a24c2/handler/transcript/download" data-value="txt">Download Text (.txt) file</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_94a61630be46">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_94a61630be46">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<h4>Software in 6.005</h4>
<table class="table table-striped no-markdown">
<tbody>
<tr>
<th width="33%">Safe from bugs</th>
<th>Easy to understand</th>
<th>Ready for change</th>
</tr>
<tr>
<td>
Correct today and correct in the unknown future
</td>
<td>
Communicates clearly with future programmers, including future you
</td>
<td>
Designed to accommodate change without rewriting
</td>
</tr>
</tbody>
</table>
<h4>Objectives for Today</h4>
<p>In today's reading, we will practice code review, which means reading and discussing code written by somebody else.</p>
<p>We’ll also talk about general principles of good coding: things you can look for in every code review, regardless of programming language or program purpose.</p>
<p>We’ll see that code review helps with all three of our big goals for good code: making it safe from bugs, easy to understand, and ready for change.</p>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical-dont-repeat-yourself">
<h2 class="hd hd-2 unit-title">Don't Repeat Yourself</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_c6cd7e93526d">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="video" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_c6cd7e93526d">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Video"}
</script>
<h3 class="hd hd-2">Video</h3>
<div
id="video_video_c6cd7e93526d"
class="video closed"
data-metadata='{"saveStateEnabled": false, "speed": null, "ytMetadataEndpoint": "", "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_c6cd7e93526d/handler/transcript/available_translations", "captionDataDir": null, "completionEnabled": false, "poster": "https://mitx-edx-video-meta-storage.s3.amazonaws.com/media/video-images/a22125bfeab04997b16c68c830500ffd.png?AWSAccessKeyId=ASIA4QDFFSURTPGWTCKO&Expires=1725182977&x-amz-security-token=IQoJb3JpZ2luX2VjEHIaCXVzLWVhc3QtMSJHMEUCIQCEnAG%2FCh3XY%2BwS%2BEkOqhHv6DmPFNobtpv%2BnqRLdm5ulQIgDdIKBXnWMRG4PIt%2BNAHTmTzs4%2BBPM3QNmxB0EsaLjLEqwgUIiv%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FARADGgw4NTkyMDU2Mzc0MTEiDHnikhNdXR4lyYsW2iqWBehRvwoCE80pAtcbvWZqBErTQG2tSq3MA0%2BaNCIDGaeVZTVbNFs4Fd7K4ZZh1ioUDt3ZESYKg8%2BBPOtZoGc2F5NIsZQFye%2Fn3vnErYAqbrgiDGb%2Fr3tWJcBJllVsFS7CTo%2Bt3jGqjhhMl8V0xr0xcitERzzaKf1snF3sz2oTXSzS5hPn7xblhWjMqs7gttCfi0JwJnXC6b928QVMMDoVcZmzPiZSekLc0lbIYzQ3C4UfdR6ol3l6zboOvSKr5UpWmF4ItQn5OWa2cGpPgT8e8B5jl4l7X0o077iMQsQaGqvdqZT2i2aPnIQO3X0A7ys0seq%2BafcZ9fDmvHXzBkzsepaB2BUkDUQ6hnmmueS5OD6%2FBlMHD3Q23eydJjVST6GLnpcjGvliZywzeea9mNnuM4SVtWqGoXxPO9x7%2FZSkcxO0E03Z24VOiE6Q%2FNMci9W6FQMlvWsYTKcCgUBcZ0c1ngU1wYKH%2B9lM8GMgcIS75xQdmj7Z5GY1%2Bu%2BWatVYb9i9KnKfW33Wc5ss5IzfYYsq8vNJ%2BxvTFZJBjQIN5iKEd0BZ7EptK3h0r2s9J6CiQ%2BUM5Ceuok7Y2NgsLmkui3dElexlPX0LNeA%2FEBmlMtMEy5lqX3ij6bfuRDrPQGTkqoGqFJBBzll%2BajyBfnePKvR%2B9P7%2FV0FNGhNMbBjTS1af5aQF4YBlJK2OzYHGKMWol%2BVIaC1KluSD2N22Q82On1rZ3eS%2BYpwEAA72oIpm9VrRPV1WKKUyUNf5Z5wyYHuNQadHj93pHQBGCrhxBlRcURAR%2F7tJPLMugjcuAtNlW2kejOhqrnOiHkPGxUdqENANJX%2FJYHOv6dvY4urbPoa3NyCMzL3XNGB3pUZdOev0O5O4Eq7Jw663UR9ZMLLf0LYGOrEBqDVTS8Y9RTRYf%2BELCalCN6HwhRdHnvsqSiPtgTd3jsMj3wyOU2nO2nzFNn%2BK3XBjPEFrM2GuXWVplpQqOa3UpvTa12ogHp3gDEdmfkH7wbOGuJ4JxdYqPr6mvKAkZVq%2FV9X8zeeboS%2FkhnKXAXSMc8qApFvIH2R%2F32L1gl6D1gVdNzuP13o9l9ZnlU9KvwoYsx9OF9PtDu954vkFLHiuESdZTx7awTokrFwBP7h4i0gf&Signature=u%2BgouLVCO0FPNwbgq4JpDu2YFMM%3D", "generalSpeed": 1.0, "transcriptLanguages": {"en": "English"}, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_c6cd7e93526d/handler/transcript/translation/__lang__", "start": 0.0, "end": 0.0, "savedVideoPosition": 0.0, "lmsRootURL": "https://openlearninglibrary.mit.edu", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_c6cd7e93526d/handler/xmodule_handler/save_user_state", "ytApiUrl": "https://www.youtube.com/iframe_api", "autoAdvance": false, "ytTestTimeout": 1500, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_c6cd7e93526d/handler/publish_completion", "showCaptions": "true", "autohideHtml5": false, "streams": "1.00:QWjgMiQp8YA", "prioritizeHls": false, "duration": 282.45, "transcriptLanguage": "en", "autoplay": false, "completionPercentage": 0.95, "recordedYoutubeIsAvailable": true, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT600512016-V001200_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V001200/MIT600512016-V001200.m3u8"]}'
data-bumper-metadata='null'
data-autoadvance-enabled="False"
data-poster='null'
tabindex="-1"
>
<div class="focus_grabber first"></div>
<div class="tc-wrapper">
<div class="video-wrapper">
<span tabindex="0" class="spinner" aria-hidden="false" aria-label="Loading video player"></span>
<span tabindex="-1" class="btn-play fa fa-youtube-play fa-2x is-hidden" aria-hidden="true" aria-label="Play video"></span>
<div class="video-player-pre"></div>
<div class="video-player">
<div id="video_c6cd7e93526d"></div>
<h4 class="hd hd-4 video-error is-hidden">No playable video sources found.</h4>
<h4 class="hd hd-4 video-hls-error is-hidden">
Your browser does not support this video format. Try using a different browser.
</h4>
</div>
<div class="video-player-post"></div>
<div class="closed-captions"></div>
<div class="video-controls is-hidden">
<div>
<div class="vcr"><div class="vidtime">0:00 / 0:00</div></div>
<div class="secondary-controls"></div>
</div>
</div>
</div>
</div>
<div class="focus_grabber last"></div>
<h3 class="hd hd-4 downloads-heading sr" id="video-download-transcripts_video_c6cd7e93526d">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_c6cd7e93526d">
<div class="wrapper-download-transcripts">
<h4 class="hd hd-5">Transcripts</h4>
<ul class="list-download-transcripts">
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_c6cd7e93526d/handler/transcript/download" data-value="srt">Download SubRip (.srt) file</a>
</li>
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_c6cd7e93526d/handler/transcript/download" data-value="txt">Download Text (.txt) file</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_e5065823f1b3">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_e5065823f1b3">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<h2 id="code_review">Code Review</h2>
<div data-outline="code_review">
<p>Code review is careful, systematic study of source code by people who are not the original author of the code. It's analogous to proofreading a term paper.</p>
<p>Code review really has two purposes:</p>
<ul>
<li><strong>Improving the code.</strong> Finding bugs, anticipating possible bugs, checking the clarity of the code, and checking for consistency with the project's style standards.</li>
<li><strong>Improving the programmer.</strong> Code review is an important way that programmers learn and teach each other, about new language features, changes in the design of the project or its coding standards, and new techniques. In open source projects, particularly, much conversation happens in the context of code reviews.</li>
</ul>
<p>Code review is widely practiced in open source projects like Apache and <a href="http://blog.humphd.org/vocamus-1569/?p=1569">Mozilla</a>. Code review is also widely practiced in industry. At Google, you can't push any code into the main repository until another engineer has signed off on it in a code review.</p>
<h3 id="style_standards">Style Standards</h3>
<div data-outline="style_standards">
<p>Most companies and large projects have coding style standards (for example, <a href="http://google.github.io/styleguide/javaguide.html">Google Java Style</a>). These can get pretty detailed, even to the point of specifying whitespace (how deep to indent) and where <a href="https://en.wikipedia.org/wiki/Indent_style#Placement_of_braces">curly braces</a> and parentheses should go. These kinds of questions often lead to <a href="http://www.outpost9.com/reference/jargon/jargon_23.html#TAG897">holy wars</a> since they end up being a matter of taste and style.</p>
<p>In 6.005, we have no official style guide of this sort. We're not going to tell you where to put your curly braces. That's a personal decision that each programmer should make. It's important to be self-consistent, however, and it's <em>very</em> important to follow the conventions of the project you're working on. If you're the programmer who reformats every module you touch to match your personal style, your teammates will hate you, and rightly so. Be a team player.</p>
<p>But there are some rules that are quite sensible and target our big three properties, in a stronger way than placing curly braces. The rest of this reading talks about some of these rules, at least the ones that are relevant at this point in the course, where we're mostly talking about writing basic Java. Here's a summary of the rules we'll cover in this reading:</p>
<ul>
<li>Don't Repeat Yourself (DRY)</li>
<li>Comments where needed</li>
<li>Fail fast</li>
<li>Avoid magic numbers</li>
<li>One purpose for each variable</li>
<li>Use good names</li>
<li>No global variables</li>
<li>Return results, don't print them</li>
<li>Use whitespace for readability</li>
</ul>
<p>These are some things you should start to look for when you're code reviewing other students, and when you're looking at your own code for improvement. Don't consider it an exhaustive list of code style guidelines, however. Over the course of the semester, we'll talk about a lot more things — specifications, abstract data types with representation invariants, concurrency and thread safety — which will then become fodder for code review.</p>
</div>
</div>
<h2 id="smelly_example_1">Smelly Example #1</h2>
<div data-outline="smelly_example_1">
<p>Programmers often describe bad code as having a “bad smell” that needs to be removed. “Code hygiene” is another word for this. Let's start with some smelly code.</p><pre><code class="language-java hljs"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> <span class="hljs-title">dayOfYear</span><span class="hljs-params">(<span class="hljs-keyword">int</span> month, <span class="hljs-keyword">int</span> dayOfMonth, <span class="hljs-keyword">int</span> year)</span> </span>{
<span class="hljs-keyword">if</span> (month == <span class="hljs-number">2</span>) {
dayOfMonth += <span class="hljs-number">31</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (month == <span class="hljs-number">3</span>) {
dayOfMonth += <span class="hljs-number">59</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (month == <span class="hljs-number">4</span>) {
dayOfMonth += <span class="hljs-number">90</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (month == <span class="hljs-number">5</span>) {
dayOfMonth += <span class="hljs-number">31</span> + <span class="hljs-number">28</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (month == <span class="hljs-number">6</span>) {
dayOfMonth += <span class="hljs-number">31</span> + <span class="hljs-number">28</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (month == <span class="hljs-number">7</span>) {
dayOfMonth += <span class="hljs-number">31</span> + <span class="hljs-number">28</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (month == <span class="hljs-number">8</span>) {
dayOfMonth += <span class="hljs-number">31</span> + <span class="hljs-number">28</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (month == <span class="hljs-number">9</span>) {
dayOfMonth += <span class="hljs-number">31</span> + <span class="hljs-number">28</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">31</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (month == <span class="hljs-number">10</span>) {
dayOfMonth += <span class="hljs-number">31</span> + <span class="hljs-number">28</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (month == <span class="hljs-number">11</span>) {
dayOfMonth += <span class="hljs-number">31</span> + <span class="hljs-number">28</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (month == <span class="hljs-number">12</span>) {
dayOfMonth += <span class="hljs-number">31</span> + <span class="hljs-number">28</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">31</span>;
}
<span class="hljs-keyword">return</span> dayOfMonth;
}</code></pre>
<p>The next few sections and exercises will pick out the particular smells in this code example.</p>
</div>
<h2 id="dont_repeat_yourself">Don't Repeat Yourself</h2>
<div data-outline="dont_repeat_yourself">
<p>Duplicated code is a risk to safety. If you have identical or very similar code in two places, then the fundamental risk is that there's a bug in both copies, and some maintainer fixes the bug in one place but not the other. </p>
<p>Avoid duplication like you'd avoid crossing the street without looking. Copy-and-paste is an enormously tempting programming tool, and you should feel a frisson of danger run down your spine every time you use it. The longer the block you're copying, the riskier it is.</p>
<p><a href="http://en.wikipedia.org/wiki/Don't_repeat_yourself">Don't Repeat Yourself</a>, or DRY for short, has become a programmer's mantra.</p>
<p>The dayOfYear example is full of identical code. How would you DRY it out?</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_118686a0e3aa">
<h2 class="hd hd-2 unit-title">Questions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<script src="/assets/courseware/v1/3e6443fb95285d2dc06e8ffc448b9bdf/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism.js"></script>
<script src="/assets/courseware/v1/2ba76f8bdfef4e2d45e31a3dc5208470/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/edx-script-v1.js"></script>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-repeat-yourself">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-repeat-yourself">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_02-dont-repeat-yourself" class="problems-wrapper" role="group"
aria-labelledby="02-dont-repeat-yourself-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-repeat-yourself" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-repeat-yourself/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="02-dont-repeat-yourself-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-repeat-yourself-problem-progress" tabindex="-1">
don&#39;t repeat yourself
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-repeat-yourself-problem-progress"></div>
<div class="problem">
<div>
<pre class="code">
public static int dayOfYear(int month, int dayOfMonth, int year) {
if (month == 2) {
dayOfMonth += 31;
} else if (month == 3) {
dayOfMonth += 59;
} else if (month == 4) {
dayOfMonth += 90;
} else if (month == 5) {
dayOfMonth += 31 + 28 + 31 + 30;
} else if (month == 6) {
dayOfMonth += 31 + 28 + 31 + 30 + 31;
} else if (month == 7) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30;
} else if (month == 8) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31;
} else if (month == 9) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
} else if (month == 10) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
} else if (month == 11) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
} else if (month == 12) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 31;
}
return dayOfMonth;
}
</pre>
<p>Some of the repetition in dayOfYear() is repeated values. How many times is the number of days in April written in dayOfYear()?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div id="formulaequationinput_02-dont-repeat-yourself_2_1" class="inputtype formulaequationinput">
<div class="unanswered">
<input type="text" name="input_02-dont-repeat-yourself_2_1" id="input_02-dont-repeat-yourself_2_1" data-input-id="02-dont-repeat-yourself_2_1" value="" aria-describedby="status_02-dont-repeat-yourself_2_1" size="20"/>
<span class="trailing_text" id="trailing_text_02-dont-repeat-yourself_2_1"/>
<span class="status unanswered" id="status_02-dont-repeat-yourself_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
<p id="answer_02-dont-repeat-yourself_2_1" class="answer"/>
<div id="input_02-dont-repeat-yourself_2_1_preview" class="equation">
\(\)
<img src="/static/images/spinner.bc34f953403f.gif" class="loading" alt="Loading"/>
</div>
</div>
<div class="script_placeholder" data-src="/static/js/capa/src/formula_equation_preview.b1967ab28c31.js"/>
</div></div>
<div class="solution-span">
<span id="solution_02-dont-repeat-yourself_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="don&#39;t repeat yourself" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_02-dont-repeat-yourself" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_02-dont-repeat-yourself">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-dont-repeat-yourself-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-dont-repeat-yourself-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-dont-repeat-yourself-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-2" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-repeat-yourself-5">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-repeat-yourself-5">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_02-dont-repeat-yourself-5" class="problems-wrapper" role="group"
aria-labelledby="02-dont-repeat-yourself-5-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-repeat-yourself-5" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-repeat-yourself-5/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="02-dont-repeat-yourself-5-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-repeat-yourself-5-problem-progress" tabindex="-1">
don&#39;t repeat yourself, part 2
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-repeat-yourself-5-problem-progress"></div>
<div class="problem">
<div>
<p>One reason why repeated code is bad is because a problem in the repeated code has to be fixed in many places, not just one. Suppose our calendar changed so that February really has 30 days instead of 28. How many numbers in this code have to be changed?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div id="formulaequationinput_02-dont-repeat-yourself-5_2_1" class="inputtype formulaequationinput">
<div class="unanswered">
<input type="text" name="input_02-dont-repeat-yourself-5_2_1" id="input_02-dont-repeat-yourself-5_2_1" data-input-id="02-dont-repeat-yourself-5_2_1" value="" aria-describedby="status_02-dont-repeat-yourself-5_2_1" size="20"/>
<span class="trailing_text" id="trailing_text_02-dont-repeat-yourself-5_2_1"/>
<span class="status unanswered" id="status_02-dont-repeat-yourself-5_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
<p id="answer_02-dont-repeat-yourself-5_2_1" class="answer"/>
<div id="input_02-dont-repeat-yourself-5_2_1_preview" class="equation">
\(\)
<img src="/static/images/spinner.bc34f953403f.gif" class="loading" alt="Loading"/>
</div>
</div>
<div class="script_placeholder" data-src="/static/js/capa/src/formula_equation_preview.b1967ab28c31.js"/>
</div></div>
<div class="solution-span">
<span id="solution_02-dont-repeat-yourself-5_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="don&#39;t repeat yourself, part 2" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_02-dont-repeat-yourself-5" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_02-dont-repeat-yourself-5">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-dont-repeat-yourself-5-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-dont-repeat-yourself-5-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-dont-repeat-yourself-5-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-3" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-repeat-yourself-6">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-repeat-yourself-6">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_02-dont-repeat-yourself-6" class="problems-wrapper" role="group"
aria-labelledby="02-dont-repeat-yourself-6-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-repeat-yourself-6" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-repeat-yourself-6/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="02-dont-repeat-yourself-6-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-repeat-yourself-6-problem-progress" tabindex="-1">
don&#39;t repeat yourself, part 3
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-repeat-yourself-6-problem-progress"></div>
<div class="problem">
<div>
<p>Another kind of repetition in this code is dayOfMonth+=. Assume you have an array:</p>
<pre class="code">
int[] monthLengths = new int[] { 31, 28, 31, 30, ..., 31}
</pre>
<p>Which of the following code skeletons could be used to DRY the code out enough so that dayOfMonth+= appears only once? Check all that apply.</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_02-dont-repeat-yourself-6_2_1">
<fieldset aria-describedby="status_02-dont-repeat-yourself-6_2_1">
<div class="field">
<input class="field-input input-checkbox" name="input_02-dont-repeat-yourself-6_2_1[]" id="input_02-dont-repeat-yourself-6_2_1_choice_0" type="checkbox" value="choice_0"/><label class="response-label field-label label-inline" for="input_02-dont-repeat-yourself-6_2_1_choice_0" id="02-dont-repeat-yourself-6_2_1-choice_0-label" aria-describedby="status_02-dont-repeat-yourself-6_2_1"> for (int m = 1; m &lt; month; ++m) { ... }
</label>
</div>
<div class="field">
<input class="field-input input-checkbox" name="input_02-dont-repeat-yourself-6_2_1[]" id="input_02-dont-repeat-yourself-6_2_1_choice_1" type="checkbox" value="choice_1"/><label class="response-label field-label label-inline" for="input_02-dont-repeat-yourself-6_2_1_choice_1" id="02-dont-repeat-yourself-6_2_1-choice_1-label" aria-describedby="status_02-dont-repeat-yourself-6_2_1"> switch (month) { case 1: ...; break; case 2: ...; break; ... }
</label>
</div>
<div class="field">
<input class="field-input input-checkbox" name="input_02-dont-repeat-yourself-6_2_1[]" id="input_02-dont-repeat-yourself-6_2_1_choice_2" type="checkbox" value="choice_2"/><label class="response-label field-label label-inline" for="input_02-dont-repeat-yourself-6_2_1_choice_2" id="02-dont-repeat-yourself-6_2_1-choice_2-label" aria-describedby="status_02-dont-repeat-yourself-6_2_1"> while (m &lt; month) { ...; m += 1; }
</label>
</div>
<div class="field">
<input class="field-input input-checkbox" name="input_02-dont-repeat-yourself-6_2_1[]" id="input_02-dont-repeat-yourself-6_2_1_choice_3" type="checkbox" value="choice_3"/><label class="response-label field-label label-inline" for="input_02-dont-repeat-yourself-6_2_1_choice_3" id="02-dont-repeat-yourself-6_2_1-choice_3-label" aria-describedby="status_02-dont-repeat-yourself-6_2_1"> if (month == 1) { ... } else { ... dayOfYear(month-1, dayOfMonth, year) ... }
</label>
</div>
<span id="answer_02-dont-repeat-yourself-6_2_1"/>
</fieldset>
<div class="indicator-container">
<span data-tooltip="Not yet answered." class="status unanswered" id="status_02-dont-repeat-yourself-6_2_1">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_02-dont-repeat-yourself-6_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="don&#39;t repeat yourself, part 3" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_02-dont-repeat-yourself-6" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_02-dont-repeat-yourself-6">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-dont-repeat-yourself-6-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-dont-repeat-yourself-6-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-dont-repeat-yourself-6-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@Comments_Where_Needed">
<h2 class="hd hd-2 unit-title">Comments Where Needed</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_3e4c9f57f60b">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="video" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_3e4c9f57f60b">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Video"}
</script>
<h3 class="hd hd-2">Video</h3>
<div
id="video_video_3e4c9f57f60b"
class="video closed"
data-metadata='{"saveStateEnabled": false, "speed": null, "ytMetadataEndpoint": "", "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_3e4c9f57f60b/handler/transcript/available_translations", "captionDataDir": null, "completionEnabled": false, "poster": null, "generalSpeed": 1.0, "transcriptLanguages": {"en": "English"}, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_3e4c9f57f60b/handler/transcript/translation/__lang__", "start": 0.0, "end": 0.0, "savedVideoPosition": 0.0, "lmsRootURL": "https://openlearninglibrary.mit.edu", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_3e4c9f57f60b/handler/xmodule_handler/save_user_state", "ytApiUrl": "https://www.youtube.com/iframe_api", "autoAdvance": false, "ytTestTimeout": 1500, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_3e4c9f57f60b/handler/publish_completion", "showCaptions": "true", "autohideHtml5": false, "streams": "1.00:nHN98UJZSmc", "prioritizeHls": false, "duration": 191.43, "transcriptLanguage": "en", "autoplay": false, "completionPercentage": 0.95, "recordedYoutubeIsAvailable": true, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT600512016-V001300_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V001300/MIT600512016-V001300.m3u8"]}'
data-bumper-metadata='null'
data-autoadvance-enabled="False"
data-poster='null'
tabindex="-1"
>
<div class="focus_grabber first"></div>
<div class="tc-wrapper">
<div class="video-wrapper">
<span tabindex="0" class="spinner" aria-hidden="false" aria-label="Loading video player"></span>
<span tabindex="-1" class="btn-play fa fa-youtube-play fa-2x is-hidden" aria-hidden="true" aria-label="Play video"></span>
<div class="video-player-pre"></div>
<div class="video-player">
<div id="video_3e4c9f57f60b"></div>
<h4 class="hd hd-4 video-error is-hidden">No playable video sources found.</h4>
<h4 class="hd hd-4 video-hls-error is-hidden">
Your browser does not support this video format. Try using a different browser.
</h4>
</div>
<div class="video-player-post"></div>
<div class="closed-captions"></div>
<div class="video-controls is-hidden">
<div>
<div class="vcr"><div class="vidtime">0:00 / 0:00</div></div>
<div class="secondary-controls"></div>
</div>
</div>
</div>
</div>
<div class="focus_grabber last"></div>
<h3 class="hd hd-4 downloads-heading sr" id="video-download-transcripts_video_3e4c9f57f60b">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_3e4c9f57f60b">
<div class="wrapper-download-transcripts">
<h4 class="hd hd-5">Transcripts</h4>
<ul class="list-download-transcripts">
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_3e4c9f57f60b/handler/transcript/download" data-value="srt">Download SubRip (.srt) file</a>
</li>
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_3e4c9f57f60b/handler/transcript/download" data-value="txt">Download Text (.txt) file</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_bac56b06d1ad">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_bac56b06d1ad">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<h2 id="comments_where_needed">Comments Where Needed</h2>
<div data-outline="comments_where_needed">
<p>A quick general word about commenting. Good software developers write comments in their code, and do it judiciously. Good comments should make the code easier to understand, safer from bugs (because important assumptions have been documented), and ready for change.</p>
<p>One kind of crucial comment is a specification, which appears above a method or above a class and documents the behavior of the method or class. In Java, this is conventionally written as a Javadoc comment, meaning that it starts with <code>/**</code> and includes <code>@</code>-syntax, like <code>@param</code> and <code>@return</code> for methods. Here's an example of a spec:</p><pre><code class="language-java hljs"><span class="hljs-comment">/**
* Compute the hailstone sequence.
* See http://en.wikipedia.org/wiki/Collatz_conjecture#Statement_of_the_problem
* <span class="hljs-doctag">@param</span> n starting number of sequence; requires n > 0.
* <span class="hljs-doctag">@return</span> the hailstone sequence starting at n and ending with 1.
* For example, hailstone(3)=[3,10,5,16,8,4,2,1].
*/</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> List<Integer> <span class="hljs-title">hailstoneSequence</span><span class="hljs-params">(<span class="hljs-keyword">int</span> n)</span> </span>{
...
}</code></pre>
<p>Specifications document assumptions. We've already mentioned specs a few times, and there will be much more to say about them in a future reading.</p>
<p>Another crucial comment is one that specifies the provenance or source of a piece of code that was copied or adapted from elsewhere. This is vitally important for practicing software developers, when you adapt code you found on the web. Here is an example:</p><pre><code class="language-java hljs"><span class="hljs-comment">// read a web page into a string</span>
<span class="hljs-comment">// see http://stackoverflow.com/questions/4328711/read-url-to-string-in-few-lines-of-java-code</span>
String mitHomepage = <span class="hljs-keyword">new</span> Scanner(<span class="hljs-keyword">new</span> URL(<span class="hljs-string">"http://www.mit.edu"</span>).openStream(), <span class="hljs-string">"UTF-8"</span>)
.useDelimiter(<span class="hljs-string">"\\A"</span>).next();</code></pre>
<p>One reason for documenting sources is to avoid violations of copyright. Small snippets of code on Stack Overflow are typically in the public domain, but code copied from other sources may be proprietary or covered by other kinds of open source licenses, which are more restrictive. Another reason for documenting sources is that the code can fall out of date; the <a href="http://stackoverflow.com/questions/4328711/read-url-to-string-in-few-lines-of-java-code">Stack Overflow answer</a> from which this code came has evolved significantly in the years since it was first answered.</p>
<p>Some comments are bad and unnecessary. Direct transliterations of code into English, for example, do nothing to improve understanding, because you should assume that your reader at least knows Java:</p><pre><code class="language-java hljs"><span class="hljs-keyword">while</span> (n != <span class="hljs-number">1</span>) { <span class="hljs-comment">// test whether n is 1 (don't write comments like this!)</span>
++i; <span class="hljs-comment">// increment i</span>
l.add(n); <span class="hljs-comment">// add n to l</span>
}</code></pre>
<p>But obscure code should get a comment:</p><pre><code class="language-java hljs">sendMessage(<span class="hljs-string">"as you wish"</span>); <span class="hljs-comment">// this basically says "I love you"</span></code></pre>
<p>The <code>dayOfYear</code> code needs some comments — where would you put them? For example, where would you document whether <code>month</code> runs from 0 to 11 or from 1 to 12? </p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_bfbb7bc03663">
<h2 class="hd hd-2 unit-title">Questions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<script src="/assets/courseware/v1/3e6443fb95285d2dc06e8ffc448b9bdf/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism.js"></script>
<script src="/assets/courseware/v1/2ba76f8bdfef4e2d45e31a3dc5208470/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/edx-script-v1.js"></script>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-comments-where-needed">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-comments-where-needed">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_02-comments-where-needed" class="problems-wrapper" role="group"
aria-labelledby="02-comments-where-needed-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-comments-where-needed" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-comments-where-needed/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="02-comments-where-needed-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-comments-where-needed-problem-progress" tabindex="-1">
comments where needed
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-comments-where-needed-problem-progress"></div>
<div class="problem">
<div>
<p>Which comments are useful additions to the code? Consider each comment independently, as if the other comments weren't there. Check all that apply.</p>
<pre class="code">
/** @param month month of the year, where January=1 and December=12 [C1] */
public static int dayOfYear(int month, int dayOfMonth, int year) {
if (month == 2) { // we're in February [C2]
dayOfMonth += 31; // add in the days of January that already passed [C3]
} else if (month == 3) {
dayOfMonth += 59; // month is 3 here [C4]
} else if (month == 4) {
dayOfMonth += 90;
}
...
} else if (month == 12) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 31;
}
return dayOfMonth; // the answer [C5]
}
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_02-comments-where-needed_2_1">
<fieldset aria-describedby="status_02-comments-where-needed_2_1">
<div class="field">
<input type="checkbox" name="input_02-comments-where-needed_2_1[]" id="input_02-comments-where-needed_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="02-comments-where-needed_2_1-choice_0-label" for="input_02-comments-where-needed_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_02-comments-where-needed_2_1"> C1
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-comments-where-needed_2_1[]" id="input_02-comments-where-needed_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="02-comments-where-needed_2_1-choice_1-label" for="input_02-comments-where-needed_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_02-comments-where-needed_2_1"> C2
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-comments-where-needed_2_1[]" id="input_02-comments-where-needed_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="02-comments-where-needed_2_1-choice_2-label" for="input_02-comments-where-needed_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_02-comments-where-needed_2_1"> C3
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-comments-where-needed_2_1[]" id="input_02-comments-where-needed_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="02-comments-where-needed_2_1-choice_3-label" for="input_02-comments-where-needed_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_02-comments-where-needed_2_1"> C4
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-comments-where-needed_2_1[]" id="input_02-comments-where-needed_2_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="02-comments-where-needed_2_1-choice_4-label" for="input_02-comments-where-needed_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_02-comments-where-needed_2_1"> C5
</label>
</div>
<span id="answer_02-comments-where-needed_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_02-comments-where-needed_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_02-comments-where-needed_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="comments where needed" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_02-comments-where-needed" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_02-comments-where-needed">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-comments-where-needed-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-comments-where-needed-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-comments-where-needed-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical-fail-fast">
<h2 class="hd hd-2 unit-title">Fail Fast</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_f2945d16c14b">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="video" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_f2945d16c14b">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Video"}
</script>
<h3 class="hd hd-2">Video</h3>
<div
id="video_video_f2945d16c14b"
class="video closed"
data-metadata='{"saveStateEnabled": false, "speed": null, "ytMetadataEndpoint": "", "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_f2945d16c14b/handler/transcript/available_translations", "captionDataDir": null, "completionEnabled": false, "poster": null, "generalSpeed": 1.0, "transcriptLanguages": {"en": "English"}, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_f2945d16c14b/handler/transcript/translation/__lang__", "start": 0.0, "end": 0.0, "savedVideoPosition": 0.0, "lmsRootURL": "https://openlearninglibrary.mit.edu", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_f2945d16c14b/handler/xmodule_handler/save_user_state", "ytApiUrl": "https://www.youtube.com/iframe_api", "autoAdvance": false, "ytTestTimeout": 1500, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_f2945d16c14b/handler/publish_completion", "showCaptions": "true", "autohideHtml5": false, "streams": "1.00:t_bioAGtrkM", "prioritizeHls": false, "duration": 96.43, "transcriptLanguage": "en", "autoplay": false, "completionPercentage": 0.95, "recordedYoutubeIsAvailable": true, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT600512016-V000900_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V000900/MIT600512016-V000900.m3u8"]}'
data-bumper-metadata='null'
data-autoadvance-enabled="False"
data-poster='null'
tabindex="-1"
>
<div class="focus_grabber first"></div>
<div class="tc-wrapper">
<div class="video-wrapper">
<span tabindex="0" class="spinner" aria-hidden="false" aria-label="Loading video player"></span>
<span tabindex="-1" class="btn-play fa fa-youtube-play fa-2x is-hidden" aria-hidden="true" aria-label="Play video"></span>
<div class="video-player-pre"></div>
<div class="video-player">
<div id="video_f2945d16c14b"></div>
<h4 class="hd hd-4 video-error is-hidden">No playable video sources found.</h4>
<h4 class="hd hd-4 video-hls-error is-hidden">
Your browser does not support this video format. Try using a different browser.
</h4>
</div>
<div class="video-player-post"></div>
<div class="closed-captions"></div>
<div class="video-controls is-hidden">
<div>
<div class="vcr"><div class="vidtime">0:00 / 0:00</div></div>
<div class="secondary-controls"></div>
</div>
</div>
</div>
</div>
<div class="focus_grabber last"></div>
<h3 class="hd hd-4 downloads-heading sr" id="video-download-transcripts_video_f2945d16c14b">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_f2945d16c14b">
<div class="wrapper-download-transcripts">
<h4 class="hd hd-5">Transcripts</h4>
<ul class="list-download-transcripts">
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_f2945d16c14b/handler/transcript/download" data-value="srt">Download SubRip (.srt) file</a>
</li>
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_f2945d16c14b/handler/transcript/download" data-value="txt">Download Text (.txt) file</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_343e252527da">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_343e252527da">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<h2 id="fail_fast">Fail Fast</h2>
<div data-outline="fail_fast">
<p><em>Failing fast</em> means that code should reveal its bugs as early as possible. The earlier a problem is observed (the closer to its cause), the easier it is to find and fix. As we saw in the <a href="/courses/course-v1:MITx+6.005.1x+3T2016/jump_to_id/vertical-static-typing#static_checking_dynamic_checking_no_checking">first reading</a>, static checking fails faster than dynamic checking, and dynamic checking fails faster than no checking at all, which may produce a wrong answer that could corrupt subsequent computation.</p>
<pre><code class="language-java hljs"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> <span class="hljs-title">dayOfYear</span><span class="hljs-params">(<span class="hljs-keyword">int</span> month, <span class="hljs-keyword">int</span> dayOfMonth, <span class="hljs-keyword">int</span> year)</span> </span>{
<span class="hljs-keyword">if</span> (month == <span class="hljs-number">2</span>) {
...
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (month == <span class="hljs-number">12</span>) {
dayOfMonth += <span class="hljs-number">31</span> + <span class="hljs-number">28</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">31</span>;
}
<span class="hljs-keyword">return</span> dayOfMonth;
}</code></pre>
<p>The <code>dayOfYear</code> function doesn't fail fast — if you pass it the arguments in the wrong order, it will quietly return the wrong answer. In fact, the way dayOfYear is designed, it's highly likely that a non-American will pass the arguments in the wrong order! It needs more checking — either static checking or dynamic checking.</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_78ac73adb996">
<h2 class="hd hd-2 unit-title">Questions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<script src="/assets/courseware/v1/3e6443fb95285d2dc06e8ffc448b9bdf/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism.js"></script>
<script src="/assets/courseware/v1/2ba76f8bdfef4e2d45e31a3dc5208470/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/edx-script-v1.js"></script>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-fail-fast">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-fail-fast">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_02-fail-fast" class="problems-wrapper" role="group"
aria-labelledby="02-fail-fast-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-fail-fast" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-fail-fast/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="6"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="02-fail-fast-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-fail-fast-problem-progress" tabindex="-1">
fail fast
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-fail-fast-problem-progress"></div>
<div class="problem">
<div>
<pre class="code">
public static int dayOfYear(int month, int dayOfMonth, int year) {
if (month == 2) {
dayOfMonth += 31;
} else if (month == 3) {
dayOfMonth += 59;
} else if (month == 4) {
dayOfMonth += 90;
} else if (month == 5) {
dayOfMonth += 31 + 28 + 31 + 30;
} else if (month == 6) {
dayOfMonth += 31 + 28 + 31 + 30 + 31;
} else if (month == 7) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30;
} else if (month == 8) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31;
} else if (month == 9) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
} else if (month == 10) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
} else if (month == 11) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
} else if (month == 12) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 31;
}
return dayOfMonth;
}
</pre>
<p>Suppose the date is January 3, 2019. The correct dayOfYear for this date is 3, since it's the third day of the year.</p>
<p>Which of the following are plausible ways that a programmer might (mistakenly) call dayOfYear? And for each one, does it lead to a static error, dynamic error, or wrong answer?</p>
<pre class="code">
dayOfYear(1, 3, 2019)
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="inputtype option-input ">
<select name="input_02-fail-fast_2_1" id="input_02-fail-fast_2_1" aria-describedby="status_02-fail-fast_2_1">
<option value="option_02-fail-fast_2_1_dummy_default">Select an option</option>
<option value="plausible mistake -- static error"> plausible mistake -- static error</option>
<option value="plausible mistake -- dynamic error"> plausible mistake -- dynamic error</option>
<option value="plausible mistake -- wrong answer"> plausible mistake -- wrong answer</option>
<option value="plausible mistake -- right answer"> plausible mistake -- right answer</option>
<option value="implausible mistake"> implausible mistake</option>
<option value="not a mistake -- right answer"> not a mistake -- right answer</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_02-fail-fast_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_02-fail-fast_2_1"/>
</div></div>
<div class="solution-span">
<span id="solution_02-fail-fast_solution_1"/>
</div><pre class="code">
dayOfYear(0, 3, 2019)
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 2" role="group"><div class="inputtype option-input ">
<select name="input_02-fail-fast_3_1" id="input_02-fail-fast_3_1" aria-describedby="status_02-fail-fast_3_1">
<option value="option_02-fail-fast_3_1_dummy_default">Select an option</option>
<option value="plausible mistake -- static error"> plausible mistake -- static error</option>
<option value="plausible mistake -- dynamic error"> plausible mistake -- dynamic error</option>
<option value="plausible mistake -- wrong answer"> plausible mistake -- wrong answer</option>
<option value="plausible mistake -- right answer"> plausible mistake -- right answer</option>
<option value="implausible mistake"> implausible mistake</option>
<option value="not a mistake -- right answer"> not a mistake -- right answer</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_02-fail-fast_3_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_02-fail-fast_3_1"/>
</div></div>
<div class="solution-span">
<span id="solution_02-fail-fast_solution_2"/>
</div><pre class="code">
dayOfYear(3, 1, 2019)
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 3" role="group"><div class="inputtype option-input ">
<select name="input_02-fail-fast_4_1" id="input_02-fail-fast_4_1" aria-describedby="status_02-fail-fast_4_1">
<option value="option_02-fail-fast_4_1_dummy_default">Select an option</option>
<option value="plausible mistake -- static error"> plausible mistake -- static error</option>
<option value="plausible mistake -- dynamic error"> plausible mistake -- dynamic error</option>
<option value="plausible mistake -- wrong answer"> plausible mistake -- wrong answer</option>
<option value="plausible mistake -- right answer"> plausible mistake -- right answer</option>
<option value="implausible mistake"> implausible mistake</option>
<option value="not a mistake -- right answer"> not a mistake -- right answer</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_02-fail-fast_4_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_02-fail-fast_4_1"/>
</div></div>
<div class="solution-span">
<span id="solution_02-fail-fast_solution_3"/>
</div><pre class="code">
dayOfYear("January", 3, 2019)
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 4" role="group"><div class="inputtype option-input ">
<select name="input_02-fail-fast_5_1" id="input_02-fail-fast_5_1" aria-describedby="status_02-fail-fast_5_1">
<option value="option_02-fail-fast_5_1_dummy_default">Select an option</option>
<option value="plausible mistake -- static error"> plausible mistake -- static error</option>
<option value="plausible mistake -- dynamic error"> plausible mistake -- dynamic error</option>
<option value="plausible mistake -- wrong answer"> plausible mistake -- wrong answer</option>
<option value="plausible mistake -- right answer"> plausible mistake -- right answer</option>
<option value="implausible mistake"> implausible mistake</option>
<option value="not a mistake -- right answer"> not a mistake -- right answer</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_02-fail-fast_5_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_02-fail-fast_5_1"/>
</div></div>
<div class="solution-span">
<span id="solution_02-fail-fast_solution_4"/>
</div><pre class="code">
dayOfYear(2019, 1, 3)
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 5" role="group"><div class="inputtype option-input ">
<select name="input_02-fail-fast_6_1" id="input_02-fail-fast_6_1" aria-describedby="status_02-fail-fast_6_1">
<option value="option_02-fail-fast_6_1_dummy_default">Select an option</option>
<option value="plausible mistake -- static error"> plausible mistake -- static error</option>
<option value="plausible mistake -- dynamic error"> plausible mistake -- dynamic error</option>
<option value="plausible mistake -- wrong answer"> plausible mistake -- wrong answer</option>
<option value="plausible mistake -- right answer"> plausible mistake -- right answer</option>
<option value="implausible mistake"> implausible mistake</option>
<option value="not a mistake -- right answer"> not a mistake -- right answer</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_02-fail-fast_6_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_02-fail-fast_6_1"/>
</div></div>
<div class="solution-span">
<span id="solution_02-fail-fast_solution_5"/>
</div><pre class="code">
dayOfYear(1, 2019, 3)
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 6" role="group"><div class="inputtype option-input ">
<select name="input_02-fail-fast_7_1" id="input_02-fail-fast_7_1" aria-describedby="status_02-fail-fast_7_1">
<option value="option_02-fail-fast_7_1_dummy_default">Select an option</option>
<option value="plausible mistake -- static error"> plausible mistake -- static error</option>
<option value="plausible mistake -- dynamic error"> plausible mistake -- dynamic error</option>
<option value="plausible mistake -- wrong answer"> plausible mistake -- wrong answer</option>
<option value="plausible mistake -- right answer"> plausible mistake -- right answer</option>
<option value="implausible mistake"> implausible mistake</option>
<option value="not a mistake -- right answer"> not a mistake -- right answer</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_02-fail-fast_7_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_02-fail-fast_7_1"/>
</div></div>
<div class="solution-span">
<span id="solution_02-fail-fast_solution_6"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="fail fast" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_02-fail-fast" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_02-fail-fast">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-fail-fast-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-fail-fast-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-fail-fast-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-2" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-fail-fast-8">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-fail-fast-8">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_02-fail-fast-8" class="problems-wrapper" role="group"
aria-labelledby="02-fail-fast-8-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-fail-fast-8" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-fail-fast-8/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="5"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="02-fail-fast-8-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-fail-fast-8-problem-progress" tabindex="-1">
fail fast, part 2
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-fail-fast-8-problem-progress"></div>
<div class="problem">
<div>
<p>Which of the following changes (considered separately) would make the code fail faster if it were called with arguments in the wrong order?</p>
<pre class="code">
public static int dayOfYear(String month, int dayOfMonth, int year) {
...
}
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="inputtype option-input ">
<select name="input_02-fail-fast-8_2_1" id="input_02-fail-fast-8_2_1" aria-describedby="status_02-fail-fast-8_2_1">
<option value="option_02-fail-fast-8_2_1_dummy_default">Select an option</option>
<option value="no change"> no change</option>
<option value="fails faster -- static error"> fails faster -- static error</option>
<option value="fails faster -- dynamic error"> fails faster -- dynamic error</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_02-fail-fast-8_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_02-fail-fast-8_2_1"/>
</div></div>
<div class="solution-span">
<span id="solution_02-fail-fast-8_solution_1"/>
</div><pre class="code">
public static int dayOfYear(int month, int dayOfMonth, int year) {
if (month &lt; 1 || month &gt; 12) {
return -1;
}
...
}
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 2" role="group"><div class="inputtype option-input ">
<select name="input_02-fail-fast-8_3_1" id="input_02-fail-fast-8_3_1" aria-describedby="status_02-fail-fast-8_3_1">
<option value="option_02-fail-fast-8_3_1_dummy_default">Select an option</option>
<option value="no change"> no change</option>
<option value="fails faster -- static error"> fails faster -- static error</option>
<option value="fails faster -- dynamic error"> fails faster -- dynamic error</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_02-fail-fast-8_3_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_02-fail-fast-8_3_1"/>
</div></div>
<div class="solution-span">
<span id="solution_02-fail-fast-8_solution_2"/>
</div><pre class="code">
public static int dayOfYear(int month, int dayOfMonth, int year) {
if (month &lt; 1 || month &gt; 12) {
throw new IllegalArgumentException();
}
...
}
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 3" role="group"><div class="inputtype option-input ">
<select name="input_02-fail-fast-8_4_1" id="input_02-fail-fast-8_4_1" aria-describedby="status_02-fail-fast-8_4_1">
<option value="option_02-fail-fast-8_4_1_dummy_default">Select an option</option>
<option value="no change"> no change</option>
<option value="fails faster -- static error"> fails faster -- static error</option>
<option value="fails faster -- dynamic error"> fails faster -- dynamic error</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_02-fail-fast-8_4_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_02-fail-fast-8_4_1"/>
</div></div>
<div class="solution-span">
<span id="solution_02-fail-fast-8_solution_3"/>
</div><pre class="code">
public enum Month { JANUARY, FEBRUARY, MARCH, ..., DECEMBER };
public static int dayOfYear(Month month, int dayOfMonth, int year) {
...
}
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 4" role="group"><div class="inputtype option-input ">
<select name="input_02-fail-fast-8_5_1" id="input_02-fail-fast-8_5_1" aria-describedby="status_02-fail-fast-8_5_1">
<option value="option_02-fail-fast-8_5_1_dummy_default">Select an option</option>
<option value="no change"> no change</option>
<option value="fails faster -- static error"> fails faster -- static error</option>
<option value="fails faster -- dynamic error"> fails faster -- dynamic error</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_02-fail-fast-8_5_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_02-fail-fast-8_5_1"/>
</div></div>
<div class="solution-span">
<span id="solution_02-fail-fast-8_solution_4"/>
</div><pre class="code">
public static int dayOfYear(int month, int dayOfMonth, int year) {
if (month == 1) {
...
} else if (month == 2) {
...
}
...
} else if (month == 12) {
...
} else {
throw new IllegalArgumentException("month out of range");
}
}
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 5" role="group"><div class="inputtype option-input ">
<select name="input_02-fail-fast-8_6_1" id="input_02-fail-fast-8_6_1" aria-describedby="status_02-fail-fast-8_6_1">
<option value="option_02-fail-fast-8_6_1_dummy_default">Select an option</option>
<option value="no change"> no change</option>
<option value="fails faster -- static error"> fails faster -- static error</option>
<option value="fails faster -- dynamic error"> fails faster -- dynamic error</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_02-fail-fast-8_6_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_02-fail-fast-8_6_1"/>
</div></div>
<div class="solution-span">
<span id="solution_02-fail-fast-8_solution_5"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="fail fast, part 2" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_02-fail-fast-8" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_02-fail-fast-8">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-fail-fast-8-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-fail-fast-8-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-fail-fast-8-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@Avoid_Magic_Numbers">
<h2 class="hd hd-2 unit-title">Avoid Magic Numbers</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_409fa2c115d9">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="video" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_409fa2c115d9">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Video"}
</script>
<h3 class="hd hd-2">Video</h3>
<div
id="video_video_409fa2c115d9"
class="video closed"
data-metadata='{"saveStateEnabled": false, "speed": null, "ytMetadataEndpoint": "", "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_409fa2c115d9/handler/transcript/available_translations", "captionDataDir": null, "completionEnabled": false, "poster": null, "generalSpeed": 1.0, "transcriptLanguages": {"en": "English"}, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_409fa2c115d9/handler/transcript/translation/__lang__", "start": 0.0, "end": 0.0, "savedVideoPosition": 0.0, "lmsRootURL": "https://openlearninglibrary.mit.edu", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_409fa2c115d9/handler/xmodule_handler/save_user_state", "ytApiUrl": "https://www.youtube.com/iframe_api", "autoAdvance": false, "ytTestTimeout": 1500, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_409fa2c115d9/handler/publish_completion", "showCaptions": "true", "autohideHtml5": false, "streams": "1.00:SkUb4opiXfg", "prioritizeHls": false, "duration": 110.64, "transcriptLanguage": "en", "autoplay": false, "completionPercentage": 0.95, "recordedYoutubeIsAvailable": true, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT600512016-V001000_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V001000/MIT600512016-V001000.m3u8"]}'
data-bumper-metadata='null'
data-autoadvance-enabled="False"
data-poster='null'
tabindex="-1"
>
<div class="focus_grabber first"></div>
<div class="tc-wrapper">
<div class="video-wrapper">
<span tabindex="0" class="spinner" aria-hidden="false" aria-label="Loading video player"></span>
<span tabindex="-1" class="btn-play fa fa-youtube-play fa-2x is-hidden" aria-hidden="true" aria-label="Play video"></span>
<div class="video-player-pre"></div>
<div class="video-player">
<div id="video_409fa2c115d9"></div>
<h4 class="hd hd-4 video-error is-hidden">No playable video sources found.</h4>
<h4 class="hd hd-4 video-hls-error is-hidden">
Your browser does not support this video format. Try using a different browser.
</h4>
</div>
<div class="video-player-post"></div>
<div class="closed-captions"></div>
<div class="video-controls is-hidden">
<div>
<div class="vcr"><div class="vidtime">0:00 / 0:00</div></div>
<div class="secondary-controls"></div>
</div>
</div>
</div>
</div>
<div class="focus_grabber last"></div>
<h3 class="hd hd-4 downloads-heading sr" id="video-download-transcripts_video_409fa2c115d9">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_409fa2c115d9">
<div class="wrapper-download-transcripts">
<h4 class="hd hd-5">Transcripts</h4>
<ul class="list-download-transcripts">
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_409fa2c115d9/handler/transcript/download" data-value="srt">Download SubRip (.srt) file</a>
</li>
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_409fa2c115d9/handler/transcript/download" data-value="txt">Download Text (.txt) file</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_800fcaeee8ea">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_800fcaeee8ea">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<h2 id="avoid_magic_numbers">Avoid Magic Numbers</h2>
<div data-outline="avoid_magic_numbers">
There are really only two constants that computer scientists recognize as valid in and of themselves: 0, 1, and maybe 2. (Okay, three constants.)</p>
<p>All other constants are called <a href="https://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constants">magic</a>, because they appear as if out of thin air with no explanation. One way to explain a number is with a comment, but a far better way is to declare the number as a named constant with a good, clear name.</p>
<pre><code class="language-java hljs"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> <span class="hljs-title">dayOfYear</span><span class="hljs-params">(<span class="hljs-keyword">int</span> month, <span class="hljs-keyword">int</span> dayOfMonth, <span class="hljs-keyword">int</span> year)</span> </span>{
<span class="hljs-keyword">if</span> (month == <span class="hljs-number">2</span>) {
dayOfMonth += <span class="hljs-number">31</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (month == <span class="hljs-number">3</span>) {
dayOfMonth += <span class="hljs-number">59</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (month == <span class="hljs-number">4</span>) {
dayOfMonth += <span class="hljs-number">90</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (month == <span class="hljs-number">5</span>) {
dayOfMonth += <span class="hljs-number">31</span> + <span class="hljs-number">28</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (month == <span class="hljs-number">6</span>) {
dayOfMonth += <span class="hljs-number">31</span> + <span class="hljs-number">28</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (month == <span class="hljs-number">7</span>) {
...
}</code></pre>
<p><code>dayOfYear</code> is full of magic numbers:</p>
<ul>
<li>The months 2, …, 12 would be far more readable as <code>FEBRUARY</code>, …, <code>DECEMBER</code>.</li>
<li>The days-of-months 30, 31, 28 would be more readable (and eliminate duplicate code) if they were in a data structure like an array, list, or map, e.g. <code>MONTH_LENGTH[month]</code>.</li>
<li>The mysterious numbers 59 and 90 are particularly pernicious examples of magic numbers. Not only are they uncommented and undocumented, but they are also the result of a <em>computation done by hand</em> by the programmer. Don't hardcode constants that you've computed by hand. Java is better at arithmetic than you are. Explicit computations like <code>31 + 28</code> make the provenance of these mysterious numbers much clearer. <code>MONTH_LENGTH[JANUARY] + MONTH_LENGTH[FEBRUARY]</code> would be clearer still.</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_10ae60ac943b">
<h2 class="hd hd-2 unit-title">Questions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<script src="/assets/courseware/v1/3e6443fb95285d2dc06e8ffc448b9bdf/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism.js"></script>
<script src="/assets/courseware/v1/2ba76f8bdfef4e2d45e31a3dc5208470/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/edx-script-v1.js"></script>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-avoid-magic-numbers">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-avoid-magic-numbers">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_02-avoid-magic-numbers" class="problems-wrapper" role="group"
aria-labelledby="02-avoid-magic-numbers-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-avoid-magic-numbers" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-avoid-magic-numbers/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="02-avoid-magic-numbers-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-avoid-magic-numbers-problem-progress" tabindex="-1">
avoid magic numbers
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-avoid-magic-numbers-problem-progress"></div>
<div class="problem">
<div>
<p>In the code:</p>
<pre class="code">
if (month == 2) { ... }
</pre>
<p>what might a reasonable programmer plausibly assume about the meaning of the magic number 2? Check all that apply.</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_02-avoid-magic-numbers_2_1">
<fieldset aria-describedby="status_02-avoid-magic-numbers_2_1">
<div class="field">
<input type="checkbox" name="input_02-avoid-magic-numbers_2_1[]" id="input_02-avoid-magic-numbers_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="02-avoid-magic-numbers_2_1-choice_0-label" for="input_02-avoid-magic-numbers_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_02-avoid-magic-numbers_2_1"> 2 might mean January
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-avoid-magic-numbers_2_1[]" id="input_02-avoid-magic-numbers_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="02-avoid-magic-numbers_2_1-choice_1-label" for="input_02-avoid-magic-numbers_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_02-avoid-magic-numbers_2_1"> 2 might mean February
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-avoid-magic-numbers_2_1[]" id="input_02-avoid-magic-numbers_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="02-avoid-magic-numbers_2_1-choice_2-label" for="input_02-avoid-magic-numbers_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_02-avoid-magic-numbers_2_1"> 2 might mean March
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-avoid-magic-numbers_2_1[]" id="input_02-avoid-magic-numbers_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="02-avoid-magic-numbers_2_1-choice_3-label" for="input_02-avoid-magic-numbers_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_02-avoid-magic-numbers_2_1"> 2 might mean the year 2 AD
</label>
</div>
<span id="answer_02-avoid-magic-numbers_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_02-avoid-magic-numbers_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_02-avoid-magic-numbers_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="avoid magic numbers" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_02-avoid-magic-numbers" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_02-avoid-magic-numbers">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-avoid-magic-numbers-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-avoid-magic-numbers-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-avoid-magic-numbers-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-2" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-avoid-magic-numbers-3">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-avoid-magic-numbers-3">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_02-avoid-magic-numbers-3" class="problems-wrapper" role="group"
aria-labelledby="02-avoid-magic-numbers-3-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-avoid-magic-numbers-3" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-avoid-magic-numbers-3/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="02-avoid-magic-numbers-3-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-avoid-magic-numbers-3-problem-progress" tabindex="-1">
avoid magic numbers, part 2
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-avoid-magic-numbers-3-problem-progress"></div>
<div class="problem">
<div>
<p>Suppose you're reading some code that uses a turtle graphics library that you don't know well, and you see the code:</p>
<pre class="code">
turtle.rotate(3);
</pre>
<p>Which of the following are likely assumptions you might make about the meaning of the magic number 3? Check all that apply.</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_02-avoid-magic-numbers-3_2_1">
<fieldset aria-describedby="status_02-avoid-magic-numbers-3_2_1">
<div class="field">
<input type="checkbox" name="input_02-avoid-magic-numbers-3_2_1[]" id="input_02-avoid-magic-numbers-3_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="02-avoid-magic-numbers-3_2_1-choice_0-label" for="input_02-avoid-magic-numbers-3_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_02-avoid-magic-numbers-3_2_1"> 3 might mean 3 degrees
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-avoid-magic-numbers-3_2_1[]" id="input_02-avoid-magic-numbers-3_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="02-avoid-magic-numbers-3_2_1-choice_1-label" for="input_02-avoid-magic-numbers-3_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_02-avoid-magic-numbers-3_2_1"> 3 might mean 3 radians
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-avoid-magic-numbers-3_2_1[]" id="input_02-avoid-magic-numbers-3_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="02-avoid-magic-numbers-3_2_1-choice_2-label" for="input_02-avoid-magic-numbers-3_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_02-avoid-magic-numbers-3_2_1"> 3 might mean 3 full revolutions
</label>
</div>
<span id="answer_02-avoid-magic-numbers-3_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_02-avoid-magic-numbers-3_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_02-avoid-magic-numbers-3_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="avoid magic numbers, part 2" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_02-avoid-magic-numbers-3" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_02-avoid-magic-numbers-3">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-avoid-magic-numbers-3-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-avoid-magic-numbers-3-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-avoid-magic-numbers-3-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-3" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-avoid-magic-numbers-4">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-avoid-magic-numbers-4">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_02-avoid-magic-numbers-4" class="problems-wrapper" role="group"
aria-labelledby="02-avoid-magic-numbers-4-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-avoid-magic-numbers-4" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-avoid-magic-numbers-4/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="4"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="02-avoid-magic-numbers-4-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-avoid-magic-numbers-4-problem-progress" tabindex="-1">
avoid magic numbers, part 3
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-avoid-magic-numbers-4-problem-progress"></div>
<div class="problem">
<div>
<p>Consider this code:</p>
<pre class="code">
for (int i = 0; i &lt; 5; ++i) {
turtle.forward(36);
turtle.turn(72);
}
</pre>
<p>The magic numbers in this code cause it to fail all three of our measures of code quality: it's not safe from bugs (SFB), not easy to understand (ETU) and not ready for change (RFC).</p>
<p>For each of the following rewrites, judge whether it improves SFB, ETU, and/or RFC, or none of the above. Check all that apply.</p>
<pre class="code">
final int five = 5;
final int thirtySix = 36;
final int seventyTwo = 72;
for (int i = 0; i &lt; five; ++i) {
turtle.forward(thirtySix);
turtle.turn(seventyTwo);
}
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_02-avoid-magic-numbers-4_2_1">
<fieldset aria-describedby="status_02-avoid-magic-numbers-4_2_1">
<div class="field">
<input type="checkbox" name="input_02-avoid-magic-numbers-4_2_1[]" id="input_02-avoid-magic-numbers-4_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="02-avoid-magic-numbers-4_2_1-choice_0-label" for="input_02-avoid-magic-numbers-4_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_02-avoid-magic-numbers-4_2_1"> no improvement (or worse)
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-avoid-magic-numbers-4_2_1[]" id="input_02-avoid-magic-numbers-4_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="02-avoid-magic-numbers-4_2_1-choice_1-label" for="input_02-avoid-magic-numbers-4_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_02-avoid-magic-numbers-4_2_1"> safer from bugs
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-avoid-magic-numbers-4_2_1[]" id="input_02-avoid-magic-numbers-4_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="02-avoid-magic-numbers-4_2_1-choice_2-label" for="input_02-avoid-magic-numbers-4_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_02-avoid-magic-numbers-4_2_1"> easier to understand
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-avoid-magic-numbers-4_2_1[]" id="input_02-avoid-magic-numbers-4_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="02-avoid-magic-numbers-4_2_1-choice_3-label" for="input_02-avoid-magic-numbers-4_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_02-avoid-magic-numbers-4_2_1"> more ready for change
</label>
</div>
<span id="answer_02-avoid-magic-numbers-4_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_02-avoid-magic-numbers-4_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_02-avoid-magic-numbers-4_solution_1"/>
</div><pre class="code">
int[] numbers = new int[] { 5, 36, 72 };
for (int i = 0; i &lt; numbers[0]; ++i) {
turtle.forward(numbers[1]);
turtle.turn(numbers[2]);
}
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 2" role="group"><div class="choicegroup capa_inputtype" id="inputtype_02-avoid-magic-numbers-4_3_1">
<fieldset aria-describedby="status_02-avoid-magic-numbers-4_3_1">
<div class="field">
<input type="checkbox" name="input_02-avoid-magic-numbers-4_3_1[]" id="input_02-avoid-magic-numbers-4_3_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="02-avoid-magic-numbers-4_3_1-choice_0-label" for="input_02-avoid-magic-numbers-4_3_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_02-avoid-magic-numbers-4_3_1"> no improvement (or worse)
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-avoid-magic-numbers-4_3_1[]" id="input_02-avoid-magic-numbers-4_3_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="02-avoid-magic-numbers-4_3_1-choice_1-label" for="input_02-avoid-magic-numbers-4_3_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_02-avoid-magic-numbers-4_3_1"> safer from bugs
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-avoid-magic-numbers-4_3_1[]" id="input_02-avoid-magic-numbers-4_3_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="02-avoid-magic-numbers-4_3_1-choice_2-label" for="input_02-avoid-magic-numbers-4_3_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_02-avoid-magic-numbers-4_3_1"> easier to understand
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-avoid-magic-numbers-4_3_1[]" id="input_02-avoid-magic-numbers-4_3_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="02-avoid-magic-numbers-4_3_1-choice_3-label" for="input_02-avoid-magic-numbers-4_3_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_02-avoid-magic-numbers-4_3_1"> more ready for change
</label>
</div>
<span id="answer_02-avoid-magic-numbers-4_3_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_02-avoid-magic-numbers-4_3_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_02-avoid-magic-numbers-4_solution_2"/>
</div><pre class="code">
int x = 5;
for (int i = 0; i &lt; x; ++i) {
turtle.forward(36);
turtle.turn(360 / x);
}
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 3" role="group"><div class="choicegroup capa_inputtype" id="inputtype_02-avoid-magic-numbers-4_4_1">
<fieldset aria-describedby="status_02-avoid-magic-numbers-4_4_1">
<div class="field">
<input type="checkbox" name="input_02-avoid-magic-numbers-4_4_1[]" id="input_02-avoid-magic-numbers-4_4_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="02-avoid-magic-numbers-4_4_1-choice_0-label" for="input_02-avoid-magic-numbers-4_4_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_02-avoid-magic-numbers-4_4_1"> no improvement (or worse)
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-avoid-magic-numbers-4_4_1[]" id="input_02-avoid-magic-numbers-4_4_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="02-avoid-magic-numbers-4_4_1-choice_1-label" for="input_02-avoid-magic-numbers-4_4_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_02-avoid-magic-numbers-4_4_1"> safer from bugs
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-avoid-magic-numbers-4_4_1[]" id="input_02-avoid-magic-numbers-4_4_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="02-avoid-magic-numbers-4_4_1-choice_2-label" for="input_02-avoid-magic-numbers-4_4_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_02-avoid-magic-numbers-4_4_1"> easier to understand
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-avoid-magic-numbers-4_4_1[]" id="input_02-avoid-magic-numbers-4_4_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="02-avoid-magic-numbers-4_4_1-choice_3-label" for="input_02-avoid-magic-numbers-4_4_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_02-avoid-magic-numbers-4_4_1"> more ready for change
</label>
</div>
<span id="answer_02-avoid-magic-numbers-4_4_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_02-avoid-magic-numbers-4_4_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_02-avoid-magic-numbers-4_solution_3"/>
</div><pre class="code">
final int fullCircleDegrees = 360;
final int numSides = 5;
final int sideLength = 36;
for (int i = 0; i &lt; numSides; ++i) {
turtle.forward(sideLength);
turtle.turn(fullCircleDegrees / numSides);
}
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 4" role="group"><div class="choicegroup capa_inputtype" id="inputtype_02-avoid-magic-numbers-4_5_1">
<fieldset aria-describedby="status_02-avoid-magic-numbers-4_5_1">
<div class="field">
<input type="checkbox" name="input_02-avoid-magic-numbers-4_5_1[]" id="input_02-avoid-magic-numbers-4_5_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="02-avoid-magic-numbers-4_5_1-choice_0-label" for="input_02-avoid-magic-numbers-4_5_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_02-avoid-magic-numbers-4_5_1"> no improvement (or worse)
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-avoid-magic-numbers-4_5_1[]" id="input_02-avoid-magic-numbers-4_5_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="02-avoid-magic-numbers-4_5_1-choice_1-label" for="input_02-avoid-magic-numbers-4_5_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_02-avoid-magic-numbers-4_5_1"> safer from bugs
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-avoid-magic-numbers-4_5_1[]" id="input_02-avoid-magic-numbers-4_5_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="02-avoid-magic-numbers-4_5_1-choice_2-label" for="input_02-avoid-magic-numbers-4_5_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_02-avoid-magic-numbers-4_5_1"> easier to understand
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-avoid-magic-numbers-4_5_1[]" id="input_02-avoid-magic-numbers-4_5_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="02-avoid-magic-numbers-4_5_1-choice_3-label" for="input_02-avoid-magic-numbers-4_5_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_02-avoid-magic-numbers-4_5_1"> more ready for change
</label>
</div>
<span id="answer_02-avoid-magic-numbers-4_5_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_02-avoid-magic-numbers-4_5_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_02-avoid-magic-numbers-4_solution_4"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="avoid magic numbers, part 3" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_02-avoid-magic-numbers-4" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_02-avoid-magic-numbers-4">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-avoid-magic-numbers-4-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-avoid-magic-numbers-4-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-avoid-magic-numbers-4-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@One_Purpose_For_Each_Variable">
<h2 class="hd hd-2 unit-title">One Purpose For Each Variable</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_bc3b5ad14dd6">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="video" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_bc3b5ad14dd6">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Video"}
</script>
<h3 class="hd hd-2">Video</h3>
<div
id="video_video_bc3b5ad14dd6"
class="video closed"
data-metadata='{"saveStateEnabled": false, "speed": null, "ytMetadataEndpoint": "", "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_bc3b5ad14dd6/handler/transcript/available_translations", "captionDataDir": null, "completionEnabled": false, "poster": null, "generalSpeed": 1.0, "transcriptLanguages": {"en": "English"}, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_bc3b5ad14dd6/handler/transcript/translation/__lang__", "start": 0.0, "end": 0.0, "savedVideoPosition": 0.0, "lmsRootURL": "https://openlearninglibrary.mit.edu", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_bc3b5ad14dd6/handler/xmodule_handler/save_user_state", "ytApiUrl": "https://www.youtube.com/iframe_api", "autoAdvance": false, "ytTestTimeout": 1500, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_bc3b5ad14dd6/handler/publish_completion", "showCaptions": "true", "autohideHtml5": false, "streams": "1.00:X2qhnJRMNhk", "prioritizeHls": false, "duration": 107.21000000000001, "transcriptLanguage": "en", "autoplay": false, "completionPercentage": 0.95, "recordedYoutubeIsAvailable": true, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT600512016-V001100_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V001100/MIT600512016-V001100.m3u8"]}'
data-bumper-metadata='null'
data-autoadvance-enabled="False"
data-poster='null'
tabindex="-1"
>
<div class="focus_grabber first"></div>
<div class="tc-wrapper">
<div class="video-wrapper">
<span tabindex="0" class="spinner" aria-hidden="false" aria-label="Loading video player"></span>
<span tabindex="-1" class="btn-play fa fa-youtube-play fa-2x is-hidden" aria-hidden="true" aria-label="Play video"></span>
<div class="video-player-pre"></div>
<div class="video-player">
<div id="video_bc3b5ad14dd6"></div>
<h4 class="hd hd-4 video-error is-hidden">No playable video sources found.</h4>
<h4 class="hd hd-4 video-hls-error is-hidden">
Your browser does not support this video format. Try using a different browser.
</h4>
</div>
<div class="video-player-post"></div>
<div class="closed-captions"></div>
<div class="video-controls is-hidden">
<div>
<div class="vcr"><div class="vidtime">0:00 / 0:00</div></div>
<div class="secondary-controls"></div>
</div>
</div>
</div>
</div>
<div class="focus_grabber last"></div>
<h3 class="hd hd-4 downloads-heading sr" id="video-download-transcripts_video_bc3b5ad14dd6">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_bc3b5ad14dd6">
<div class="wrapper-download-transcripts">
<h4 class="hd hd-5">Transcripts</h4>
<ul class="list-download-transcripts">
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_bc3b5ad14dd6/handler/transcript/download" data-value="srt">Download SubRip (.srt) file</a>
</li>
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_bc3b5ad14dd6/handler/transcript/download" data-value="txt">Download Text (.txt) file</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_b77041deffbd">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_b77041deffbd">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<h2 id="one_purpose_for_each_variable">One Purpose For Each Variable</h2>
<div data-outline="one_purpose_for_each_variable">
<p>In the <code>dayOfYear</code> example, the parameter <code>dayOfMonth</code> is reused to compute a very different value — the return value of the function, which is not the day of the month.</p>
<pre><code class="language-java hljs"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> <span class="hljs-title">dayOfYear</span><span class="hljs-params">(<span class="hljs-keyword">int</span> month, <span class="hljs-keyword">int</span> dayOfMonth, <span class="hljs-keyword">int</span> year)</span> </span>{
<span class="hljs-keyword">if</span> (month == <span class="hljs-number">2</span>) {
dayOfMonth += <span class="hljs-number">31</span>;
}
...
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (month == <span class="hljs-number">12</span>) {
dayOfMonth += <span class="hljs-number">31</span> + <span class="hljs-number">28</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">31</span>;
}
<span class="hljs-keyword">return</span> dayOfMonth;
}</code></pre>
<p>Don't reuse parameters, and don't reuse variables. Variables are not a scarce resource in programming. Introduce them freely, give them good names, and just stop using them when you stop needing them. You will confuse your reader if a variable that used to mean one thing suddenly starts meaning something different a few lines down.</p>
<p>Not only is this an ease-of-understanding question, but it's also a safety-from-bugs and ready-for-change question.</p>
<p>Method parameters, in particular, should generally be left unmodified. (This is important for being ready-for-change — in the future, some other part of the method may want to know what the original parameters of the method were, so you shouldn't blow them away while you're computing.) It's a good idea to use <code>final</code> for method parameters, and as many other variables as you can. The <code>final</code> keyword says that the variable should never be reassigned, and the Java compiler will check it statically. For example:</p><pre><code class="hljs java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> <span class="hljs-title">dayOfYear</span><span class="hljs-params">(<span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> month, <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> dayOfMonth, <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> year)</span> </span>{ ... }
</code></pre></div>
<h2 id="smelly_example_2">Smelly Example #2</h2>
<div data-outline="smelly_example_2">
<p>There was a latent bug in dayOfYear. It didn't handle leap years at all. As part of fixing that, suppose we write a leap-year method.</p><pre><code class="hljs cpp"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> boolean <span class="hljs-title">leap</span><span class="hljs-params">(<span class="hljs-keyword">int</span> y)</span> </span>{
String tmp = String.valueOf(y);
<span class="hljs-keyword">if</span> (tmp.charAt(<span class="hljs-number">2</span>) == <span class="hljs-string">'1'</span> || tmp.charAt(<span class="hljs-number">2</span>) == <span class="hljs-string">'3'</span> || tmp.charAt(<span class="hljs-number">2</span>) == <span class="hljs-number">5</span> || tmp.charAt(<span class="hljs-number">2</span>) == <span class="hljs-string">'7'</span> || tmp.charAt(<span class="hljs-number">2</span>) == <span class="hljs-string">'9'</span>) {
<span class="hljs-keyword">if</span> (tmp.charAt(<span class="hljs-number">3</span>)==<span class="hljs-string">'2'</span>||tmp.charAt(<span class="hljs-number">3</span>)==<span class="hljs-string">'6'</span>) <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
}<span class="hljs-keyword">else</span>{
<span class="hljs-keyword">if</span> (tmp.charAt(<span class="hljs-number">2</span>) == <span class="hljs-string">'0'</span> && tmp.charAt(<span class="hljs-number">3</span>) == <span class="hljs-string">'0'</span>) {
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
}
<span class="hljs-keyword">if</span> (tmp.charAt(<span class="hljs-number">3</span>)==<span class="hljs-string">'0'</span>||tmp.charAt(<span class="hljs-number">3</span>)==<span class="hljs-string">'4'</span>||tmp.charAt(<span class="hljs-number">3</span>)==<span class="hljs-string">'8'</span>)<span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
}
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
}
</code></pre>
<p>What are the bugs hidden in this code? And what style problems that we've already talked about?</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_0347825b8b49">
<h2 class="hd hd-2 unit-title">Questions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<script src="/assets/courseware/v1/3e6443fb95285d2dc06e8ffc448b9bdf/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism.js"></script>
<script src="/assets/courseware/v1/2ba76f8bdfef4e2d45e31a3dc5208470/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/edx-script-v1.js"></script>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_02-leap-year-code-review" class="problems-wrapper" role="group"
aria-labelledby="02-leap-year-code-review-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="02-leap-year-code-review-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-problem-progress" tabindex="-1">
leap year code review
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-problem-progress"></div>
<div class="problem">
<div>
<pre class="code">
public static boolean leap(int y) {
String tmp = String.valueOf(y);
if (tmp.charAt(2) == '1' || tmp.charAt(2) == '3' || tmp.charAt(2) == 5
|| tmp.charAt(2) == '7' || tmp.charAt(2) == '9') {
if (tmp.charAt(3)=='2'||tmp.charAt(3)=='6') return true; /*R1*/
else
return false; /*R2*/
}else{
if (tmp.charAt(2) == '0' &amp;&amp; tmp.charAt(3) == '0') {
return false; /*R3*/
}
if (tmp.charAt(3)=='0'||tmp.charAt(3)=='4'||tmp.charAt(3)=='8')return true; /*R4*/
}
return false; /*R5*/
}
</pre>
<p>What happens when you call:</p>
<pre class="code">
leap(2016)
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_02-leap-year-code-review_2_1">
<fieldset aria-describedby="status_02-leap-year-code-review_2_1">
<div class="field">
<input type="radio" name="input_02-leap-year-code-review_2_1" id="input_02-leap-year-code-review_2_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="02-leap-year-code-review_2_1-choice_0-label" for="input_02-leap-year-code-review_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review_2_1"> returns true on line R1
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review_2_1" id="input_02-leap-year-code-review_2_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="02-leap-year-code-review_2_1-choice_1-label" for="input_02-leap-year-code-review_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review_2_1"> returns false on line R2
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review_2_1" id="input_02-leap-year-code-review_2_1_choice_2" class="field-input input-radio" value="choice_2"/><label id="02-leap-year-code-review_2_1-choice_2-label" for="input_02-leap-year-code-review_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review_2_1"> returns false on line R3
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review_2_1" id="input_02-leap-year-code-review_2_1_choice_3" class="field-input input-radio" value="choice_3"/><label id="02-leap-year-code-review_2_1-choice_3-label" for="input_02-leap-year-code-review_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review_2_1"> returns true on line R4
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review_2_1" id="input_02-leap-year-code-review_2_1_choice_4" class="field-input input-radio" value="choice_4"/><label id="02-leap-year-code-review_2_1-choice_4-label" for="input_02-leap-year-code-review_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review_2_1"> returns false on line R5
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review_2_1" id="input_02-leap-year-code-review_2_1_choice_5" class="field-input input-radio" value="choice_5"/><label id="02-leap-year-code-review_2_1-choice_5-label" for="input_02-leap-year-code-review_2_1_choice_5" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review_2_1"> error before program starts
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review_2_1" id="input_02-leap-year-code-review_2_1_choice_6" class="field-input input-radio" value="choice_6"/><label id="02-leap-year-code-review_2_1-choice_6-label" for="input_02-leap-year-code-review_2_1_choice_6" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review_2_1"> error while program is running
</label>
</div>
<span id="answer_02-leap-year-code-review_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_02-leap-year-code-review_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_02-leap-year-code-review_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="leap year code review" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_02-leap-year-code-review" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_02-leap-year-code-review">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-leap-year-code-review-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-leap-year-code-review-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-leap-year-code-review-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-2" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-9">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-9">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_02-leap-year-code-review-9" class="problems-wrapper" role="group"
aria-labelledby="02-leap-year-code-review-9-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-9" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-9/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="02-leap-year-code-review-9-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-9-problem-progress" tabindex="-1">
leap year code review, part 2
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-9-problem-progress"></div>
<div class="problem">
<div>
<p>What happens when you call:</p>
<pre class="code">
leap(2017)
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_02-leap-year-code-review-9_2_1">
<fieldset aria-describedby="status_02-leap-year-code-review-9_2_1">
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-9_2_1" id="input_02-leap-year-code-review-9_2_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="02-leap-year-code-review-9_2_1-choice_0-label" for="input_02-leap-year-code-review-9_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-9_2_1"> returns true on line R1
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-9_2_1" id="input_02-leap-year-code-review-9_2_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="02-leap-year-code-review-9_2_1-choice_1-label" for="input_02-leap-year-code-review-9_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-9_2_1"> returns false on line R2
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-9_2_1" id="input_02-leap-year-code-review-9_2_1_choice_2" class="field-input input-radio" value="choice_2"/><label id="02-leap-year-code-review-9_2_1-choice_2-label" for="input_02-leap-year-code-review-9_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-9_2_1"> returns false on line R3
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-9_2_1" id="input_02-leap-year-code-review-9_2_1_choice_3" class="field-input input-radio" value="choice_3"/><label id="02-leap-year-code-review-9_2_1-choice_3-label" for="input_02-leap-year-code-review-9_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-9_2_1"> returns true on line R4
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-9_2_1" id="input_02-leap-year-code-review-9_2_1_choice_4" class="field-input input-radio" value="choice_4"/><label id="02-leap-year-code-review-9_2_1-choice_4-label" for="input_02-leap-year-code-review-9_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-9_2_1"> returns false on line 15
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-9_2_1" id="input_02-leap-year-code-review-9_2_1_choice_5" class="field-input input-radio" value="choice_5"/><label id="02-leap-year-code-review-9_2_1-choice_5-label" for="input_02-leap-year-code-review-9_2_1_choice_5" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-9_2_1"> error before program starts
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-9_2_1" id="input_02-leap-year-code-review-9_2_1_choice_6" class="field-input input-radio" value="choice_6"/><label id="02-leap-year-code-review-9_2_1-choice_6-label" for="input_02-leap-year-code-review-9_2_1_choice_6" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-9_2_1"> error while program is running
</label>
</div>
<span id="answer_02-leap-year-code-review-9_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_02-leap-year-code-review-9_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_02-leap-year-code-review-9_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="leap year code review, part 2" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_02-leap-year-code-review-9" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_02-leap-year-code-review-9">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-leap-year-code-review-9-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-leap-year-code-review-9-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-leap-year-code-review-9-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-3" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-10">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-10">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_02-leap-year-code-review-10" class="problems-wrapper" role="group"
aria-labelledby="02-leap-year-code-review-10-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-10" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-10/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="02-leap-year-code-review-10-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-10-problem-progress" tabindex="-1">
leap year code review, part 3
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-10-problem-progress"></div>
<div class="problem">
<div>
<p>What happens when you call:</p>
<pre class="code">
leap(2050)
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_02-leap-year-code-review-10_2_1">
<fieldset aria-describedby="status_02-leap-year-code-review-10_2_1">
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-10_2_1" id="input_02-leap-year-code-review-10_2_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="02-leap-year-code-review-10_2_1-choice_0-label" for="input_02-leap-year-code-review-10_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-10_2_1"> returns true on line R1
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-10_2_1" id="input_02-leap-year-code-review-10_2_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="02-leap-year-code-review-10_2_1-choice_1-label" for="input_02-leap-year-code-review-10_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-10_2_1"> returns false on line R2
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-10_2_1" id="input_02-leap-year-code-review-10_2_1_choice_2" class="field-input input-radio" value="choice_2"/><label id="02-leap-year-code-review-10_2_1-choice_2-label" for="input_02-leap-year-code-review-10_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-10_2_1"> returns false on line R3
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-10_2_1" id="input_02-leap-year-code-review-10_2_1_choice_3" class="field-input input-radio" value="choice_3"/><label id="02-leap-year-code-review-10_2_1-choice_3-label" for="input_02-leap-year-code-review-10_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-10_2_1"> returns true on line R4
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-10_2_1" id="input_02-leap-year-code-review-10_2_1_choice_4" class="field-input input-radio" value="choice_4"/><label id="02-leap-year-code-review-10_2_1-choice_4-label" for="input_02-leap-year-code-review-10_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-10_2_1"> returns false on line R5
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-10_2_1" id="input_02-leap-year-code-review-10_2_1_choice_5" class="field-input input-radio" value="choice_5"/><label id="02-leap-year-code-review-10_2_1-choice_5-label" for="input_02-leap-year-code-review-10_2_1_choice_5" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-10_2_1"> error before program starts
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-10_2_1" id="input_02-leap-year-code-review-10_2_1_choice_6" class="field-input input-radio" value="choice_6"/><label id="02-leap-year-code-review-10_2_1-choice_6-label" for="input_02-leap-year-code-review-10_2_1_choice_6" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-10_2_1"> error while program is running
</label>
</div>
<span id="answer_02-leap-year-code-review-10_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_02-leap-year-code-review-10_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_02-leap-year-code-review-10_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="leap year code review, part 3" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_02-leap-year-code-review-10" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_02-leap-year-code-review-10">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-leap-year-code-review-10-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-leap-year-code-review-10-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-leap-year-code-review-10-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-4" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-11">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-11">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_02-leap-year-code-review-11" class="problems-wrapper" role="group"
aria-labelledby="02-leap-year-code-review-11-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-11" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-11/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="02-leap-year-code-review-11-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-11-problem-progress" tabindex="-1">
leap year code review, part 4
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-11-problem-progress"></div>
<div class="problem">
<div>
<p>What happens when you call:</p>
<pre class="code">
leap(-2016)
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_02-leap-year-code-review-11_2_1">
<fieldset aria-describedby="status_02-leap-year-code-review-11_2_1">
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-11_2_1" id="input_02-leap-year-code-review-11_2_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="02-leap-year-code-review-11_2_1-choice_0-label" for="input_02-leap-year-code-review-11_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-11_2_1"> returns true on line R1
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-11_2_1" id="input_02-leap-year-code-review-11_2_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="02-leap-year-code-review-11_2_1-choice_1-label" for="input_02-leap-year-code-review-11_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-11_2_1"> returns false on line R2
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-11_2_1" id="input_02-leap-year-code-review-11_2_1_choice_2" class="field-input input-radio" value="choice_2"/><label id="02-leap-year-code-review-11_2_1-choice_2-label" for="input_02-leap-year-code-review-11_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-11_2_1"> returns false on line R3
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-11_2_1" id="input_02-leap-year-code-review-11_2_1_choice_3" class="field-input input-radio" value="choice_3"/><label id="02-leap-year-code-review-11_2_1-choice_3-label" for="input_02-leap-year-code-review-11_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-11_2_1"> returns true on line R4
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-11_2_1" id="input_02-leap-year-code-review-11_2_1_choice_4" class="field-input input-radio" value="choice_4"/><label id="02-leap-year-code-review-11_2_1-choice_4-label" for="input_02-leap-year-code-review-11_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-11_2_1"> returns false on line R5
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-11_2_1" id="input_02-leap-year-code-review-11_2_1_choice_5" class="field-input input-radio" value="choice_5"/><label id="02-leap-year-code-review-11_2_1-choice_5-label" for="input_02-leap-year-code-review-11_2_1_choice_5" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-11_2_1"> error before program starts
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-11_2_1" id="input_02-leap-year-code-review-11_2_1_choice_6" class="field-input input-radio" value="choice_6"/><label id="02-leap-year-code-review-11_2_1-choice_6-label" for="input_02-leap-year-code-review-11_2_1_choice_6" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-11_2_1"> error while program is running
</label>
</div>
<span id="answer_02-leap-year-code-review-11_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_02-leap-year-code-review-11_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_02-leap-year-code-review-11_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="leap year code review, part 4" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_02-leap-year-code-review-11" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_02-leap-year-code-review-11">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-leap-year-code-review-11-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-leap-year-code-review-11-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-leap-year-code-review-11-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-5" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-12">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-12">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_02-leap-year-code-review-12" class="problems-wrapper" role="group"
aria-labelledby="02-leap-year-code-review-12-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-12" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-12/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="02-leap-year-code-review-12-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-12-problem-progress" tabindex="-1">
leap year code review, part 5
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-12-problem-progress"></div>
<div class="problem">
<div>
<p>What happens when you call:</p>
<pre class="code">
leap(916)
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_02-leap-year-code-review-12_2_1">
<fieldset aria-describedby="status_02-leap-year-code-review-12_2_1">
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-12_2_1" id="input_02-leap-year-code-review-12_2_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="02-leap-year-code-review-12_2_1-choice_0-label" for="input_02-leap-year-code-review-12_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-12_2_1"> returns true on line R1
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-12_2_1" id="input_02-leap-year-code-review-12_2_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="02-leap-year-code-review-12_2_1-choice_1-label" for="input_02-leap-year-code-review-12_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-12_2_1"> returns false on line R2
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-12_2_1" id="input_02-leap-year-code-review-12_2_1_choice_2" class="field-input input-radio" value="choice_2"/><label id="02-leap-year-code-review-12_2_1-choice_2-label" for="input_02-leap-year-code-review-12_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-12_2_1"> returns false on line R3
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-12_2_1" id="input_02-leap-year-code-review-12_2_1_choice_3" class="field-input input-radio" value="choice_3"/><label id="02-leap-year-code-review-12_2_1-choice_3-label" for="input_02-leap-year-code-review-12_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-12_2_1"> returns true on line R4
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-12_2_1" id="input_02-leap-year-code-review-12_2_1_choice_4" class="field-input input-radio" value="choice_4"/><label id="02-leap-year-code-review-12_2_1-choice_4-label" for="input_02-leap-year-code-review-12_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-12_2_1"> returns false on line R5
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-12_2_1" id="input_02-leap-year-code-review-12_2_1_choice_5" class="field-input input-radio" value="choice_5"/><label id="02-leap-year-code-review-12_2_1-choice_5-label" for="input_02-leap-year-code-review-12_2_1_choice_5" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-12_2_1"> error before program starts
</label>
</div>
<div class="field">
<input type="radio" name="input_02-leap-year-code-review-12_2_1" id="input_02-leap-year-code-review-12_2_1_choice_6" class="field-input input-radio" value="choice_6"/><label id="02-leap-year-code-review-12_2_1-choice_6-label" for="input_02-leap-year-code-review-12_2_1_choice_6" class="response-label field-label label-inline" aria-describedby="status_02-leap-year-code-review-12_2_1"> error while program is running
</label>
</div>
<span id="answer_02-leap-year-code-review-12_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_02-leap-year-code-review-12_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_02-leap-year-code-review-12_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="leap year code review, part 5" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_02-leap-year-code-review-12" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_02-leap-year-code-review-12">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-leap-year-code-review-12-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-leap-year-code-review-12-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-leap-year-code-review-12-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-6" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-13">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-13">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_02-leap-year-code-review-13" class="problems-wrapper" role="group"
aria-labelledby="02-leap-year-code-review-13-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-13" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-13/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="02-leap-year-code-review-13-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-13-problem-progress" tabindex="-1">
leap year code review, part 6
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-13-problem-progress"></div>
<div class="problem">
<div>
<p>How many magic numbers are in this code?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div id="formulaequationinput_02-leap-year-code-review-13_2_1" class="inputtype formulaequationinput">
<div class="unanswered">
<input type="text" name="input_02-leap-year-code-review-13_2_1" id="input_02-leap-year-code-review-13_2_1" data-input-id="02-leap-year-code-review-13_2_1" value="" aria-describedby="status_02-leap-year-code-review-13_2_1" size="20"/>
<span class="trailing_text" id="trailing_text_02-leap-year-code-review-13_2_1"/>
<span class="status unanswered" id="status_02-leap-year-code-review-13_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
<p id="answer_02-leap-year-code-review-13_2_1" class="answer"/>
<div id="input_02-leap-year-code-review-13_2_1_preview" class="equation">
\(\)
<img src="/static/images/spinner.bc34f953403f.gif" class="loading" alt="Loading"/>
</div>
</div>
<div class="script_placeholder" data-src="/static/js/capa/src/formula_equation_preview.b1967ab28c31.js"/>
</div></div>
<div class="solution-span">
<span id="solution_02-leap-year-code-review-13_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="leap year code review, part 6" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_02-leap-year-code-review-13" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_02-leap-year-code-review-13">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-leap-year-code-review-13-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-leap-year-code-review-13-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-leap-year-code-review-13-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-7" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-14">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-14">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_02-leap-year-code-review-14" class="problems-wrapper" role="group"
aria-labelledby="02-leap-year-code-review-14-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-14" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-14/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="02-leap-year-code-review-14-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-14-problem-progress" tabindex="-1">
leap year code review, part 7
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-leap-year-code-review-14-problem-progress"></div>
<div class="problem">
<div>
<p>Suppose you wrote the helper function:</p>
<pre class="code">
public static boolean isDivisibleBy(int number, int factor) { return number % factor == 0; }
</pre>
<p>If leap() were rewritten to use isDivisibleBy(year, ...) and to correctly follow the <a target="_blank" href="http://en.wikipedia.org/wiki/Leap_year#Algorithm">leap year algorithm</a>, how many magic numbers would be in the code?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div id="formulaequationinput_02-leap-year-code-review-14_2_1" class="inputtype formulaequationinput">
<div class="unanswered">
<input type="text" name="input_02-leap-year-code-review-14_2_1" id="input_02-leap-year-code-review-14_2_1" data-input-id="02-leap-year-code-review-14_2_1" value="" aria-describedby="status_02-leap-year-code-review-14_2_1" size="20"/>
<span class="trailing_text" id="trailing_text_02-leap-year-code-review-14_2_1"/>
<span class="status unanswered" id="status_02-leap-year-code-review-14_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
<p id="answer_02-leap-year-code-review-14_2_1" class="answer"/>
<div id="input_02-leap-year-code-review-14_2_1_preview" class="equation">
\(\)
<img src="/static/images/spinner.bc34f953403f.gif" class="loading" alt="Loading"/>
</div>
</div>
<div class="script_placeholder" data-src="/static/js/capa/src/formula_equation_preview.b1967ab28c31.js"/>
</div></div>
<div class="solution-span">
<span id="solution_02-leap-year-code-review-14_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="leap year code review, part 7" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_02-leap-year-code-review-14" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_02-leap-year-code-review-14">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-leap-year-code-review-14-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-leap-year-code-review-14-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-leap-year-code-review-14-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@Use_Good_Names">
<h2 class="hd hd-2 unit-title">Use Good Names</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_77fdeabdcf59">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="video" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_77fdeabdcf59">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Video"}
</script>
<h3 class="hd hd-2">Video</h3>
<div
id="video_video_77fdeabdcf59"
class="video closed"
data-metadata='{"saveStateEnabled": false, "speed": null, "ytMetadataEndpoint": "", "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_77fdeabdcf59/handler/transcript/available_translations", "captionDataDir": null, "completionEnabled": false, "poster": null, "generalSpeed": 1.0, "transcriptLanguages": {"en": "English"}, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_77fdeabdcf59/handler/transcript/translation/__lang__", "start": 0.0, "end": 0.0, "savedVideoPosition": 0.0, "lmsRootURL": "https://openlearninglibrary.mit.edu", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_77fdeabdcf59/handler/xmodule_handler/save_user_state", "ytApiUrl": "https://www.youtube.com/iframe_api", "autoAdvance": false, "ytTestTimeout": 1500, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_77fdeabdcf59/handler/publish_completion", "showCaptions": "true", "autohideHtml5": false, "streams": "1.00:WyAieBNc8R0", "prioritizeHls": false, "duration": 171.62, "transcriptLanguage": "en", "autoplay": false, "completionPercentage": 0.95, "recordedYoutubeIsAvailable": true, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT600512016-V001600_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V001600/MIT600512016-V001600.m3u8"]}'
data-bumper-metadata='null'
data-autoadvance-enabled="False"
data-poster='null'
tabindex="-1"
>
<div class="focus_grabber first"></div>
<div class="tc-wrapper">
<div class="video-wrapper">
<span tabindex="0" class="spinner" aria-hidden="false" aria-label="Loading video player"></span>
<span tabindex="-1" class="btn-play fa fa-youtube-play fa-2x is-hidden" aria-hidden="true" aria-label="Play video"></span>
<div class="video-player-pre"></div>
<div class="video-player">
<div id="video_77fdeabdcf59"></div>
<h4 class="hd hd-4 video-error is-hidden">No playable video sources found.</h4>
<h4 class="hd hd-4 video-hls-error is-hidden">
Your browser does not support this video format. Try using a different browser.
</h4>
</div>
<div class="video-player-post"></div>
<div class="closed-captions"></div>
<div class="video-controls is-hidden">
<div>
<div class="vcr"><div class="vidtime">0:00 / 0:00</div></div>
<div class="secondary-controls"></div>
</div>
</div>
</div>
</div>
<div class="focus_grabber last"></div>
<h3 class="hd hd-4 downloads-heading sr" id="video-download-transcripts_video_77fdeabdcf59">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_77fdeabdcf59">
<div class="wrapper-download-transcripts">
<h4 class="hd hd-5">Transcripts</h4>
<ul class="list-download-transcripts">
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_77fdeabdcf59/handler/transcript/download" data-value="srt">Download SubRip (.srt) file</a>
</li>
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_77fdeabdcf59/handler/transcript/download" data-value="txt">Download Text (.txt) file</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_2257b496f76e">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_2257b496f76e">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<h2 id="use_good_names">Use Good Names</h2>
<div data-outline="use_good_names">
<p>Good method and variable names are long and self-descriptive. Comments can often be avoided entirely by making the code itself more readable, with better names that describe the methods and variables. </p>
<p>For example, you can rewrite </p><pre><code class="hljs cpp"><span class="hljs-keyword">int</span> tmp = <span class="hljs-number">86400</span>; <span class="hljs-comment">// tmp is the number of seconds in a day (don't do this!)</span>
</code></pre>
<p>as:</p><pre><code class="hljs cpp"><span class="hljs-keyword">int</span> secondsPerDay = <span class="hljs-number">86400</span>;
</code></pre>
<p>In general, variable names like <code>tmp</code>, <code>temp</code>, and <code>data</code> are awful symptoms of extreme programmer laziness. Every local variable is temporary, and every variable is data, so those names are generally meaningless. It is better to use a longer, more descriptive name, so that your code reads clearly all by itself.</p>
<p>Follow the lexical naming conventions of the language. In Python, classes are typically Capitalized, variables are lowercase, and words_are_separated_by_underscores. In Java:</p>
<ul>
<li>methodsAreNamedWithCamelCaseLikeThis</li>
<li>variablesAreAlsoCamelCase</li>
<li>CONSTANTS_ARE_IN_ALL_CAPS_WITH_UNDERSCORES</li>
<li>ClassesAreCapitalized</li>
<li>packages.are.lowercase.and.separated.by.dots</li>
</ul>
<p>Method names are usually verb phrases, like <code>getDate</code> or <code>isUpperCase</code>, while variable and class names are usually noun phrases. Choose short words, and be concise, but avoid abbreviations. For example, <code>message</code> is clearer than <code>msg</code>, and <code>word</code> is so much better than <code>wd</code>. Keep in mind that many of your teammates in class and in the real world will not be native English speakers, and abbreviations can be even harder for non-native speakers.</p>
<p>ALL_CAPS_WITH_UNDERSCORES is used for <code>static final</code> constants. All variables declared inside a method, including <code>final</code> ones, use camelCaseNames.</p>
<p>The <code>leap</code> method has bad names: the method name itself, the parameter, and the local variable name. What would you call them instead?</p>
<pre><code class="hljs cpp"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> boolean <span class="hljs-title">leap</span><span class="hljs-params">(<span class="hljs-keyword">int</span> y)</span> </span>{
String tmp = String.valueOf(y);
...
}
</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_ed5bb299c02e">
<h2 class="hd hd-2 unit-title">Questions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<script src="/assets/courseware/v1/3e6443fb95285d2dc06e8ffc448b9bdf/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism.js"></script>
<script src="/assets/courseware/v1/2ba76f8bdfef4e2d45e31a3dc5208470/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/edx-script-v1.js"></script>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-use-good-names">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-use-good-names">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_02-use-good-names" class="problems-wrapper" role="group"
aria-labelledby="02-use-good-names-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-use-good-names" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-use-good-names/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="02-use-good-names-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-use-good-names-problem-progress" tabindex="-1">
use good names
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-use-good-names-problem-progress"></div>
<div class="problem">
<div>
<pre class="code">
public static boolean leap(int y) {
String tmp = String.valueOf(y);
if (tmp.charAt(2) == '1' || tmp.charAt(2) == '3' || tmp.charAt(2) == 5
|| tmp.charAt(2) == '7' || tmp.charAt(2) == '9') {
if (tmp.charAt(3)=='2'||tmp.charAt(3)=='6') return true;
else
return false;
}else{
if (tmp.charAt(2) == '0' &amp;&amp; tmp.charAt(3) == '0') {
return false;
}
if (tmp.charAt(3)=='0'||tmp.charAt(3)=='4'||tmp.charAt(3)=='8')return true;
}
return false;
}
</pre>
<p>Which of the following are good names for the leap() method?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_02-use-good-names_2_1">
<fieldset aria-describedby="status_02-use-good-names_2_1">
<div class="field">
<input type="checkbox" name="input_02-use-good-names_2_1[]" id="input_02-use-good-names_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="02-use-good-names_2_1-choice_0-label" for="input_02-use-good-names_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_02-use-good-names_2_1"> leap
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-use-good-names_2_1[]" id="input_02-use-good-names_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="02-use-good-names_2_1-choice_1-label" for="input_02-use-good-names_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_02-use-good-names_2_1"> isLeapYear
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-use-good-names_2_1[]" id="input_02-use-good-names_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="02-use-good-names_2_1-choice_2-label" for="input_02-use-good-names_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_02-use-good-names_2_1"> IsLeapYear
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-use-good-names_2_1[]" id="input_02-use-good-names_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="02-use-good-names_2_1-choice_3-label" for="input_02-use-good-names_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_02-use-good-names_2_1"> is_divisible_by_4
</label>
</div>
<span id="answer_02-use-good-names_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_02-use-good-names_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_02-use-good-names_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="use good names" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_02-use-good-names" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_02-use-good-names">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-use-good-names-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-use-good-names-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-use-good-names-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-2" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-use-good-names-15">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-use-good-names-15">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_02-use-good-names-15" class="problems-wrapper" role="group"
aria-labelledby="02-use-good-names-15-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-use-good-names-15" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-use-good-names-15/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="02-use-good-names-15-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-use-good-names-15-problem-progress" tabindex="-1">
use good names, part 2
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-use-good-names-15-problem-progress"></div>
<div class="problem">
<div>
<p>Which of the following are good names for the tmp variable?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_02-use-good-names-15_2_1">
<fieldset aria-describedby="status_02-use-good-names-15_2_1">
<div class="field">
<input type="checkbox" name="input_02-use-good-names-15_2_1[]" id="input_02-use-good-names-15_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="02-use-good-names-15_2_1-choice_0-label" for="input_02-use-good-names-15_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_02-use-good-names-15_2_1"> leapYearString
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-use-good-names-15_2_1[]" id="input_02-use-good-names-15_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="02-use-good-names-15_2_1-choice_1-label" for="input_02-use-good-names-15_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_02-use-good-names-15_2_1"> yearString
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-use-good-names-15_2_1[]" id="input_02-use-good-names-15_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="02-use-good-names-15_2_1-choice_2-label" for="input_02-use-good-names-15_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_02-use-good-names-15_2_1"> temp
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-use-good-names-15_2_1[]" id="input_02-use-good-names-15_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="02-use-good-names-15_2_1-choice_3-label" for="input_02-use-good-names-15_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_02-use-good-names-15_2_1"> secondsPerDay
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-use-good-names-15_2_1[]" id="input_02-use-good-names-15_2_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="02-use-good-names-15_2_1-choice_4-label" for="input_02-use-good-names-15_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_02-use-good-names-15_2_1"> s
</label>
</div>
<span id="answer_02-use-good-names-15_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_02-use-good-names-15_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_02-use-good-names-15_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="use good names, part 2" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_02-use-good-names-15" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_02-use-good-names-15">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-use-good-names-15-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-use-good-names-15-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-use-good-names-15-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@Use_Whitespace_To_Help_The_Reader">
<h2 class="hd hd-2 unit-title">Use Whitespace To Help The Reader</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_39aa35a8cdb6">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="video" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_39aa35a8cdb6">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Video"}
</script>
<h3 class="hd hd-2">Video</h3>
<div
id="video_video_39aa35a8cdb6"
class="video closed"
data-metadata='{"saveStateEnabled": false, "speed": null, "ytMetadataEndpoint": "", "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_39aa35a8cdb6/handler/transcript/available_translations", "captionDataDir": null, "completionEnabled": false, "poster": null, "generalSpeed": 1.0, "transcriptLanguages": {"en": "English"}, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_39aa35a8cdb6/handler/transcript/translation/__lang__", "start": 0.0, "end": 0.0, "savedVideoPosition": 0.0, "lmsRootURL": "https://openlearninglibrary.mit.edu", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_39aa35a8cdb6/handler/xmodule_handler/save_user_state", "ytApiUrl": "https://www.youtube.com/iframe_api", "autoAdvance": false, "ytTestTimeout": 1500, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_39aa35a8cdb6/handler/publish_completion", "showCaptions": "true", "autohideHtml5": false, "streams": "1.00:Wxsev8EWgd4", "prioritizeHls": false, "duration": 246.71, "transcriptLanguage": "en", "autoplay": false, "completionPercentage": 0.95, "recordedYoutubeIsAvailable": true, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT600512016-V001500_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V001500/MIT600512016-V001500.m3u8"]}'
data-bumper-metadata='null'
data-autoadvance-enabled="False"
data-poster='null'
tabindex="-1"
>
<div class="focus_grabber first"></div>
<div class="tc-wrapper">
<div class="video-wrapper">
<span tabindex="0" class="spinner" aria-hidden="false" aria-label="Loading video player"></span>
<span tabindex="-1" class="btn-play fa fa-youtube-play fa-2x is-hidden" aria-hidden="true" aria-label="Play video"></span>
<div class="video-player-pre"></div>
<div class="video-player">
<div id="video_39aa35a8cdb6"></div>
<h4 class="hd hd-4 video-error is-hidden">No playable video sources found.</h4>
<h4 class="hd hd-4 video-hls-error is-hidden">
Your browser does not support this video format. Try using a different browser.
</h4>
</div>
<div class="video-player-post"></div>
<div class="closed-captions"></div>
<div class="video-controls is-hidden">
<div>
<div class="vcr"><div class="vidtime">0:00 / 0:00</div></div>
<div class="secondary-controls"></div>
</div>
</div>
</div>
</div>
<div class="focus_grabber last"></div>
<h3 class="hd hd-4 downloads-heading sr" id="video-download-transcripts_video_39aa35a8cdb6">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_39aa35a8cdb6">
<div class="wrapper-download-transcripts">
<h4 class="hd hd-5">Transcripts</h4>
<ul class="list-download-transcripts">
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_39aa35a8cdb6/handler/transcript/download" data-value="srt">Download SubRip (.srt) file</a>
</li>
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_39aa35a8cdb6/handler/transcript/download" data-value="txt">Download Text (.txt) file</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_564c5de2d9fa">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_564c5de2d9fa">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<h2 id="use_whitespace_to_help_the_reader">Use Whitespace to Help the Reader</h2>
<div data-outline="use_whitespace_to_help_the_reader">
<p>Use consistent indentation. The <code>leap</code> example is bad at this:</p>
<pre><code class="hljs cpp"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> boolean <span class="hljs-title">leap</span><span class="hljs-params">(<span class="hljs-keyword">int</span> y)</span> </span>{
String tmp = String.valueOf(y);
<span class="hljs-keyword">if</span> (tmp.charAt(<span class="hljs-number">2</span>) == <span class="hljs-string">'1'</span> || tmp.charAt(<span class="hljs-number">2</span>) == <span class="hljs-string">'3'</span> || tmp.charAt(<span class="hljs-number">2</span>) == <span class="hljs-number">5</span> || tmp.charAt(<span class="hljs-number">2</span>) == <span class="hljs-string">'7'</span> || tmp.charAt(<span class="hljs-number">2</span>) == <span class="hljs-string">'9'</span>) {
<span class="hljs-keyword">if</span> (tmp.charAt(<span class="hljs-number">3</span>)==<span class="hljs-string">'2'</span>||tmp.charAt(<span class="hljs-number">3</span>)==<span class="hljs-string">'6'</span>) <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
}<span class="hljs-keyword">else</span>{
<span class="hljs-keyword">if</span> (tmp.charAt(<span class="hljs-number">2</span>) == <span class="hljs-string">'0'</span> && tmp.charAt(<span class="hljs-number">3</span>) == <span class="hljs-string">'0'</span>) {
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
}
<span class="hljs-keyword">if</span> (tmp.charAt(<span class="hljs-number">3</span>)==<span class="hljs-string">'0'</span>||tmp.charAt(<span class="hljs-number">3</span>)==<span class="hljs-string">'4'</span>||tmp.charAt(<span class="hljs-number">3</span>)==<span class="hljs-string">'8'</span>)<span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
}
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
}
</code></pre>
<p>The <code>dayOfYear</code> example is much better. In fact, <code>dayOfYear</code> nicely lines up all the numbers into columns, making them easy for a human reader to compare and check. That's a great use of whitespace.</p>
<pre><code class="language-java hljs">
...
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (month == <span class="hljs-number">5</span>) {
dayOfMonth += <span class="hljs-number">31</span> + <span class="hljs-number">28</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (month == <span class="hljs-number">6</span>) {
dayOfMonth += <span class="hljs-number">31</span> + <span class="hljs-number">28</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (month == <span class="hljs-number">7</span>) {
dayOfMonth += <span class="hljs-number">31</span> + <span class="hljs-number">28</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (month == <span class="hljs-number">8</span>) {
dayOfMonth += <span class="hljs-number">31</span> + <span class="hljs-number">28</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (month == <span class="hljs-number">9</span>) {
dayOfMonth += <span class="hljs-number">31</span> + <span class="hljs-number">28</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">31</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (month == <span class="hljs-number">10</span>) {
dayOfMonth += <span class="hljs-number">31</span> + <span class="hljs-number">28</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (month == <span class="hljs-number">11</span>) {
dayOfMonth += <span class="hljs-number">31</span> + <span class="hljs-number">28</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (month == <span class="hljs-number">12</span>) {
dayOfMonth += <span class="hljs-number">31</span> + <span class="hljs-number">28</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">31</span> + <span class="hljs-number">30</span> + <span class="hljs-number">31</span> + <span class="hljs-number">31</span>;
}
</code></pre>
<p>Put spaces within code lines to make them easy to read. The leap example has some lines that are packed together — put in some spaces.</p>
<p>Never use tab characters for indentation, only space characters. Note that we say <em>characters</em>, not keys. We're not saying you should never press the Tab key, only that your editor should never put a tab character into your source file in response to your pressing the Tab key. The reason for this rule is that different tools treat tab characters differently — sometimes expanding them to 4 spaces, sometimes to 2 spaces, sometimes to 8. If another programmer views your source code in a different editor, then the indentation may be completely screwed up. Just use spaces. Always set your programming editor to insert space characters when you press the Tab key.
<a href="http://blog.codinghorror.com/death-to-the-space-infidels/">Unless the team you're working with has opposite policy</a>, of course.</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_56818266eb2f">
<h2 class="hd hd-2 unit-title">Questions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<script src="/assets/courseware/v1/3e6443fb95285d2dc06e8ffc448b9bdf/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism.js"></script>
<script src="/assets/courseware/v1/2ba76f8bdfef4e2d45e31a3dc5208470/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/edx-script-v1.js"></script>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-whitespace">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-whitespace">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_02-whitespace" class="problems-wrapper" role="group"
aria-labelledby="02-whitespace-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-whitespace" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-whitespace/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="02-whitespace-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-whitespace-problem-progress" tabindex="-1">
whitespace
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-whitespace-problem-progress"></div>
<div class="problem">
<div>
<p>The code below was written using an editor with a tab setting of 8 columns, meaning that a tab character causes a jump to the next multiple of 8 columns. The space and tab characters are visualized the way Eclipse would do it, with &#183; for space and &#187; for tab.</p>
<pre class="code">
&#183;&#183;&#183;&#183;if&#183;(a)&#183;{
&#187; if&#183;(b)&#183;{}
&#187; else&#183;if&#183;(c)&#183;{}
&#183;&#183;&#183;&#183;}&#183;else&#183;{}
</pre>
<p>Suppose a different programmer loads this code into their own editor, with a different tab setting. What tab setting in that programmer's editor would make all four lines look like they were aligned with each other, starting at the same column?</p>
<p>Tab setting (number of columns):</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div id="formulaequationinput_02-whitespace_2_1" class="inputtype formulaequationinput">
<div class="unanswered">
<input type="text" name="input_02-whitespace_2_1" id="input_02-whitespace_2_1" data-input-id="02-whitespace_2_1" value="" aria-describedby="status_02-whitespace_2_1" size="20"/>
<span class="trailing_text" id="trailing_text_02-whitespace_2_1"/>
<span class="status unanswered" id="status_02-whitespace_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
<p id="answer_02-whitespace_2_1" class="answer"/>
<div id="input_02-whitespace_2_1_preview" class="equation">
\(\)
<img src="/static/images/spinner.bc34f953403f.gif" class="loading" alt="Loading"/>
</div>
</div>
<div class="script_placeholder" data-src="/static/js/capa/src/formula_equation_preview.b1967ab28c31.js"/>
</div></div>
<div class="solution-span">
<span id="solution_02-whitespace_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="whitespace" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_02-whitespace" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_02-whitespace">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-whitespace-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-whitespace-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-whitespace-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-2" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-whitespace-2">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-whitespace-2">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_02-whitespace-2" class="problems-wrapper" role="group"
aria-labelledby="02-whitespace-2-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-whitespace-2" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-whitespace-2/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="02-whitespace-2-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-whitespace-2-problem-progress" tabindex="-1">
whitespace confusion
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-whitespace-2-problem-progress"></div>
<div class="problem">
<div>
<p>Suppose some sloppy editing by the first programmer changed the indentation of line 2 into 4 spaces followed by a tab, so that it looks like this in the first programmer's tab-8 setting:</p>
<pre class="code">
&#183;&#183;&#183;&#183;if&#183;(a)&#183;{
&#183;&#183;&#183;&#183;&#187; if&#183;(b)&#183;{}
&#187; else&#183;if&#183;(c)&#183;{}
&#183;&#183;&#183;&#183;}&#183;else&#183;{}
</pre>
<p>Notice that line 2 still looks indented by only 8 columns because a tab character jumps ahead to the next <em>tab stop</em>, the next multiple of the tab setting. So putting four spaces ahead of the tab doesn't affect it how it looks to the first programmer; the tab still jumps ahead to column 8.</p>
<p>Now the second programmer looks at the code in their own editor, which has the tab setting we found in the previous exercise. Which of the following are true?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_02-whitespace-2_2_1">
<fieldset aria-describedby="status_02-whitespace-2_2_1">
<div class="field">
<input type="checkbox" name="input_02-whitespace-2_2_1[]" id="input_02-whitespace-2_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="02-whitespace-2_2_1-choice_0-label" for="input_02-whitespace-2_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_02-whitespace-2_2_1"> For the second programmer, all four lines look aligned with each other
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-whitespace-2_2_1[]" id="input_02-whitespace-2_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="02-whitespace-2_2_1-choice_1-label" for="input_02-whitespace-2_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_02-whitespace-2_2_1"> For both programmers, line 2 is indented to column 8
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-whitespace-2_2_1[]" id="input_02-whitespace-2_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="02-whitespace-2_2_1-choice_2-label" for="input_02-whitespace-2_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_02-whitespace-2_2_1"> For the second programmer, the <code>else if</code> on line 3 aligns with the <code>if</code> on line 1
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-whitespace-2_2_1[]" id="input_02-whitespace-2_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="02-whitespace-2_2_1-choice_3-label" for="input_02-whitespace-2_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_02-whitespace-2_2_1"> For the second programmer, the indentation disagrees with the block structure indicated by the curly braces
</label>
</div>
<span id="answer_02-whitespace-2_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_02-whitespace-2_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_02-whitespace-2_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="whitespace confusion" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_02-whitespace-2" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_02-whitespace-2">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-whitespace-2-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-whitespace-2-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-whitespace-2-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical-dont-use-global-variables">
<h2 class="hd hd-2 unit-title">Don't Use Global Variables; Return, Don't Print</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_8017faacaf17">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="video" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_8017faacaf17">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Video"}
</script>
<h3 class="hd hd-2">Video</h3>
<div
id="video_video_8017faacaf17"
class="video closed"
data-metadata='{"saveStateEnabled": false, "speed": null, "ytMetadataEndpoint": "", "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_8017faacaf17/handler/transcript/available_translations", "captionDataDir": null, "completionEnabled": false, "poster": null, "generalSpeed": 1.0, "transcriptLanguages": {"en": "English"}, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_8017faacaf17/handler/transcript/translation/__lang__", "start": 0.0, "end": 0.0, "savedVideoPosition": 0.0, "lmsRootURL": "https://openlearninglibrary.mit.edu", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_8017faacaf17/handler/xmodule_handler/save_user_state", "ytApiUrl": "https://www.youtube.com/iframe_api", "autoAdvance": false, "ytTestTimeout": 1500, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_8017faacaf17/handler/publish_completion", "showCaptions": "true", "autohideHtml5": false, "streams": "1.00:R4apUxTLO_k", "prioritizeHls": false, "duration": 183.49, "transcriptLanguage": "en", "autoplay": false, "completionPercentage": 0.95, "recordedYoutubeIsAvailable": true, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT600512016-V001700_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V001700/MIT600512016-V001700.m3u8"]}'
data-bumper-metadata='null'
data-autoadvance-enabled="False"
data-poster='null'
tabindex="-1"
>
<div class="focus_grabber first"></div>
<div class="tc-wrapper">
<div class="video-wrapper">
<span tabindex="0" class="spinner" aria-hidden="false" aria-label="Loading video player"></span>
<span tabindex="-1" class="btn-play fa fa-youtube-play fa-2x is-hidden" aria-hidden="true" aria-label="Play video"></span>
<div class="video-player-pre"></div>
<div class="video-player">
<div id="video_8017faacaf17"></div>
<h4 class="hd hd-4 video-error is-hidden">No playable video sources found.</h4>
<h4 class="hd hd-4 video-hls-error is-hidden">
Your browser does not support this video format. Try using a different browser.
</h4>
</div>
<div class="video-player-post"></div>
<div class="closed-captions"></div>
<div class="video-controls is-hidden">
<div>
<div class="vcr"><div class="vidtime">0:00 / 0:00</div></div>
<div class="secondary-controls"></div>
</div>
</div>
</div>
</div>
<div class="focus_grabber last"></div>
<h3 class="hd hd-4 downloads-heading sr" id="video-download-transcripts_video_8017faacaf17">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_8017faacaf17">
<div class="wrapper-download-transcripts">
<h4 class="hd hd-5">Transcripts</h4>
<ul class="list-download-transcripts">
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_8017faacaf17/handler/transcript/download" data-value="srt">Download SubRip (.srt) file</a>
</li>
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_8017faacaf17/handler/transcript/download" data-value="txt">Download Text (.txt) file</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_b461bed29f67">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_b461bed29f67">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<h2 id="smelly_example_3">Smelly Example #3</h2>
<div data-outline="smelly_example_3">
<p>Here's a third example of smelly code that will illustrate the remaining points of this reading.</p><pre><code class="hljs cpp"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> LONG_WORD_LENGTH = <span class="hljs-number">5</span>;
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String longestWord;
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">countLongWords</span><span class="hljs-params">(List<String> words)</span> </span>{
<span class="hljs-keyword">int</span> n = <span class="hljs-number">0</span>;
longestWord = <span class="hljs-string">""</span>;
<span class="hljs-keyword">for</span> (String word: words) {
<span class="hljs-keyword">if</span> (word.length() > LONG_WORD_LENGTH) ++n;
<span class="hljs-keyword">if</span> (word.length() > longestWord.length()) longestWord = word;
}
System.out.println(n);
}
</code></pre></div>
<h2 id="dont_use_global_variables">Don't Use Global Variables</h2>
<div data-outline="dont_use_global_variables">
<p>Avoid global variables. Let's break down what we mean by <em>global variable</em>. A global variable is:</p>
<ul>
<li>a <em>variable</em>, a name whose meaning can be changed</li>
<li>that is <em>global</em>, accessible and changeable from anywhere in the program.</li>
</ul>
<p><a href="http://c2.com/cgi/wiki?GlobalVariablesAreBad">Why Global Variables Are Bad</a> [<a href="http://web.archive.org/web/20160902115611/http://c2.com/cgi/wiki?GlobalVariablesAreBad">cached version</a>] has a good list of the dangers of global variables.</p>
<p>In Java, a global variable is declared <code>public static</code>. The <code>public</code> modifier makes it accessible anywhere, and <code>static</code> means there is a single instance of the variable.</p>
<p>In general, change global variables into parameters and return values, or put them inside objects that you're calling methods on. We'll see many techniques for doing that in future readings.</p>
</div>
<h2 id="methods_should_return_results_not_print_them">Methods Should Return Results, not Print Them</h2>
<div data-outline="methods_should_return_results_not_print_them">
<p><code>countLongWords</code> isn't ready for change. It sends some of its result to the console, <code>System.out</code>. That means that if you want to use it in another context — where the number is needed for some other purpose, like computation rather than human eyes — it would have to be rewritten.</p>
<p>In general, only the highest-level parts of a program should interact with the human user or the console. Lower-level parts should take their input as parameters and return their output as results. The sole exception here is debugging output, which can of course be printed to the console. But that kind of output shouldn't be a part of your design, only a part of how you debug your design.</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_f93ad28490d6">
<h2 class="hd hd-2 unit-title">Questions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<script src="/assets/courseware/v1/3e6443fb95285d2dc06e8ffc448b9bdf/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism.js"></script>
<script src="/assets/courseware/v1/2ba76f8bdfef4e2d45e31a3dc5208470/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/edx-script-v1.js"></script>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-use-global-variables">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-use-global-variables">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_02-dont-use-global-variables" class="problems-wrapper" role="group"
aria-labelledby="02-dont-use-global-variables-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-use-global-variables" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-use-global-variables/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="02-dont-use-global-variables-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-use-global-variables-problem-progress" tabindex="-1">
don&#39;t use global variables
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-use-global-variables-problem-progress"></div>
<div class="problem">
<div>
<pre class="code">
public static int LONG_WORD_LENGTH = 5;
public static String longestWord;
public static void countLongWords(List&lt;String&gt; words) {
int n = 0;
longestWord = "";
for (String word: words) {
if (word.length() &gt; LONG_WORD_LENGTH) ++n;
if (word.length() &gt; longestWord.length()) longestWord = word;
}
System.out.println(n);
}
</pre>
<p>In this code, which of these are global variables?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_02-dont-use-global-variables_2_1">
<fieldset aria-describedby="status_02-dont-use-global-variables_2_1">
<div class="field">
<input type="checkbox" name="input_02-dont-use-global-variables_2_1[]" id="input_02-dont-use-global-variables_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="02-dont-use-global-variables_2_1-choice_0-label" for="input_02-dont-use-global-variables_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_02-dont-use-global-variables_2_1"> countLongWords
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-dont-use-global-variables_2_1[]" id="input_02-dont-use-global-variables_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="02-dont-use-global-variables_2_1-choice_1-label" for="input_02-dont-use-global-variables_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_02-dont-use-global-variables_2_1"> n
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-dont-use-global-variables_2_1[]" id="input_02-dont-use-global-variables_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="02-dont-use-global-variables_2_1-choice_2-label" for="input_02-dont-use-global-variables_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_02-dont-use-global-variables_2_1"> LONG_WORD_LENGTH
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-dont-use-global-variables_2_1[]" id="input_02-dont-use-global-variables_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="02-dont-use-global-variables_2_1-choice_3-label" for="input_02-dont-use-global-variables_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_02-dont-use-global-variables_2_1"> longestWord
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-dont-use-global-variables_2_1[]" id="input_02-dont-use-global-variables_2_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="02-dont-use-global-variables_2_1-choice_4-label" for="input_02-dont-use-global-variables_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_02-dont-use-global-variables_2_1"> word
</label>
</div>
<div class="field">
<input type="checkbox" name="input_02-dont-use-global-variables_2_1[]" id="input_02-dont-use-global-variables_2_1_choice_5" class="field-input input-checkbox" value="choice_5"/><label id="02-dont-use-global-variables_2_1-choice_5-label" for="input_02-dont-use-global-variables_2_1_choice_5" class="response-label field-label label-inline" aria-describedby="status_02-dont-use-global-variables_2_1"> words
</label>
</div>
<span id="answer_02-dont-use-global-variables_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_02-dont-use-global-variables_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_02-dont-use-global-variables_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="don&#39;t use global variables" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_02-dont-use-global-variables" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_02-dont-use-global-variables">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-dont-use-global-variables-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-dont-use-global-variables-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-dont-use-global-variables-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-2" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-use-global-variables-7">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-use-global-variables-7">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_02-dont-use-global-variables-7" class="problems-wrapper" role="group"
aria-labelledby="02-dont-use-global-variables-7-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-use-global-variables-7" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-use-global-variables-7/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="5"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="02-dont-use-global-variables-7-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-use-global-variables-7-problem-progress" tabindex="-1">
don&#39;t use global variables, part 2
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@02-dont-use-global-variables-7-problem-progress"></div>
<div class="problem">
<div>
<p>Making a variable into a constant by adding the final keyword can eliminate the risk of global variables. What happens to each of these when the final keyword is added?</p>
<p>n</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="inputtype option-input ">
<select name="input_02-dont-use-global-variables-7_2_1" id="input_02-dont-use-global-variables-7_2_1" aria-describedby="status_02-dont-use-global-variables-7_2_1">
<option value="option_02-dont-use-global-variables-7_2_1_dummy_default">Select an option</option>
<option value="becomes constant"> becomes constant</option>
<option value="doesn't become constant"> doesn't become constant</option>
<option value="error before program starts"> error before program starts</option>
<option value="error while program is running"> error while program is running</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_02-dont-use-global-variables-7_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_02-dont-use-global-variables-7_2_1"/>
</div></div>
<div class="solution-span">
<span id="solution_02-dont-use-global-variables-7_solution_1"/>
</div><p>LONG_WORD_LENGTH</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 2" role="group"><div class="inputtype option-input ">
<select name="input_02-dont-use-global-variables-7_3_1" id="input_02-dont-use-global-variables-7_3_1" aria-describedby="status_02-dont-use-global-variables-7_3_1">
<option value="option_02-dont-use-global-variables-7_3_1_dummy_default">Select an option</option>
<option value="becomes constant"> becomes constant</option>
<option value="doesn't become constant"> doesn't become constant</option>
<option value="error before program starts"> error before program starts</option>
<option value="error while program is running"> error while program is running</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_02-dont-use-global-variables-7_3_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_02-dont-use-global-variables-7_3_1"/>
</div></div>
<div class="solution-span">
<span id="solution_02-dont-use-global-variables-7_solution_2"/>
</div><p>longestWord</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 3" role="group"><div class="inputtype option-input ">
<select name="input_02-dont-use-global-variables-7_4_1" id="input_02-dont-use-global-variables-7_4_1" aria-describedby="status_02-dont-use-global-variables-7_4_1">
<option value="option_02-dont-use-global-variables-7_4_1_dummy_default">Select an option</option>
<option value="becomes constant"> becomes constant</option>
<option value="doesn't become constant"> doesn't become constant</option>
<option value="error before program starts"> error before program starts</option>
<option value="error while program is running"> error while program is running</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_02-dont-use-global-variables-7_4_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_02-dont-use-global-variables-7_4_1"/>
</div></div>
<div class="solution-span">
<span id="solution_02-dont-use-global-variables-7_solution_3"/>
</div><p>word</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 4" role="group"><div class="inputtype option-input ">
<select name="input_02-dont-use-global-variables-7_5_1" id="input_02-dont-use-global-variables-7_5_1" aria-describedby="status_02-dont-use-global-variables-7_5_1">
<option value="option_02-dont-use-global-variables-7_5_1_dummy_default">Select an option</option>
<option value="becomes constant"> becomes constant</option>
<option value="doesn't become constant"> doesn't become constant</option>
<option value="error before program starts"> error before program starts</option>
<option value="error while program is running"> error while program is running</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_02-dont-use-global-variables-7_5_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_02-dont-use-global-variables-7_5_1"/>
</div></div>
<div class="solution-span">
<span id="solution_02-dont-use-global-variables-7_solution_4"/>
</div><p>words</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 5" role="group"><div class="inputtype option-input ">
<select name="input_02-dont-use-global-variables-7_6_1" id="input_02-dont-use-global-variables-7_6_1" aria-describedby="status_02-dont-use-global-variables-7_6_1">
<option value="option_02-dont-use-global-variables-7_6_1_dummy_default">Select an option</option>
<option value="becomes constant"> becomes constant</option>
<option value="doesn't become constant"> doesn't become constant</option>
<option value="error before program starts"> error before program starts</option>
<option value="error while program is running"> error while program is running</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_02-dont-use-global-variables-7_6_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_02-dont-use-global-variables-7_6_1"/>
</div></div>
<div class="solution-span">
<span id="solution_02-dont-use-global-variables-7_solution_5"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="don&#39;t use global variables, part 2" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_02-dont-use-global-variables-7" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_02-dont-use-global-variables-7">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-dont-use-global-variables-7-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-dont-use-global-variables-7-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="02-dont-use-global-variables-7-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@Reading_2_Summary">
<h2 class="hd hd-2 unit-title">Reading 2 Summary</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_ca2d90f877c1">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="0123a7f0684411ef8c8a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_ca2d90f877c1">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<h2 id="summary">Summary</h2>
<div data-outline="summary">
<p>Code review is a widely-used technique for improving software quality by human inspection. Code review can detect many kinds of problems in code, but as a starter, this reading talked about these general principles of good code:</p>
<ul>
<li>Don't Repeat Yourself (DRY)</li>
<li>Comments where needed</li>
<li>Fail fast</li>
<li>Avoid magic numbers</li>
<li>One purpose for each variable</li>
<li>Use good names</li>
<li>No global variables</li>
<li>Return results, don't print them</li>
<li>Use whitespace for readability</li>
</ul>
<p>The topics of today's reading connect to our three key properties of good software as follows:</p>
<ul>
<li>
<p><strong>Safe from bugs.</strong> In general, code review uses human reviewers to find bugs. DRY code lets you fix a bug in only one place, without fear that it has propagated elsewhere. Commenting your assumptions clearly makes it less likely that another programmer will introduce a bug. The Fail Fast principle detects bugs as early as possible. Avoiding global variables makes it easier to localize bugs related to variable values, since non-global variables can be changed in only limited places in the code.</p>
</li>
<li>
<p><strong>Easy to understand.</strong> Code review is really the only way to find obscure or confusing code, because other people are reading it and trying to understand it. Using judicious comments, avoiding magic numbers, keeping one purpose for each variable, using good names, and using whitespace well can all improve the understandability of code.</p>
</li>
<li>
<p><strong>Ready for change.</strong> Code review helps here when it's done by experienced software developers who can anticipate what might change and suggest ways to guard against it. DRY code is more ready for change, because a change only needs to be made in one place. Returning results instead of printing them makes it easier to adapt the code to a new purpose.</p>
</li>
</ul>
<hr>
</div>
<div class="license">This reading was collaboratively authored with contributions from: Saman Amarasinghe, Adam Chlipala, Srini Devadas, Michael Ernst, Max Goldman, John Guttag, Daniel Jackson, Rob Miller, Martin Rinard, and Armando Solar-Lezama. This work is licensed under <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">CC BY-SA 4.0</a>.</div>
</div>
</div>
</div>
</div>