when use type inference? we can always use VAR?

Guglielmo Astro

The type inference VAR,serves only to be recognized by the program only if he has to go to replace "int "string" etc.? If yes,would it not be better to always use VAR?

Claies

There are pros and cons to using the var keyword.

What are the benefits of using var

  • Dont Repeat Yourself (DRY) – Redundant code should be avoided.
  • Faster creation of code
  • Calls for improved naming of variables – can work as a reminder for descriptive naming
  • Improved readability – in some cases (where the repeated type is long and complex)
  • Less code modification if you later need to change the type
  • Clearer distinction of when you really want to specify the type

The disadvantages of using var

  • Loss of readability – in some cases (where the type isn’t obvious)
  • Changes to the type could introduce bugs which the compiler otherwise would have caught for the developer

+ Good:

  • var numbers = new int[] {1, 2, 3, 4};
  • var stringbuilder = new StringBuilder();
  • var cars = new List();
  • var orders = new Dictionary();

+/- OK with either (but prefer explicit declaration):

  • int pages = 10;
  • string username = “john”;
  • var username = “john”;
  • var order = GetOrder(orderId); // ok if the type is Order, otherwise not
  • for (var x = 1; x < 10; x++)

- Bad:

  • var settings = GetInboxSettings(); // not obvious at all
  • var userId = GetUserId(); // ambigous, is this guid, string, int or a custom UserId object?
  • Dictionary orders = new Dictionary(); // redundant

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

When can we use async?

From Dev

When do we use $scope and when do we we use var in AngularJS?

From Dev

When do we use $scope and when do we we use var in AngularJS?

From Dev

Can we force perl always use three argument form of open when running from command line?

From Dev

Use of var keyword when data type is known

From Dev

Use of var keyword when data type is known

From Dev

can we use empty parameter with pointer and when?

From Dev

Can we use dbCommand and AddInParameter while inserting always encrypted columns

From Dev

How to/Can we apply a "limit" when we use replication in Cloudant?

From Dev

Can we use inline template without using any type of routing?

From Dev

Can we use macros to statically dispatch on a return type in Clojure?

From Dev

How can we use a variable of the type of the template parameter?

From Dev

How can we use a machine learning algorithm on this type of data?

From Dev

How can we use a variable of the type of the template parameter?

From Dev

Can we use some other type of time picker?

From Dev

Can we use macros to statically dispatch on a return type in Clojure?

From Dev

Can we use register interpolator twice on the same type

From Dev

No suitable constructor found for type when we use an Enum - Jackson JSON

From Dev

How to use selector when we swap on piano type buttons in android

From Dev

No suitable constructor found for type when we use an Enum - Jackson JSON

From Dev

Why do we use UserManager to acess user profile info when we can use application dbcontext?

From Dev

Why do we use pthread_exit() when we can use return?

From Dev

Should we always use `override` in Trait

From Dev

Should we always use @NotNull or @Nullable?

From Dev

When can we use an identifier number instead of its name in PostgreSQL?

From Dev

Can we use a CASE...WHEN inside SUBSTRING?

From Dev

Can we use sstableloader when the cluster is in partially upgraded state?

From Dev

generics benefit when we can use parent object

From Dev

Java var and inference type ambiguity

Related Related

  1. 1

    When can we use async?

  2. 2

    When do we use $scope and when do we we use var in AngularJS?

  3. 3

    When do we use $scope and when do we we use var in AngularJS?

  4. 4

    Can we force perl always use three argument form of open when running from command line?

  5. 5

    Use of var keyword when data type is known

  6. 6

    Use of var keyword when data type is known

  7. 7

    can we use empty parameter with pointer and when?

  8. 8

    Can we use dbCommand and AddInParameter while inserting always encrypted columns

  9. 9

    How to/Can we apply a "limit" when we use replication in Cloudant?

  10. 10

    Can we use inline template without using any type of routing?

  11. 11

    Can we use macros to statically dispatch on a return type in Clojure?

  12. 12

    How can we use a variable of the type of the template parameter?

  13. 13

    How can we use a machine learning algorithm on this type of data?

  14. 14

    How can we use a variable of the type of the template parameter?

  15. 15

    Can we use some other type of time picker?

  16. 16

    Can we use macros to statically dispatch on a return type in Clojure?

  17. 17

    Can we use register interpolator twice on the same type

  18. 18

    No suitable constructor found for type when we use an Enum - Jackson JSON

  19. 19

    How to use selector when we swap on piano type buttons in android

  20. 20

    No suitable constructor found for type when we use an Enum - Jackson JSON

  21. 21

    Why do we use UserManager to acess user profile info when we can use application dbcontext?

  22. 22

    Why do we use pthread_exit() when we can use return?

  23. 23

    Should we always use `override` in Trait

  24. 24

    Should we always use @NotNull or @Nullable?

  25. 25

    When can we use an identifier number instead of its name in PostgreSQL?

  26. 26

    Can we use a CASE...WHEN inside SUBSTRING?

  27. 27

    Can we use sstableloader when the cluster is in partially upgraded state?

  28. 28

    generics benefit when we can use parent object

  29. 29

    Java var and inference type ambiguity

HotTag

Archive