IDEA сплошное подчеркивание серым — что это? [дубликат]

Что означает такое вот подчеркивание?
Отслеживать
задан 10 янв 2019 в 15:44
user224616 user224616
По поводу вашей правки: нет, не «могут изменяться», а именно что изменяются. В показанном вами коде requestLocale тоже «может изменяться», но не подчёркивается, потому что не изменяется
12 янв 2019 в 15:05
Я имел в виду, если if -ы не сработают, то locale не изменится, т.е. именно МОГУТ изменяться, а не изменяются
– user224616
12 янв 2019 в 15:12
Ваша интерпретация интересена, но, подозреваю, для анализатора кода не применима: с его точки зрения, если есть locale = — значит считаем за изменение, даже если завернуть в if (false) и реального изменения никогда не произойдёт — всё равно подчеркнётся (я не проверял, но подозреваю, что так и будет, проверьте на всякий случай)
Notification saying «Reassigned local variable» in Android Studio
My string is getting underlined, with a notification saying «Reassigned local variable» . I have worked out that this happens any time that the string is modified, but I am not sure why this notification is coming up. It doesn’t appear do be causing any errors, and nothing is showing under the «problems» tab, so I’m not sure if it is something I should change or if I should just ignore it. Below is a simplified version of the code.
Canvas canvas; Paint p = new Paint(); String value = "a"; //'value' here becomes underlined if(someCondition()) value += "bcdefghijklmnop"; //'value' here becomes underlined canvas.drawText(value, 50, 50, p);
asked Dec 22, 2022 at 22:24
29 1 1 silver badge 7 7 bronze badges
Whats the question? Do you want to avoid re-assigning the local variable or remove the notification?
Dec 22, 2022 at 22:29
1 Answer 1
It is just a notification, probably because sometimes reassigned local variables can cause problems for later operations. In your case it is totally legit to reassign, just be aware that it will create several new objects.
If you would like to prevent this, you could work with a StringBuilder here that you convert to a String whenever necessary, but this also creates a new String object, so if you don’t use this in a loop usually it is not necessary to change anything (and for the string manipulation in a loop you probably will get a warning, a stronger sign for a potential problem in your code than this actual notification).
answered Dec 22, 2022 at 22:55
cyberbrain cyberbrain
3,726 1 1 gold badge 13 13 silver badges 24 24 bronze badges
- java
- android-studio
-
The Overflow Blog
Related
Hot Network Questions
Subscribe to RSS
Question feed
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2024 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2024.1.3.2953
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Reassign parameter to local variable
Is this just some cargo-cult practice that some programming teachers pass on, or are there coding style guidelines that actually recommend it? The only justification I can think of for this would be in languages that pass parameters by reference, if the function needs to reassign the variable without affecting the caller’s variable. Is this a legacy of instructors who grew up learning Fortran, which passes all parameters by reference? Most other languages with by-reference parameters require them to be declared explictly, as in C++’s type ¶meter declaration, and in these cases it’s usually desirable to modify the caller’s variable. This style of coding for variables that are reassigned is discussed in Is there a reason to not modify values of parameters passed by value?. But I also see this in functions that only read the variable. For instance, this question has a function like this:
function say(messages = []) < let alphabets = []; let textLines = messages console.log(textLines.length) textLines.forEach((line, i) =>< if (i < textLines.length - 1) line.text += " "; // more code >); >);
It could just as easily be written with textLines as the parameter name.
asked Oct 5, 2022 at 18:46
324 2 2 silver badges 5 5 bronze badges
2 Answers 2
It will happen if you want a different name for the parameter at the caller side and for the argument on the callee side. The developer making the call and the developer implementing the function can have different views of things.
Another situation are parameters being references and you don’t want to unintentionally modify the original, or read-only, or you want both a modified value and the original value.
So there are cases where it makes sense and cases where it doesn’t. If you see it, probably not worth changing.
answered Oct 6, 2022 at 10:27
gnasher729 gnasher729
43.9k 4 4 gold badges 61 61 silver badges 125 125 bronze badges
But we’re talking about exercise code, not code implementing an API with pre-ordained parameter names. The programmer gets to decide what the variables are named.
Oct 6, 2022 at 13:41
Is this just some cargo-cult practice that some programming teachers pass on,
There are millions of teachers all over the world, so how shall we know?
or are there coding style guidelines that actually recommend it?
I have never seen one, but the situation is not different than the former: there are too many coding standards all over the world to answer this.
Hence I think it is impossible to answer this generically «for all questions on SO which use this style» in a sensible manner. One has to look at each individual case, review the code or ask the authors for their reasons. So please do yourself a favor and stop asking questions which wrongly assume the existence of some overgeneralizated answer.
In your specific example, however, I guess the two different names simply reflect the different points of view of the interface vs. implementation:
- on the caller’s side, the function signature is just say(messages) . The name messages just gives the caller a hint what the parameter should contain.
- from the implementors’s point of view, those messages may be interpreted as text lines, so they will be given a different name. The primary context is here the internal agorithm. This is hidden from the caller, since it is an implementation detail.
So introducing a alias name can make sense in certain situations. However, for other situations there might be different reasons, or it is quite unneccessary — one has to look at each individual case.
Saved searches
Use saved searches to filter your results more quickly
Cancel Create saved search
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
dart-lang / sdk Public
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support highlighting reassigned local variables in IntelliJ #27842
davidmorgan opened this issue Nov 17, 2016 · 23 comments
Support highlighting reassigned local variables in IntelliJ #27842
davidmorgan opened this issue Nov 17, 2016 · 23 comments
area-analyzer area-intellij P3 A lower priority bug or feature request type-enhancement A request for a change that isn’t a bug
Comments
Contributor
davidmorgan commented Nov 17, 2016
For Java, IntelliJ offers color/font/highlighting for local variables based on whether they are ever reassigned.
(Settings->Editor->Colors & Fonts->Java, Variables->Local variable, Variables->Reassigned local variable).
Please add equivalent support for Dart.
Reasoning: some (me included!) currently write «final» instead of «var» where possible to highlight what can be reassigned. The IDE should be configurable to do this for us, so we can just write «var» everywhere.
The text was updated successfully, but these errors were encountered:
bwilkerson added the P2 A bug or feature request we’re likely to work on label Nov 22, 2016
Contributor Author
davidmorgan commented Feb 1, 2017
Any ideas if/when this might happen, please?
Alternatively, if someone can point me in the right direction I might take a shot at it, since I seem to be the person who wants it most 🙂
mit-mit commented Feb 1, 2017
dave26199 commented Feb 26, 2017
Any pointers please? If this is easy to implement I’d like to take a look.
mit-mit commented Feb 26, 2017
@devoncarew @pq @stevemessick can any of you offer some pointers for how to about this?
devoncarew commented Feb 26, 2017
I think this would qualify as semantic highlighting — highlighting info decided on in the analysis server and sent over to IntelliJ. See hereabouts for the spec: https://htmlpreview.github.io/?https://github.com/dart-lang/sdk/blob/master/pkg/analysis_server/doc/api.html#type_HighlightRegionType
@scheglov has the most experience with this on the analysis server side and @alexander-doroshko on the IntelliJ side.
Contributor
scheglov commented Feb 27, 2017
See the semantic highlighting computer.
Because in general adding new highlighting types is not backward compatible, you will need to create another copy and activate it only when —useAnalysisHighlight3 is passed to the Analysis Server.
Contributor Author
davidmorgan commented Mar 10, 2017
Is there a guide somewhere to working on the analyzer code please? I tried downloading the SDK from github, and running «dart test/integration/analysis/highlights_test2.dart»; but I get errors about missing import «analyzer-0.28.1/lib/dart/ast/standard_resolution_map.dart» — no idea what’s going on here.
If I can get it running I guess the code change isn’t too hard. How does adding a new highlight type work from the IntelliJ side, please? Is there work needed there to support a new type / to pass —useAnalysisHighlight3?
Contributor
scheglov commented Mar 10, 2017
I think you ran pub get in one of the packages. You should not do this. SDK already comes with the .packages file, which is used to keep packages compatible with each other and third_party .
Yes, you need to change the Dart plugin for IDEA. You need to add new identifiers to the configuration, and use them to process semantic highlighting from Analysis Server.
Contributor Author
davidmorgan commented Mar 10, 2017 •
That was it, thanks. Removing the wrong .packages allowed me to run the test.
Thanks for the plugin links.
bwilkerson commented Mar 10, 2017
If we’re going to add a useAnalysisHighlight3 option, then I want us to spend some time thinking about whether there are other highlighting changes we would like to add. I don’t want to add a new flag every time we add a single highlighting kind.
@jwren @devoncarew @alexander-doroshko Can you think of any additional highlighting information we’d like to have server report (even if we don’t have time to implement it now)?
alexander-doroshko commented Mar 10, 2017
I’ve found 2 more requests submitted by @zoechi:
Contributor Author
davidmorgan commented Mar 13, 2017
Implementation question — I’m sure this is obvious to anyone used to working on AST visitors 🙂
Highlighting reassigned locals is going to need some kind of lookahead — because we need to highlight it where it’s declared/used based on what happens later.
Is there a standard pattern for doing this? I guess something like:
- Visitor that builds a list of reassigned identifiers
- Whenever we enter a scope (method declaration, constructor declaration, . ) call this visitor and store the results . where? In DartUnitHighlightsComputer3?
bwilkerson commented Mar 13, 2017
I’m sure this is obvious to anyone used to working on AST visitors
I wish that were true 🙂
I don’t know that it’s best, but I would probably extend _DartUnitHighlightsComputerVisitor3 to keep track of the current local variable scope, allowing scopes to nest. (There isn’t a class that will do exactly what you need, so you’d have to write this yourself.) Each scope would remember the variables defined in that scope, and for each variable the information you need in order to produce highlighting information (such as the offset of the variable and whether it was re-assigned). You can then produce the highlighting regions when exiting a scope (because you know that you’ve now seen everything you need to see about the variables).
Contributor
scheglov commented Mar 13, 2017
We might already have information you need.
See potentiallyMutatedInScope.
Contributor Author
davidmorgan commented Mar 14, 2017
@scheglov Thanks! That does indeed make it very easy to implement.
I realized that to try it I can use one of the other highlights that I don’t need — dynamic locals — to avoid having to change the protocol. So, currently giving this highlight a try to confirm that it’s useful enough to be worth all the hassle. will also see if others are interested.
Contributor Author
davidmorgan commented Mar 21, 2017
User interface question: it looks like we’re doing to have to double up the existing set of «local variable» highlights, leading to 8 highlights for them:
(reassigned/not) x (dynamic/not) x (declaration/reference) local variable
One alternative that comes to mind is we put the highlight on the symbol before the variable name, i.e. on the «var» or the type annotation. Then we’d have one new highlight: «type annotation (or var) of reassigned local». I’d have to try this to see if it can be implemented in a reasonable way — and if it’s as nice to use.
Any other suggestions? Or should we just live with eight «local variable» highlights?
bwilkerson commented Mar 21, 2017
Personally, I don’t think I’d like having type annotations highlighted differently based on whether the variable being defined is reassigned, but I’d have to see it to know for sure.
I will point out that Dart allows you to declare multiple variables in any given declaration
var x, y, z;
and therefore the keyword / type annotation isn’t always associated with a single variable, which could lead to confusion. How would you highlight the above if x and z are only assigned once, but y is reassigned?