GithubHelp home page GithubHelp logo

Comments (12)

epong avatar epong commented on June 17, 2024 40

The point of a common style is that it overrides personal preference.

This isn't just a question of style, though. These examples are functionally different. The snippets by @jgroeneveld and @jriquelme limit the scope of variables to the if block. The linter enforces polluting the parent scope.

At any rate, there is not even any indication that your example is the "usual Go style". Just see Go's own tutorial on flow control: https://tour.golang.org/flowcontrol/7.

func pow(x, n, lim float64) float64 {
	if v := math.Pow(x, n); v < lim {
		return v
	} else {
		fmt.Printf("%g >= %g\n", v, lim)
	}
	// can't use v here, though
	return lim
}

Please reconsider.

from lint.

dsymonds avatar dsymonds commented on June 17, 2024 16

I think it's fine. The usual Go style for that is

func get(i int) int {
    f := i * 2
    if f == 6 {
        return f
    }
    return f * 3
}

from lint.

jgroeneveld avatar jgroeneveld commented on June 17, 2024 6

This is actually a nice pattern to retrieve a value, evaluate the error, return on error or assign the value if there is no error while protecting the namespace.

I see why this is rule exists in the usual cases but when using a local var I was surprised that golint is reporting it.

[...] some code
    if loc, err := store.Location(); err != nil {
        return nil, false
    } else {
        doc.Location = loc.ToArray()
    }

// at this point there is no var loc anymore, its only used in the else case
[...] some code

from lint.

yurishkuro avatar yurishkuro commented on June 17, 2024 4

@dsymonds However, golint readme also says:

If you find an established style that is frequently violated, and which you think golint could statically check, file an issue.

from lint.

siraj-mansour-deltatre avatar siraj-mansour-deltatre commented on June 17, 2024 2

This is quite noisy and promotes scope leaking, i am highly for re-opening this issue and changing the rules.

from lint.

attilaolah avatar attilaolah commented on June 17, 2024

@dsymonds you are referring to the third code example in this section? Looks like I misinterpreted it then. I was just about to open a duplicate issue.

Thanks for pointing that out.

from lint.

dsymonds avatar dsymonds commented on June 17, 2024

Yes, it's that one.

from lint.

jriquelme avatar jriquelme commented on June 17, 2024

Same opinion as @jgroeneveld here, I have many complains of golink for things like:

if nid, err := att(node, "nid"); err != nil {
    return nil, err
} else {
    deliveryMsg.Nid = parseNid(nid)
}

I prefer this form for this particular case (doesn't pollute the scope with "temporary" variables).

from lint.

dsymonds avatar dsymonds commented on June 17, 2024

The point of a common style is that it overrides personal preference.

from lint.

gordallott avatar gordallott commented on June 17, 2024

for anyone coming to this via google search and finding this dead end, i forked and removed this poor decision. just go get that version instead and enjoy less scope leaking

https://github.com/gordallott/lint

from lint.

l0k18 avatar l0k18 commented on June 17, 2024

I have discovered that variables declared inside the first part of an if keyword's block, the if <statement>; <boolean> { ... }that these variables fall out of scope after the if block, but remain in scope inside subsequent else blocks attached to the main if block. For these cases, you have two options, to pre-declare the variable in the parent scope or you have to perform all the operations on the variables within following else blocks.

I am in two minds about it, I kinda came to it with the expectation that these variables were bound to the parent scope but they are not. Conforming to what golint says requires considerably more predeclarations of variables, so I just ignore the green squiggle indicating golint's warning. I don't know whether it would be better if the scoping of declarations inside the if statement parameters were bound to the parent, or if it is better to leave it as is and perhaps have golint not give these warnings if the declared variables are used within the direct or deeper descendant, but if it were changed it would break a lot of the code I am working on at the moment.

It's a very trivial issue and taking the conservative, idiomatic way of looking at it, that the grammar should not be changed as it doesn't provide significant expressivity, and could potentially break a lot of code (in a relatively trivial way), it would only reduce some of the wordiness of these constructions, and for me having to put else blocks isn't a terribly onerous burden compared to not having to predeclare.

from lint.

jkassis avatar jkassis commented on June 17, 2024

keep the scope clean. help me inline my if blocks. too much err != nil in my face.

from lint.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.