Bob Martin: “C has perfect encapsulation” HOW?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Bob Martin in this video says that "C has perfect encapsulation". I do not understand why he is saying this... I understand that we can separate the implementation in a .c file and declare the interface in a header .h file, but there is nothing really stopping me from accessing implementation details like this:
main.c
#include <stdio.h>
#include "file1.h"
extern int x;
int main() {
printf("%dn", x);
return 0;
}
file1.c
int x = 5;
int getnum_file1() {
return x + 1;
}
file1.h
int getnum_file1();
In this case, main.c has access to implementation detail in file1.c. This code also compiles and executes with the expected result. How is this perfect encapsulation??
c oop encapsulation software-design
|
show 1 more comment
Bob Martin in this video says that "C has perfect encapsulation". I do not understand why he is saying this... I understand that we can separate the implementation in a .c file and declare the interface in a header .h file, but there is nothing really stopping me from accessing implementation details like this:
main.c
#include <stdio.h>
#include "file1.h"
extern int x;
int main() {
printf("%dn", x);
return 0;
}
file1.c
int x = 5;
int getnum_file1() {
return x + 1;
}
file1.h
int getnum_file1();
In this case, main.c has access to implementation detail in file1.c. This code also compiles and executes with the expected result. How is this perfect encapsulation??
c oop encapsulation software-design
Just because a language has encapsulation facilities doesn't mean a programmer can't fail to encapsulate.
– StoryTeller
Nov 22 '18 at 8:47
Because file1 is breaking encapsulation by defining a (potential) global variable?
– GermanNerd
Nov 22 '18 at 8:48
c and c++ can be used and abused its really up to the developer to employ good practice. You can do really nasty things in both languages, even Java lets you do some pretty awful things, for example its perfectly possible to define a static public member variable which is globally accessible. In your example, remove extern int x, changed x in file1.c to be static.
– SPlatten
Nov 22 '18 at 8:49
Ah I see.. so is the best practice to attach the 'static' keyword to any variable you would like to keep hidden as implementation detail?
– N. Parikh
Nov 22 '18 at 8:56
@N.Parikh, static just restricts the variable to the scope it is declared in.
– SPlatten
Nov 22 '18 at 9:02
|
show 1 more comment
Bob Martin in this video says that "C has perfect encapsulation". I do not understand why he is saying this... I understand that we can separate the implementation in a .c file and declare the interface in a header .h file, but there is nothing really stopping me from accessing implementation details like this:
main.c
#include <stdio.h>
#include "file1.h"
extern int x;
int main() {
printf("%dn", x);
return 0;
}
file1.c
int x = 5;
int getnum_file1() {
return x + 1;
}
file1.h
int getnum_file1();
In this case, main.c has access to implementation detail in file1.c. This code also compiles and executes with the expected result. How is this perfect encapsulation??
c oop encapsulation software-design
Bob Martin in this video says that "C has perfect encapsulation". I do not understand why he is saying this... I understand that we can separate the implementation in a .c file and declare the interface in a header .h file, but there is nothing really stopping me from accessing implementation details like this:
main.c
#include <stdio.h>
#include "file1.h"
extern int x;
int main() {
printf("%dn", x);
return 0;
}
file1.c
int x = 5;
int getnum_file1() {
return x + 1;
}
file1.h
int getnum_file1();
In this case, main.c has access to implementation detail in file1.c. This code also compiles and executes with the expected result. How is this perfect encapsulation??
c oop encapsulation software-design
c oop encapsulation software-design
asked Nov 22 '18 at 8:45
N. ParikhN. Parikh
555
555
Just because a language has encapsulation facilities doesn't mean a programmer can't fail to encapsulate.
– StoryTeller
Nov 22 '18 at 8:47
Because file1 is breaking encapsulation by defining a (potential) global variable?
– GermanNerd
Nov 22 '18 at 8:48
c and c++ can be used and abused its really up to the developer to employ good practice. You can do really nasty things in both languages, even Java lets you do some pretty awful things, for example its perfectly possible to define a static public member variable which is globally accessible. In your example, remove extern int x, changed x in file1.c to be static.
– SPlatten
Nov 22 '18 at 8:49
Ah I see.. so is the best practice to attach the 'static' keyword to any variable you would like to keep hidden as implementation detail?
– N. Parikh
Nov 22 '18 at 8:56
@N.Parikh, static just restricts the variable to the scope it is declared in.
– SPlatten
Nov 22 '18 at 9:02
|
show 1 more comment
Just because a language has encapsulation facilities doesn't mean a programmer can't fail to encapsulate.
– StoryTeller
Nov 22 '18 at 8:47
Because file1 is breaking encapsulation by defining a (potential) global variable?
– GermanNerd
Nov 22 '18 at 8:48
c and c++ can be used and abused its really up to the developer to employ good practice. You can do really nasty things in both languages, even Java lets you do some pretty awful things, for example its perfectly possible to define a static public member variable which is globally accessible. In your example, remove extern int x, changed x in file1.c to be static.
– SPlatten
Nov 22 '18 at 8:49
Ah I see.. so is the best practice to attach the 'static' keyword to any variable you would like to keep hidden as implementation detail?
– N. Parikh
Nov 22 '18 at 8:56
@N.Parikh, static just restricts the variable to the scope it is declared in.
– SPlatten
Nov 22 '18 at 9:02
Just because a language has encapsulation facilities doesn't mean a programmer can't fail to encapsulate.
– StoryTeller
Nov 22 '18 at 8:47
Just because a language has encapsulation facilities doesn't mean a programmer can't fail to encapsulate.
– StoryTeller
Nov 22 '18 at 8:47
Because file1 is breaking encapsulation by defining a (potential) global variable?
– GermanNerd
Nov 22 '18 at 8:48
Because file1 is breaking encapsulation by defining a (potential) global variable?
– GermanNerd
Nov 22 '18 at 8:48
c and c++ can be used and abused its really up to the developer to employ good practice. You can do really nasty things in both languages, even Java lets you do some pretty awful things, for example its perfectly possible to define a static public member variable which is globally accessible. In your example, remove extern int x, changed x in file1.c to be static.
– SPlatten
Nov 22 '18 at 8:49
c and c++ can be used and abused its really up to the developer to employ good practice. You can do really nasty things in both languages, even Java lets you do some pretty awful things, for example its perfectly possible to define a static public member variable which is globally accessible. In your example, remove extern int x, changed x in file1.c to be static.
– SPlatten
Nov 22 '18 at 8:49
Ah I see.. so is the best practice to attach the 'static' keyword to any variable you would like to keep hidden as implementation detail?
– N. Parikh
Nov 22 '18 at 8:56
Ah I see.. so is the best practice to attach the 'static' keyword to any variable you would like to keep hidden as implementation detail?
– N. Parikh
Nov 22 '18 at 8:56
@N.Parikh, static just restricts the variable to the scope it is declared in.
– SPlatten
Nov 22 '18 at 9:02
@N.Parikh, static just restricts the variable to the scope it is declared in.
– SPlatten
Nov 22 '18 at 9:02
|
show 1 more comment
1 Answer
1
active
oldest
votes
A way to ensure the encapsulation is declaring x
as static int x = 5;
, so it won't be visible from an external object (even using extern
).
Obviously, a global variable as x
is, can be accessed from another object (via linker, through the extern
you have added). However, this usually leads to a "spaghetti" code, in which variables are accessed and/or modified from wherever because there is not a proper encapsulation (and C or C++ do provide useful ways to avoid that).
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%2f53426936%2fbob-martin-c-has-perfect-encapsulation-how%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
A way to ensure the encapsulation is declaring x
as static int x = 5;
, so it won't be visible from an external object (even using extern
).
Obviously, a global variable as x
is, can be accessed from another object (via linker, through the extern
you have added). However, this usually leads to a "spaghetti" code, in which variables are accessed and/or modified from wherever because there is not a proper encapsulation (and C or C++ do provide useful ways to avoid that).
add a comment |
A way to ensure the encapsulation is declaring x
as static int x = 5;
, so it won't be visible from an external object (even using extern
).
Obviously, a global variable as x
is, can be accessed from another object (via linker, through the extern
you have added). However, this usually leads to a "spaghetti" code, in which variables are accessed and/or modified from wherever because there is not a proper encapsulation (and C or C++ do provide useful ways to avoid that).
add a comment |
A way to ensure the encapsulation is declaring x
as static int x = 5;
, so it won't be visible from an external object (even using extern
).
Obviously, a global variable as x
is, can be accessed from another object (via linker, through the extern
you have added). However, this usually leads to a "spaghetti" code, in which variables are accessed and/or modified from wherever because there is not a proper encapsulation (and C or C++ do provide useful ways to avoid that).
A way to ensure the encapsulation is declaring x
as static int x = 5;
, so it won't be visible from an external object (even using extern
).
Obviously, a global variable as x
is, can be accessed from another object (via linker, through the extern
you have added). However, this usually leads to a "spaghetti" code, in which variables are accessed and/or modified from wherever because there is not a proper encapsulation (and C or C++ do provide useful ways to avoid that).
edited Nov 22 '18 at 9:22
Toby Speight
17.6k134469
17.6k134469
answered Nov 22 '18 at 8:55
JoseJose
1,291516
1,291516
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%2f53426936%2fbob-martin-c-has-perfect-encapsulation-how%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
Just because a language has encapsulation facilities doesn't mean a programmer can't fail to encapsulate.
– StoryTeller
Nov 22 '18 at 8:47
Because file1 is breaking encapsulation by defining a (potential) global variable?
– GermanNerd
Nov 22 '18 at 8:48
c and c++ can be used and abused its really up to the developer to employ good practice. You can do really nasty things in both languages, even Java lets you do some pretty awful things, for example its perfectly possible to define a static public member variable which is globally accessible. In your example, remove extern int x, changed x in file1.c to be static.
– SPlatten
Nov 22 '18 at 8:49
Ah I see.. so is the best practice to attach the 'static' keyword to any variable you would like to keep hidden as implementation detail?
– N. Parikh
Nov 22 '18 at 8:56
@N.Parikh, static just restricts the variable to the scope it is declared in.
– SPlatten
Nov 22 '18 at 9:02