37

As fields are implicitly private, why there is often explicit declaraion used in the books, articles etc.?

Vitor M. Barbosa
  • 3,286
  • 1
  • 24
  • 36
Loj
  • 1,159
  • 3
  • 14
  • 23
  • Also see [Should you use the private access modifier if it's redundant?](https://stackoverflow.com/questions/254912/should-you-use-the-private-access-modifier-if-its-redundant) – Franklin Yu Sep 08 '17 at 15:04

8 Answers8

78

Because default access levels vary across languages, and many people program in more than one language. It's easy to become confused, either as the author or as someone reading the code later, thus explicit is nicer to deal with than implicit.

Donnie
  • 45,732
  • 10
  • 64
  • 86
25

The problem with implicit declarations is that the reader cannot tell if whoever wrote the code wanted the implicit declaration or simply forgot to write anything. By being explicit there's no doubt about the intentions.

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
  • Totally agree with you. It's same as cardinality in UML database diagram; without specifying it, you cannot know if it's because the author wants the default value or if he forgot to define it. – Samuel Dec 06 '16 at 13:55
12

To make your code look nice :).

You're right it's not necessary, but it's custom to write them anyway. At the very least, every method has a privilege explicitly noted and it makes your code easier to read.

Pieter van Ginkel
  • 29,160
  • 8
  • 71
  • 111
8

Sometimes explicit is better than implicit, and this is even more so when you are writing educational material. For people who do not know or cannot remember the rules for the default access levels it is one less thing for them to be concerned with when reading the code.

Related Question

The default access for everything in C# is "the most restricted access you could declare for that member".

Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
5

Because you write code for maintainability and clarity, ESPECIALLY in code samples. Implicit declarations are there for the compiler, not for the programmer. Failing to explicitly declare the visibility and scope of your variables leaves your intent ambiguous. Is it really that much extra typing?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
2

You have to remember that code can end up with someone else to reading it at some point, it might be you in 6 months and you need to understand the intent. Declaring something private means that you are not wanting that particular implementation detail to be available to all those who may use it(at this point in time), later revisions may change the way that particular thing works and if you wish to provide backward compatibility, if it's been public from the begining, it needs to remain in future revisions.

Paul Farry
  • 4,730
  • 2
  • 35
  • 61
1

Explicit declaration used to told you that the variable is deliberately set to private and it needs to be declare as private

Muhammad Faizan Khan
  • 10,013
  • 18
  • 97
  • 186
1

In my opinion it makes the code better readable. I don't have to think about the default access modifier. It's also enforced by StyleCop, a tool I use to ensure a consistent coding style.

treze
  • 3,159
  • 20
  • 21