Intellij hint condition is always true - how does it know?
Students were given an exercise to practice basic software development practices and I decided to run through it myself to see how it would go. Specifically, writing a program that converts an integer (i.e. unix timestamp) into a formatted string. Practicing TDD is part of the exercise so I've done that myself.
In other words, instead of trying to make the currently failing test pass elegantly, I'm trying to make it pass as simply as possible, and finding refactoring opportunities as they arise.
I am doing this in Java 11 with Intellij IDEA.
This is the state I am currently in. The timestamp for "1970-03-01 00:00:00" incorrectly returned "1970-02-29 00:00:00" so I made appropriate adjustments. I had similar issues with the timestamps for March 2 and March 3, and the naive fix is what's in the code below.
public MyTime(int i) {
timestamp = i;
months = (timestamp / (31*86400) % 12) + 1;
days = (timestamp / 86400 % 31) + 1;
hours = timestamp / 3600 % 24;
minutes = timestamp / 60 % 60;
seconds = timestamp % 60;
if (months == 2 && days > 28) {
months = 3;
if (days == 29) {
days = 1;
} else if (days == 30) {
days = 2;
} else if (days == 31) {
days = 3;
}
}
}
The obvious refactor here is days -= 28
, which hints at a more generic daysInMonth()
for later... All the divisions and modulo stuff needs work, too.
My actual question is about the else if (days == 31)
statement. Intellij gives me a warning that this condition is always true. I mean, it is, because days is (x % 31) + 1
but this seems like a really complicated thing for an IDE to figure out.
Does anyone know what it's doing under the hood?
intellij-idea
add a comment |
Students were given an exercise to practice basic software development practices and I decided to run through it myself to see how it would go. Specifically, writing a program that converts an integer (i.e. unix timestamp) into a formatted string. Practicing TDD is part of the exercise so I've done that myself.
In other words, instead of trying to make the currently failing test pass elegantly, I'm trying to make it pass as simply as possible, and finding refactoring opportunities as they arise.
I am doing this in Java 11 with Intellij IDEA.
This is the state I am currently in. The timestamp for "1970-03-01 00:00:00" incorrectly returned "1970-02-29 00:00:00" so I made appropriate adjustments. I had similar issues with the timestamps for March 2 and March 3, and the naive fix is what's in the code below.
public MyTime(int i) {
timestamp = i;
months = (timestamp / (31*86400) % 12) + 1;
days = (timestamp / 86400 % 31) + 1;
hours = timestamp / 3600 % 24;
minutes = timestamp / 60 % 60;
seconds = timestamp % 60;
if (months == 2 && days > 28) {
months = 3;
if (days == 29) {
days = 1;
} else if (days == 30) {
days = 2;
} else if (days == 31) {
days = 3;
}
}
}
The obvious refactor here is days -= 28
, which hints at a more generic daysInMonth()
for later... All the divisions and modulo stuff needs work, too.
My actual question is about the else if (days == 31)
statement. Intellij gives me a warning that this condition is always true. I mean, it is, because days is (x % 31) + 1
but this seems like a really complicated thing for an IDE to figure out.
Does anyone know what it's doing under the hood?
intellij-idea
add a comment |
Students were given an exercise to practice basic software development practices and I decided to run through it myself to see how it would go. Specifically, writing a program that converts an integer (i.e. unix timestamp) into a formatted string. Practicing TDD is part of the exercise so I've done that myself.
In other words, instead of trying to make the currently failing test pass elegantly, I'm trying to make it pass as simply as possible, and finding refactoring opportunities as they arise.
I am doing this in Java 11 with Intellij IDEA.
This is the state I am currently in. The timestamp for "1970-03-01 00:00:00" incorrectly returned "1970-02-29 00:00:00" so I made appropriate adjustments. I had similar issues with the timestamps for March 2 and March 3, and the naive fix is what's in the code below.
public MyTime(int i) {
timestamp = i;
months = (timestamp / (31*86400) % 12) + 1;
days = (timestamp / 86400 % 31) + 1;
hours = timestamp / 3600 % 24;
minutes = timestamp / 60 % 60;
seconds = timestamp % 60;
if (months == 2 && days > 28) {
months = 3;
if (days == 29) {
days = 1;
} else if (days == 30) {
days = 2;
} else if (days == 31) {
days = 3;
}
}
}
The obvious refactor here is days -= 28
, which hints at a more generic daysInMonth()
for later... All the divisions and modulo stuff needs work, too.
My actual question is about the else if (days == 31)
statement. Intellij gives me a warning that this condition is always true. I mean, it is, because days is (x % 31) + 1
but this seems like a really complicated thing for an IDE to figure out.
Does anyone know what it's doing under the hood?
intellij-idea
Students were given an exercise to practice basic software development practices and I decided to run through it myself to see how it would go. Specifically, writing a program that converts an integer (i.e. unix timestamp) into a formatted string. Practicing TDD is part of the exercise so I've done that myself.
In other words, instead of trying to make the currently failing test pass elegantly, I'm trying to make it pass as simply as possible, and finding refactoring opportunities as they arise.
I am doing this in Java 11 with Intellij IDEA.
This is the state I am currently in. The timestamp for "1970-03-01 00:00:00" incorrectly returned "1970-02-29 00:00:00" so I made appropriate adjustments. I had similar issues with the timestamps for March 2 and March 3, and the naive fix is what's in the code below.
public MyTime(int i) {
timestamp = i;
months = (timestamp / (31*86400) % 12) + 1;
days = (timestamp / 86400 % 31) + 1;
hours = timestamp / 3600 % 24;
minutes = timestamp / 60 % 60;
seconds = timestamp % 60;
if (months == 2 && days > 28) {
months = 3;
if (days == 29) {
days = 1;
} else if (days == 30) {
days = 2;
} else if (days == 31) {
days = 3;
}
}
}
The obvious refactor here is days -= 28
, which hints at a more generic daysInMonth()
for later... All the divisions and modulo stuff needs work, too.
My actual question is about the else if (days == 31)
statement. Intellij gives me a warning that this condition is always true. I mean, it is, because days is (x % 31) + 1
but this seems like a really complicated thing for an IDE to figure out.
Does anyone know what it's doing under the hood?
intellij-idea
intellij-idea
asked Nov 25 '18 at 19:14
Andrew PearceAndrew Pearce
141
141
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
IntelliJ IDEA can perform pretty clever dataflow analyse. If you are unsure that this part was marked as always true because of (x % 31) + 1
, you can increment 31 up to 32 and the warning will disappear.
Here is blockpost with some examples of always true and always false inspection: https://blog.jetbrains.com/idea/2018/01/fumigating-the-idea-ultimate-code-using-dataflow-analysis/
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470981%2fintellij-hint-condition-is-always-true-how-does-it-know%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
IntelliJ IDEA can perform pretty clever dataflow analyse. If you are unsure that this part was marked as always true because of (x % 31) + 1
, you can increment 31 up to 32 and the warning will disappear.
Here is blockpost with some examples of always true and always false inspection: https://blog.jetbrains.com/idea/2018/01/fumigating-the-idea-ultimate-code-using-dataflow-analysis/
add a comment |
IntelliJ IDEA can perform pretty clever dataflow analyse. If you are unsure that this part was marked as always true because of (x % 31) + 1
, you can increment 31 up to 32 and the warning will disappear.
Here is blockpost with some examples of always true and always false inspection: https://blog.jetbrains.com/idea/2018/01/fumigating-the-idea-ultimate-code-using-dataflow-analysis/
add a comment |
IntelliJ IDEA can perform pretty clever dataflow analyse. If you are unsure that this part was marked as always true because of (x % 31) + 1
, you can increment 31 up to 32 and the warning will disappear.
Here is blockpost with some examples of always true and always false inspection: https://blog.jetbrains.com/idea/2018/01/fumigating-the-idea-ultimate-code-using-dataflow-analysis/
IntelliJ IDEA can perform pretty clever dataflow analyse. If you are unsure that this part was marked as always true because of (x % 31) + 1
, you can increment 31 up to 32 and the warning will disappear.
Here is blockpost with some examples of always true and always false inspection: https://blog.jetbrains.com/idea/2018/01/fumigating-the-idea-ultimate-code-using-dataflow-analysis/
answered Nov 26 '18 at 8:09
Genetic ForestGenetic Forest
1,98131526
1,98131526
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470981%2fintellij-hint-condition-is-always-true-how-does-it-know%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown